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
views:
39answers:
1
+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
2010-09-23 11:21:57
Hi small help if i need to write only particular cell data then what should i do
Dorababu
2010-09-23 14:46:48
You mean particular cell data in the last row?
sumit_programmer
2010-09-23 14:50:48
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
2010-09-23 15:02:03
Yeah i got it any way thanks for your code
Dorababu
2010-09-23 15:02:26