DataGridView problem with cell coloring [modified]
-
I'm not sure if this is the best place to post this, but I'm retrieving a table form an SQL database and databinding the contents into a DataGridView. I have some code that searches the entire DataGridView and locates null values, and I intend to color the fields red so the user knows he needs to put something in.
private void button1\_Click(object sender, EventArgs e) { //MessageBox.Show(dataGridView.ColumnCount.ToString()); for (int column = 0; column < 1; column++)//this.dataGridView.ColumnCount { for (int row = 0; row < this.dataGridView.RowCount; row++) { //MessageBox.Show("Row,Column: "+row.ToString()+","+column.ToString()); if (this.dataGridView.Rows\[row\].Cells\[column\].Value == null) { this.dataGridView.Rows\[row\].Cells\[column\].Style.BackColor= Color.Red; //EDITED: changed property to BackColor like I should have posted it... //MessageBox.Show("Null at: " + row.ToString() + "," + column.ToString()); } } } }
theres still some messageboxes in there that I used to figure this out... If I change the conditional statement to (this.dataGridView.Rows[row].Cells[column].Value != null) so that it searches for non null fields, it successfully colors the cells. I guess my problem has to do with the nullity (lol is that a word?) of my cells. I guess I should include how I am populating my dataGridView...:
private void Form1\_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the '\_Vehicle\_EquipmentDataSet.\_Pickups\_\_\_Cars' table. You can move, or remove it, as needed. this.pickups\_\_\_CarsTableAdapter.Fill(this.\_Vehicle\_EquipmentDataSet.\_Pickups\_\_\_Cars); }
that was auto generated =) Perhaps I can handle the null data when the list is populated? Thanks in advance!!!
modified on Thursday, March 18, 2010 12:45 PM
-
I'm not sure if this is the best place to post this, but I'm retrieving a table form an SQL database and databinding the contents into a DataGridView. I have some code that searches the entire DataGridView and locates null values, and I intend to color the fields red so the user knows he needs to put something in.
private void button1\_Click(object sender, EventArgs e) { //MessageBox.Show(dataGridView.ColumnCount.ToString()); for (int column = 0; column < 1; column++)//this.dataGridView.ColumnCount { for (int row = 0; row < this.dataGridView.RowCount; row++) { //MessageBox.Show("Row,Column: "+row.ToString()+","+column.ToString()); if (this.dataGridView.Rows\[row\].Cells\[column\].Value == null) { this.dataGridView.Rows\[row\].Cells\[column\].Style.BackColor= Color.Red; //EDITED: changed property to BackColor like I should have posted it... //MessageBox.Show("Null at: " + row.ToString() + "," + column.ToString()); } } } }
theres still some messageboxes in there that I used to figure this out... If I change the conditional statement to (this.dataGridView.Rows[row].Cells[column].Value != null) so that it searches for non null fields, it successfully colors the cells. I guess my problem has to do with the nullity (lol is that a word?) of my cells. I guess I should include how I am populating my dataGridView...:
private void Form1\_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the '\_Vehicle\_EquipmentDataSet.\_Pickups\_\_\_Cars' table. You can move, or remove it, as needed. this.pickups\_\_\_CarsTableAdapter.Fill(this.\_Vehicle\_EquipmentDataSet.\_Pickups\_\_\_Cars); }
that was auto generated =) Perhaps I can handle the null data when the list is populated? Thanks in advance!!!
modified on Thursday, March 18, 2010 12:45 PM
Mattzimmerer wrote:
this.dataGridView.Rows[row].Cells[column].Style.ForeColor = Color.Red;
how do you see the ForeColor of an empty cell? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
I'm not sure if this is the best place to post this, but I'm retrieving a table form an SQL database and databinding the contents into a DataGridView. I have some code that searches the entire DataGridView and locates null values, and I intend to color the fields red so the user knows he needs to put something in.
private void button1\_Click(object sender, EventArgs e) { //MessageBox.Show(dataGridView.ColumnCount.ToString()); for (int column = 0; column < 1; column++)//this.dataGridView.ColumnCount { for (int row = 0; row < this.dataGridView.RowCount; row++) { //MessageBox.Show("Row,Column: "+row.ToString()+","+column.ToString()); if (this.dataGridView.Rows\[row\].Cells\[column\].Value == null) { this.dataGridView.Rows\[row\].Cells\[column\].Style.BackColor= Color.Red; //EDITED: changed property to BackColor like I should have posted it... //MessageBox.Show("Null at: " + row.ToString() + "," + column.ToString()); } } } }
theres still some messageboxes in there that I used to figure this out... If I change the conditional statement to (this.dataGridView.Rows[row].Cells[column].Value != null) so that it searches for non null fields, it successfully colors the cells. I guess my problem has to do with the nullity (lol is that a word?) of my cells. I guess I should include how I am populating my dataGridView...:
private void Form1\_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the '\_Vehicle\_EquipmentDataSet.\_Pickups\_\_\_Cars' table. You can move, or remove it, as needed. this.pickups\_\_\_CarsTableAdapter.Fill(this.\_Vehicle\_EquipmentDataSet.\_Pickups\_\_\_Cars); }
that was auto generated =) Perhaps I can handle the null data when the list is populated? Thanks in advance!!!
modified on Thursday, March 18, 2010 12:45 PM
You should check the value from the data source itself, not datagridview's cells. Subscribe to the datagridview's CellFormatting event, then in the handler get the DataBoundItem (object, datarow etc) and set color base on the DataBoundItem's data itself. I'm binding to collection of object, btw, so YMMV.
private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
string dataPropertyName = this.dataGridView.Columns[e.ColumnIndex].DataPropertyName;
ObjectType theObject;
switch (dataPropertyName)
{
case "ColumnName":
theObject = (ObjectType)this.dataGridView.Rows[e.RowIndex].DataBoundItem;// Set the color base on theObject's property mapped to dataPropertyName break; } }
-
Mattzimmerer wrote:
this.dataGridView.Rows[row].Cells[column].Style.ForeColor = Color.Red;
how do you see the ForeColor of an empty cell? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
Ohh, i should have changed that back. No I first tryed backcolor or w/e, which works find for populated cells... that was a purley desperate attempt.. THE PROBLEM PERSISTS!!!!
-
I'm not sure if this is the best place to post this, but I'm retrieving a table form an SQL database and databinding the contents into a DataGridView. I have some code that searches the entire DataGridView and locates null values, and I intend to color the fields red so the user knows he needs to put something in.
private void button1\_Click(object sender, EventArgs e) { //MessageBox.Show(dataGridView.ColumnCount.ToString()); for (int column = 0; column < 1; column++)//this.dataGridView.ColumnCount { for (int row = 0; row < this.dataGridView.RowCount; row++) { //MessageBox.Show("Row,Column: "+row.ToString()+","+column.ToString()); if (this.dataGridView.Rows\[row\].Cells\[column\].Value == null) { this.dataGridView.Rows\[row\].Cells\[column\].Style.BackColor= Color.Red; //EDITED: changed property to BackColor like I should have posted it... //MessageBox.Show("Null at: " + row.ToString() + "," + column.ToString()); } } } }
theres still some messageboxes in there that I used to figure this out... If I change the conditional statement to (this.dataGridView.Rows[row].Cells[column].Value != null) so that it searches for non null fields, it successfully colors the cells. I guess my problem has to do with the nullity (lol is that a word?) of my cells. I guess I should include how I am populating my dataGridView...:
private void Form1\_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the '\_Vehicle\_EquipmentDataSet.\_Pickups\_\_\_Cars' table. You can move, or remove it, as needed. this.pickups\_\_\_CarsTableAdapter.Fill(this.\_Vehicle\_EquipmentDataSet.\_Pickups\_\_\_Cars); }
that was auto generated =) Perhaps I can handle the null data when the list is populated? Thanks in advance!!!
modified on Thursday, March 18, 2010 12:45 PM
Fixed my problem:
private void Null\_check\_Click(object sender, EventArgs e) { for (int row = 0; row < this.dataGridView.RowCount-1; row++) for (int column = 0; column < this.dataGridView.ColumnCount; column++) if (this.dataGridView.Rows\[row\].Cells\[column\].Value.ToString() == "") this.dataGridView.Rows\[row\].Cells\[column\].Style.BackColor = Color.Red; }
just needed sleep I guess...