0

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);
    }
Community
  • 1
  • 1
Rilcon42
  • 9,584
  • 18
  • 83
  • 167
  • 1
    The only other piece of error data is: object reference not set to instance of object. Yes I plan to show everything in the textbox for now. I am just testing code. – Rilcon42 Jan 03 '15 at 22:32

2 Answers2

1

To avoid the NullReferenceException you may use:

ResultsText.Text +="\n"+ (cell.Value == null ? "NULL" : cell.Value.ToString());

instead of

ResultsText.Text +="\n"+ cell.Value.ToString();

(assuming that was the problem).

UPDATED

The problem might have occured because of the code initializing combo box column. Please try setting the DataPropertyName:

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 = "LoadCaseCol";

        dataGridView1.DataSource = dt;
        dataGridView1.Columns.AddRange(lc);
    }
  • This fixed the error, but Im still not sure why Im having this problem. Any idea what could be causing this? – Rilcon42 Jan 03 '15 at 22:33
1

You'll get a NullReferenceException if cell.Value is null when you call ToString().

You could use Convert.ToString() instead, which specifically checks for null and converts it to an empty string (preventing an exception from being thrown):

var sb = new StringBuilder();

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    foreach (DataGridViewCell cell in row.Cells)
    {
        sb.AppendLine(Convert.ToString(cell.Value));
    }
}

ResultsText.Text = sb.ToString();
Grant Winney
  • 65,241
  • 13
  • 115
  • 165