Datagridview with custom columns containing user defined control

787 views
Skip to first unread message

R

unread,
Sep 3, 2010, 4:23:16 PM9/3/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
How can I create a custom column in datagridview that displays data in
a user control?
In the datagridview I have to show the Goal value and it corresponding
Achieved value for each year.

ex:
| 2010 | 2011 | 2012
======================================
Earnings | T | $20000 | $25000 | $30000
| A | $18000 | $26000 | $29000
---------------------------------------------------------------------------
Expenses | T | $10000 | $10000 | $10000
| A | $9000 | $10000 | $12000
---------------------------------------------------------------------------
New Hire | T | 20 | 10 | 6
| A | 22 | 10 | 5


So I need two textboxes showing in the cell at the same time (both
when the cell is in the display mode and when it is in Editing mode)

Can this be achieved? Any other ideas are more than welcome.

Thank you.

Jamie Fraser

unread,
Sep 3, 2010, 5:52:34 PM9/3/10
to dotnetde...@googlegroups.com
TemplateColumn

R

unread,
Sep 3, 2010, 6:42:54 PM9/3/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Template column is in gridview in ASP.NET not in datagridview in
windows forms.
> > Thank you.- Hide quoted text -
>
> - Show quoted text -

Arsalan Tamiz

unread,
Sep 4, 2010, 2:24:11 AM9/4/10
to dotnetde...@googlegroups.com
Normally the rendering of a "Cell" in DataGridView control is handled through CellPainting event. If your goal is to just change the look of a cell then this is simple. You just have to use the CellPainting event.

But here as I can see you need custom editing too. For this purpose you need to host your own control in DataGridView Cell.

Here is a msdn article which describes how to host DateTimePicker control in DataGridView cell. This will give you a quick start.

Jamie Fraser

unread,
Sep 4, 2010, 6:58:00 AM9/4/10
to dotnetde...@googlegroups.com
Whoops - guess I shouldn't post after being in the pub!

Davaasuren Nyamjav

unread,
Sep 3, 2010, 7:59:23 PM9/3/10
to dotnetde...@googlegroups.com
1. Add TemplateField in Colums
2. Go to Edit Template
3. Select ItemTemplate
4. Drop whatever control you like to use (such as textbox in ur case)
--
David Nyamjav
---------------------------------------------------
Tel:     213-453-3248
Email: dava...@gmail.com

Adityo Dwiarto

unread,
Sep 3, 2010, 6:02:05 PM9/3/10
to dotnetde...@googlegroups.com

Thanks,

Adityo
====================================
sent from my Blackberry®

R

unread,
Sep 7, 2010, 6:34:26 PM9/7/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
As I said earlier I am using DatagridView in winforms and not gridview
in ASP.NET.

Template columns are possible in gridView only.

Thank you.
> Email: davaa....@gmail.com- Hide quoted text -

R

unread,
Sep 7, 2010, 6:42:18 PM9/7/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I am not sure what to do in the in cell painting event. I have looked
at the example given in msdn site and had implemented this example
with my custom control. When I look at the datagrid, I do not see my
control instead I see string[].Array. When I click on the cell and go
into edit mode, I can see and edit my control.
How can I modify the code so that by default, the data is displayed in
my user control. Thank you.

On Sep 3, 11:24 pm, Arsalan Tamiz <sallus...@gmail.com> wrote:
> Normally the rendering of a "Cell" in DataGridView control
> is handled through CellPainting event. If your goal is to just change the
> look of a cell then this is simple. You just have to use the CellPainting
> event.
>
> But here as I can see you need custom editing too. For this purpose you need
> to host your own control in DataGridView Cell.
>
> Here is a msdn article<http://msdn.microsoft.com/en-us/library/7tas5c80(v=VS.90).aspx>which
> > > - Show quoted text -- Hide quoted text -

Arsalan Tamiz

unread,
Sep 8, 2010, 2:21:26 AM9/8/10
to dotnetde...@googlegroups.com
Your custom control only displays when you edit the cell. While in non-editing mode DataGridView itself renders the data. However as you have already derived a class from DataGridViewTextBoxCell, therefore you can customize the display by overriding the Paint method in the same class.

I mean you need to modify your custom "cell" class which you have most probably derived from DataGridViewTextBoxCell. In this class you can override its Paint method to render the data as you like.

Long ago I created a custom DataGridView Progress Bar column which does NOT support editing but does custom painting. You can look into the code to have a better idea.

http://arsalantamiz.blogspot.com/2008/04/datagridview-custom-percentage-column.html

I guess "Group Moderator" wont mind as the link is related to the topic :)
Reply all
Reply to author
Forward
0 new messages