DataBinding Row DataGrid
-
Hi, I have a datagrid with several columns, one is "Finish Date", I would like that the rows that has a finish date greater than the current data have a different background color that the other one. I have thought about create a calculated, hidden column, that will hold the column of the row, it's possible then bind that calculated column, to the background color of the row ?, Is there another easier way to do it ? thanks in advance, greetings Braulio
-
Hi, I have a datagrid with several columns, one is "Finish Date", I would like that the rows that has a finish date greater than the current data have a different background color that the other one. I have thought about create a calculated, hidden column, that will hold the column of the row, it's possible then bind that calculated column, to the background color of the row ?, Is there another easier way to do it ? thanks in advance, greetings Braulio
The unfortunate truth is that in ASP.NET, doing this with a
DataGrid
(System.Web.UI.WebControls.DataGrid
, that is) is incredibly easier. You handle theItemDataBound
event, which gives you the item (the object being bound) to which you can set properties based on the data being bound, such as different font styles, colors, etc. Easy, huh? I'm guessing you're not doing this in ASP.NET, though. Now it gets harder. The best way is to derive your own class fromDataGridColumnStyle
(or perhaps even formDataGridTextBoxColumn
which you're most likely already using for the column style). If you choose the latter, it already exposes theTextBox
that is hosted when you edit the cell. Note, though, that it is not enough to set theBackColor
of theTextBox
because it's only visible when the cell is being edited. So, after deciding which you want to do (I recommend the latter), override thePaint
method and - based on the value bound to the cell - paint the appropriate background. The only thing left to do is modify theDataGrid
initialization code. If you're not already using aDataGridTableStyle
, you should because it gives you a high degree of control over the display and is still versitile enough to handle different data sources. See theDataGrid.TableStyles
property for more info. To thisDataGridTableStyle
, you add your variousDataGridColumnStyle
derivatives, such as theDataGridTextBoxColumn
, theDataGridCheckBoxColumn
, and your new-fangled derivative you created from above. If you already have one, just modified the source for that column to use your object instead of theDataGridTextBoxColumn
instance. It may sound like quite a bit of work, but this is an extremely versitile solution. Instead of handling special cases that require code every time you run into this problem, you make a single class that you can easily reuse - which is what component development is all about! If you create your own little library of handy classes like this, you won't even have to copy and paste source files from project to project - just reference the library assembly and specify the type. In the end, it definitely pays off and is "in the spirit" of the .NET base class library, i.e. extending it rather than working around it. -
The unfortunate truth is that in ASP.NET, doing this with a
DataGrid
(System.Web.UI.WebControls.DataGrid
, that is) is incredibly easier. You handle theItemDataBound
event, which gives you the item (the object being bound) to which you can set properties based on the data being bound, such as different font styles, colors, etc. Easy, huh? I'm guessing you're not doing this in ASP.NET, though. Now it gets harder. The best way is to derive your own class fromDataGridColumnStyle
(or perhaps even formDataGridTextBoxColumn
which you're most likely already using for the column style). If you choose the latter, it already exposes theTextBox
that is hosted when you edit the cell. Note, though, that it is not enough to set theBackColor
of theTextBox
because it's only visible when the cell is being edited. So, after deciding which you want to do (I recommend the latter), override thePaint
method and - based on the value bound to the cell - paint the appropriate background. The only thing left to do is modify theDataGrid
initialization code. If you're not already using aDataGridTableStyle
, you should because it gives you a high degree of control over the display and is still versitile enough to handle different data sources. See theDataGrid.TableStyles
property for more info. To thisDataGridTableStyle
, you add your variousDataGridColumnStyle
derivatives, such as theDataGridTextBoxColumn
, theDataGridCheckBoxColumn
, and your new-fangled derivative you created from above. If you already have one, just modified the source for that column to use your object instead of theDataGridTextBoxColumn
instance. It may sound like quite a bit of work, but this is an extremely versitile solution. Instead of handling special cases that require code every time you run into this problem, you make a single class that you can easily reuse - which is what component development is all about! If you create your own little library of handy classes like this, you won't even have to copy and paste source files from project to project - just reference the library assembly and specify the type. In the end, it definitely pays off and is "in the spirit" of the .NET base class library, i.e. extending it rather than working around it.Thanks for your answer. I'm using windows forms, well a bit of work to make that, it's a pity that the VBA get effects like this so fast, and we have to spend some time into it ( how can you explain to that guy that the first time you need an hour to make a thing that he makes in five minutes :-( ). Another problem that I found on the Windows Form Datagrid, is that there not an easy way to fake the load of a result ( I mean, load only the first 1000 entries, then by demand load the others or all, something like that). Greetings Braulio