I have declared a DataGridView (called dataGridView1) in my layout. The cell value I am trying to get is the result of the combobox selection that is in the cell. Based on these two links: MSDN and SO Post my code looks like this:
private void Submit_Click(object sender, EventArgs e)
{
//output data to ResultsText richtextbox to check it
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
ResultsText.Text +="\n"+ cell.Value.ToString();// getting null reference exception here
}
}
}
My DataGridView code implementation:
private void PopulateDataGridView()
{
dataGridView1.AutoGenerateColumns = false;
DataTable dt = new DataTable();
dt.Columns.Add("LoadCaseCol");
DataGridViewComboBoxColumn lc = new DataGridViewComboBoxColumn();
lc.DataSource = new List<string>() { "opt1", "opt2", "opt3", "opt4" };
lc.HeaderText = "Select Load Cases";
//lc.DataPropertyName = "Money";
//DataGridViewTextBoxColumn name = new DataGridViewTextBoxColumn();
//name.HeaderText = "Name";
//name.DataPropertyName = "Name";
dataGridView1.DataSource = dt;
//dataGridView1.Columns.AddRange(name, money);
dataGridView1.Columns.AddRange(lc);
}