views:

39

answers:

1

I would like to find the last row of the datagridview and i would like to write that particular row data to a text file can any one help me

+7  A: 

I will give you a sample code, it may help.

 List<string> lstContents = new List<string>();
        foreach (DataGridViewCell cell in mydataGrid.Rows[mydataGrid.RowCount - 1].Cells)
        {
            lstContents .Add((string)cell.Value);
        }
 string myData= string.Join(",", lstContents.ToArray());

Now using streamwriter you can write this string to your file. Also you can use any separator. I have used "," comma here. ///This will append data to your text file...

using (StreamWriter sw = new StreamWriter(FilePath, true))
        {
            sw.WriteLine(myData);
        }
sumit_programmer
Hi small help if i need to write only particular cell data then what should i do
Dorababu
You mean particular cell data in the last row?
sumit_programmer
If you want particular cell data, you can modify the above code like this: reomve the foreach loop, and in sw.WriteLine(mydataGrid.Rows[mydataGrid.RowCount - 1].Cells[yourCellNumber]); put zero based cell number that you need data of
sumit_programmer
Yeah i got it any way thanks for your code
Dorababu