Click to See Complete Forum and Search --> : DataGridView Display in a Windows Form App


catlook
February 17th, 2009, 01:33 PM
I have a windows form (not web) application, coded in C#, MS Visual Studio 2005.

I have an Account object ( AcctID -string, AcctName -string, Type -bool, and Date -DateTime). Then I have an ArrayList of Accounts called AllAccounts. In order to display AllAccounts in my datagridview on my form, I convert it to a DataTable and then bind it to the grid. Works like a charm.

However, what I'd really like to do is for the Type column display "red" if false and "blue" if true. And I'd like the Date cell to display blank if the Type is "red".

So, I could make a second object, DisplayAcct, with the same 4 attributes as Account, only all strings. Then I could loop through AllAccounts and build a new ArrayList of DisplayAccounts objects(doing a manually population of each attribute according o my requirements), convert that new list to a DataTable, and bind it to the grid. However, when I want to update an account by selecting it from the grid, I'll have to go back and find the original corresponding Account object. Seems kind of convoluted.

Is there any way to bind my original AllAccounts ArrayList to the grid, but change how the grid displays it? (I know web apps can do this easily, but I haven't found a form app solution).

Thanks for considering this!

eclipsed4utoo
February 17th, 2009, 01:38 PM
the one area that the DataGridView sucks at is this. You should look into the "DataBindingComplete" event. You are going to have to loop through the entire grid after it has been databound to change the cell's style.

catlook
February 17th, 2009, 01:53 PM
Can you provide a code snippet of changing the cell datatype? I can find how to change font and stuff like that, but not the actual datatype.

I wonder if it's worth it in the end. Will this really just change the display, or will it chnage the contents?

eclipsed4utoo
February 17th, 2009, 02:48 PM
it will change both. Now, you could set the text color of the cell to the same color as the background. This would make it seem as the cell is blank, but it would preserve the data.

So if you were going to set the background color to red, then set the text color to red also.

You shouldn't need to change the datatype.

catlook
February 17th, 2009, 02:55 PM
ah ha! I'll give it a shot...thanks!!