Click here to Skip to main content
15,924,317 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
for (int i = 0; i < dgvPizza.Rows.Count; i++)
{
    if (dgvPizza.Rows.Count != 0)
    {
        DataRow row = dt.NewRow();
        string qty = dgvPizza.Rows[i].Cells[1].Value.ToString();
        string size = dgvPizza.Rows[i].Cells[0].Value.ToString();
        string Name = dgvPizza.Rows[i].Cells[3].Value.ToString();
        row[0] = qty;
        row[1] = size;
        row[2] = Name;
        dt.Rows.Add(row);
    }
}

I was using this code to select size on ComboBox and enter quantity on TextBox which all of them are on a DataGridView and now I want to only pass the Row that I have selected, but it gives me this error: Object reference not set to an instance of an object. But when I select whole 3 rows it passes them. What shall I do?
Posted
Updated 10-Aug-10 4:22am
v2

1 solution

C++
if ( dgvPizza.Rows.Count!=0)


This is pointless. If the count is 0, your loop would not run.

You need to tell us what line gives you an error, and try using some punctuation so your post is readable. IF you use the SelectedItem property on the control, you can look at just the selected row. It is null only if nothing is selected, you should check for that.

Don't post the code that works, post the code that does not work, so we can see what is going wrong.
 
Share this answer
 
Comments
Nish Nishant 10-Aug-10 12:11pm    
Reason for my vote of 5
Proposed answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900