I created a method to automatically generate a combo box in my datagridview, which works 100% now the story looks like this.
I created a table and using Databinding. I am very able to retrieve the values to my Winform and My table Looks Like this
Quote:
|ID| Visit Date | Visitor Name | sign in Time| vehicle Number | Organization | visit type| Reason | ID type | Whom to See |
Then I use this Method below to create an Extra Column which works fine
private void MeetingSelection()
{
DataGridViewComboBoxColumn cmn = new DataGridViewComboBoxColumn();
cmn.Items.Add("Please Select");
cmn.Items.Add("Confirm Appointments");
cmn.Items.Add("Reschedule");
dataGridView1.Columns.Add(cmn);
cmn.HeaderText = "Action";
cmn.Name = "action";
}
Now I want it to be referenced from another method so that I can use it to make a choice, adding that cell 11 is the table where Action is, And it looks like this:
private void RowsColor()
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string val = dataGridView1.Rows[i].Cells[11].Value.ToString();
if (val == "Confirm Appointments")
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
MessageBox.Show("Its Red Now!");
}
else if (val == "Reschedule")
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
MessageBox.Show("Its Yellow Now!");
}
}
}
I get this Exception when I try to make it see the Cell from the datagridview
System.NullReferenceException: Object reference not set to an instance of an object.
And it throws exception at this line string val = dataGridView1.Rows[i].Cells[11].Value.ToString(); Now i am thinking if i replace it with string val = dataGridView1.Rows[i].Cells["Action"].Value.ToString(); instead?
What I have tried:
I tried doing something like this
string val = dataGridView1.Rows[i].Cells["Action"].Value.ToString();