0

I have 2 forms.. gridForm has a dataGridView gets its data from database, the second form editFormhas textboxes similar to the gridView Columns.

i want to select a row in the grid then click an edit button and the editForm must shown and the textboxes have the values from grid

gridForm

private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex >= 0)
        {
            DataGridViewRow row = dataGridView1.Rows[e.RowIndex];

            // when i put this line only , it works right.
            edditContactForm.edditContactNameSetter = row.Cells["contactNameGridViewColumn"].Value.ToString();

            // this also with the previous is working right too.
            edditContactForm.edditJobTitleSetter = row.Cells["jobTitleGridViewColumn"].Value.ToString();

            // the problem appears here and the exhibition shown to this line and any similar lines under it. 
            edditContactForm.edditCompanyNameSetter = row.Cells["CompanyNameGridViewColumn"].Value.ToString();
        }
    }

editForm:

    public string edditContactNameSetter 
    {
        set { txtContactName.Text = value; }
    }

    public string edditJobTitleSetter
    {
        set { txtJobTitle.Text = value; }
    }

    public string edditCompanyNameSetter
    {
        set { txtCompanyName.Text = value; }
    }

exhibition:

The exhibition appers when i select any row from the grid enter image description here

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Mahmoud
  • 197
  • 1
  • 5
  • 16
  • Are you sure you have a column with `CompanyNameGridViewColumn` ? – Habib Oct 04 '13 at 17:01
  • It seems that the `row.Cells["CompanyNameGridViewColumn"].Value` is null. – Hamlet Hakobyan Oct 04 '13 at 17:03
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Oct 04 '13 at 17:03
  • 1
    Try checking to see if it has a value before you convert it to a string with ToString() – Jon La Marr Oct 04 '13 at 17:03
  • how can i ensure if its value is null or not – Mahmoud Oct 04 '13 at 17:07
  • I think part of your problem here is that your naming conventions aren't consistent. You have some 'someVariableName' and some 'OtherVariableName'. Likely, that is your problem. As Hamlet mentioned above, `row.Cells["CompanyNameGridViewColumn"].Value` is (most likely) `null`. – Brian Oct 04 '13 at 17:10
  • @Mahmoud - set a breakpoint at `edditContactForm.edditCompanyNameSetter = row.Cells["CompanyNameGridViewColumn"].Value.ToString();` and inspect the value of it. – Brian Oct 04 '13 at 17:13
  • @JohnSaunders I still can't solve the problem.. is `row.Cells["CompanyNameGridViewColumn"].Value` is null that means it is not get values from database or something else??....... i don't get the real meaning of `null` .. – Mahmoud Oct 04 '13 at 18:40
  • `row.Cells["CompanyNameGridViewColumn"]` may be null by itself, meaning there is no column with that name. – John Saunders Oct 04 '13 at 18:41
  • @JohnSaunders i've take it copy/paste from `Columns: (Collection)` property – Mahmoud Oct 04 '13 at 18:44
  • Use the debugger! Stop before the error and see what's in `row.Cells["CompanyNameGridViewColumn"]` – John Saunders Oct 04 '13 at 18:49

1 Answers1

0

OK, i found the problem, and since i am a newbie in c#, i think it's a naive mistake for professionals.

the problem was in the SELECT FROM query, i just didn't select the companyName values from database, so the row.Cells["CompanyNameGridViewColumn"].Value gets null.

Mahmoud
  • 197
  • 1
  • 5
  • 16