Forum guidelines
AuthorQuestion
How to retrieve data from grid view in .NET
Posted By Farrukh Javeid on 21 Jul 2010
Hello,

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.

AuthorReply
Re: DataGridView Exception
Posted By Kirtan Patel on 21 Jul 2010  
I highlighted the mistake ..by red color you need loop till count-1


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

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

MY BLOG ( www.codestructs.com ) | MY SITE

Kirtan
 Patel   BCA   MCA (SEMESTER-IV)  MINDCRACKER MVP WEB HACKER



Re: DataGridView Exception
Posted By Sam Hobbs on 21 Jul 2010  
See also DataGridView Exception.
Thinking is a feeling; pleasant for some and unpleasant for others.

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.