Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I see the DataGridViewRow added to a DataGridView?

I am trying to show rows in a DataGridView.

Here's the code:

foreach (Customers cust in custList)
            {
                string[] rowValues = { cust.Name, cust.PhoneNo };
                DataGridViewRow row = new DataGridViewRow();
                bool rowset = row.SetValues(rowValues);
                row.Tag = cust.CustomerId;
                dataGridView1.Rows.Add(row);
            }

On form load, I have initialized dataGridView1 as:

dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].Name = "Name";
dataGridView1.Columns[1].Name = "Phone";

After this code is executed, four notable things happen:

  • I can see a new row created in the dataGridView1.
  • There's no text in it.
  • rowset is false after row.SetValues method is executed.
  • The row tag value is set correctly.

Why doesn't the DataGridView show data?

like image 205
Null Head Avatar asked Oct 12 '22 06:10

Null Head


1 Answers

List<customer> custList = GetAllCustomers();
            dataGridView1.Rows.Clear();

            foreach (Customer cust in custList)
            {
                //First add the row, cos this is how it works! Dont know why!
                DataGridViewRow R = dataGridView1.Rows[dataGridView1.Rows.Add()];
                //Then edit it
                R.Cells["Name"].Value = cust.Name;
                R.Cells["Address"].Value = cust.Address;
                R.Cells["Phone"].Value = cust.PhoneNo;
                //Customer Id is invisible but still usable, like,
                //when double clicked to show full details
                R.Tag = cust.IntCustomerId;
            }

http://aspdiary.blogspot.com/2011/04/adding-new-row-to-datagridview.html

like image 188
Null Head Avatar answered Oct 15 '22 10:10

Null Head