Color a row in a Datagrid, is there a solution that works with DataView?
-
I have found one solution for how to color a row in a DataGrid based on the data in the row. Here's one example: guffy.net[^]. The problem is that it doesn't work when the DataGrid is sorted, or when a RowFilter is applied. Example: I have a simple table with two columns, one with integer values and one with strings. 15 Some text -14 Different text 5 Another string 6 Yet another string Say I want all rows where the integer is negative to have a blue background. This is easily done using the example above. In this example the second row will be colored blue. But say I don't want to show any rows with an integer >= 10. When I create the DataGrid I use the code DataView mainDataView = ((DataTable)this.mainDataGrid.DataSource).DefaultView; mainDataView.RowFilter = "[Value] < 10"; Now the DataGrid will look like: -14 Different text 5 Another string 6 Yet another string But now the second row will have a blue background, instead of the first. How can I solve this problem? Any help is appriciated! (btw, I already asked this question in an old thread, but I'm new here and thought a thread would get "bumped" when it got a new message, turns out that the message bord is sorted by the first message in a thread, so I guess noone noticed that message...) /Cesa
-
I have found one solution for how to color a row in a DataGrid based on the data in the row. Here's one example: guffy.net[^]. The problem is that it doesn't work when the DataGrid is sorted, or when a RowFilter is applied. Example: I have a simple table with two columns, one with integer values and one with strings. 15 Some text -14 Different text 5 Another string 6 Yet another string Say I want all rows where the integer is negative to have a blue background. This is easily done using the example above. In this example the second row will be colored blue. But say I don't want to show any rows with an integer >= 10. When I create the DataGrid I use the code DataView mainDataView = ((DataTable)this.mainDataGrid.DataSource).DefaultView; mainDataView.RowFilter = "[Value] < 10"; Now the DataGrid will look like: -14 Different text 5 Another string 6 Yet another string But now the second row will have a blue background, instead of the first. How can I solve this problem? Any help is appriciated! (btw, I already asked this question in an old thread, but I'm new here and thought a thread would get "bumped" when it got a new message, turns out that the message bord is sorted by the first message in a thread, so I guess noone noticed that message...) /Cesa
You should change these two lines
DataRowView v = (DataRowView)source.Current;
string curColumnValue = Convert.ToString(v.DataView.Table.Rows[rowNum][columnName]);to
DataRowView v = (DataRowView)source.Current;
string curColumnValue = Convert.ToString(v.Row[columnName]);A DataRowView knows its associated DataRow. Thus you don't have to look into the DataTable.
-
You should change these two lines
DataRowView v = (DataRowView)source.Current;
string curColumnValue = Convert.ToString(v.DataView.Table.Rows[rowNum][columnName]);to
DataRowView v = (DataRowView)source.Current;
string curColumnValue = Convert.ToString(v.Row[columnName]);A DataRowView knows its associated DataRow. Thus you don't have to look into the DataTable.
Thanks, but for some reason I can't get it to work. To be honest I didn't look into it that much since I managed to find another solution earlier. I modified your example to my actual project: DataRowView v = (DataRowView)source.Current; DateTime date = (DateTime)v.Row["Date"]; ...but it doesn't work. This works fine though: In class: //public version of GetColumnValueAtRow public object PublicGetColumnValueAtRow(System.Windows.Forms.CurrencyManager source, System.Int32 rowNum) { return this.GetColumnValueAtRow(source, rowNum); } In Paint(...): DateTime date = (DateTime)((MyDataGridTextBoxColumn)this.DataGridTableStyle.GridColumnStyles["Date"]).PublicGetColumnValueAtRow(source, rowNum); -- modified at 12:46 Tuesday 17th January, 2006