Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. DataGridView problem with cell coloring [modified]

DataGridView problem with cell coloring [modified]

Scheduled Pinned Locked Moved Database
databasehelpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mattzimmerer
    wrote on last edited by
    #1

    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

    L D M 3 Replies Last reply
    0
    • M Mattzimmerer

      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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.


      M 1 Reply Last reply
      0
      • M Mattzimmerer

        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

        D Offline
        D Offline
        darkelv
        wrote on last edited by
        #3

        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;
        
                }
        
            }
        
        1 Reply Last reply
        0
        • L Luc Pattyn

          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.


          M Offline
          M Offline
          Mattzimmerer
          wrote on last edited by
          #4

          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!!!!

          1 Reply Last reply
          0
          • M Mattzimmerer

            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

            M Offline
            M Offline
            Mattzimmerer
            wrote on last edited by
            #5

            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...

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups