Forums - C# Corner

Forum guidelines

How to retrieve data from grid view in .NET

Jul 21 2010 11:28 AM

I have been trying to get the values from DataGridView. My stupid logic works a bit when accessing the individual cells of the Data Grid Row.
This is the the code that I am using.

private void btn_fetch_Click(object sender, EventArgs e)
           string result = "";
           string part1 = "";
           string part2 = "";

           for (int i = 1; i <= DGV_CsvFile.Rows.Count; i++)
               part1 = DGV_CsvFile.Rows[i].Cells[0].Value.ToString();
               part2 = DGV_CsvFile.Rows[i].Cells[1].Value.ToString();
               result =result + part1+" : "+part2+"\n";

But the problem comes when it completes its iteration, the following exception comes on my face. :(
"Object reference not set to an instance of an object."
and it selects the first line of the loop.
Need urgent help fellows.
Thanx in advance.

Answers (2)

File APIs for .NET – 25% off Aspose.Total
Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more! Use coupon code HOL2016PSB