Style of a DataGridView.Row by using DataSet
-
The following code shows you how I created a DataSet and added it to the DataGridView. To define the default column style, I added 2 columns to the DataGridView before and set their DataPropertyName.
DataSet ds = new DataSet(); ds.Tables.Add(); ds.Tables[0].Columns.Add("a"); ds.Tables[0].Columns.Add("b"); colColumn1.DataPropertyName = "a"; colColumn2.DataPropertyName = "b"; for (int i = 0; i < 10; i++) { ds.Tables[0].Rows.Add("a" + i, "b" + i); } dataGridView1.DataSource = ds.Tables[0];
Now I have the problem that I can't change any style (BackColor) of the rows that will be added by setting the DataSource. Can I define it somehow in the DataSet (DataRow) OR is there any event that will be called after the row finished adding? [I don't want to go through each row after the DataSource is setted and set the new style]modified on Friday, June 5, 2009 3:33 AM
-
The following code shows you how I created a DataSet and added it to the DataGridView. To define the default column style, I added 2 columns to the DataGridView before and set their DataPropertyName.
DataSet ds = new DataSet(); ds.Tables.Add(); ds.Tables[0].Columns.Add("a"); ds.Tables[0].Columns.Add("b"); colColumn1.DataPropertyName = "a"; colColumn2.DataPropertyName = "b"; for (int i = 0; i < 10; i++) { ds.Tables[0].Rows.Add("a" + i, "b" + i); } dataGridView1.DataSource = ds.Tables[0];
Now I have the problem that I can't change any style (BackColor) of the rows that will be added by setting the DataSource. Can I define it somehow in the DataSet (DataRow) OR is there any event that will be called after the row finished adding? [I don't want to go through each row after the DataSource is setted and set the new style]modified on Friday, June 5, 2009 3:33 AM
In that way i currently solved it:
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { int i = 0; try { dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White; if (dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString().Contains(textBox1.Text)) { dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.ForestGreen; } int ii = 1 / i; } catch { } }
I used the RowPrePaint event. So everytime when the row will be painted there is a check if the BackColor should be changed. I tested it with 1,000,000 rows in fullscreen and the performance is (not the best but) ok. But if you have another solution, feel free to post it. -
The following code shows you how I created a DataSet and added it to the DataGridView. To define the default column style, I added 2 columns to the DataGridView before and set their DataPropertyName.
DataSet ds = new DataSet(); ds.Tables.Add(); ds.Tables[0].Columns.Add("a"); ds.Tables[0].Columns.Add("b"); colColumn1.DataPropertyName = "a"; colColumn2.DataPropertyName = "b"; for (int i = 0; i < 10; i++) { ds.Tables[0].Rows.Add("a" + i, "b" + i); } dataGridView1.DataSource = ds.Tables[0];
Now I have the problem that I can't change any style (BackColor) of the rows that will be added by setting the DataSource. Can I define it somehow in the DataSet (DataRow) OR is there any event that will be called after the row finished adding? [I don't want to go through each row after the DataSource is setted and set the new style]modified on Friday, June 5, 2009 3:33 AM
-
Instead of trying to set the background through code its better to use CSS.
Regards Venkatesh Ellur Engineer - Software +919886417764 +919886235553