I have a DataGridView (dgv1) with some data loaded into it. I’d like to loop through dgv1 and append the data from specific columns (0,1,3 and 4) to a StringBuilder object (sb). I envisioned it would look something like this:
// Add data to the StringBuilder
int currentRow = 0;
StringBuilder temp = new StringBuilder();
foreach (DataGridViewRow row in dgv1.Rows)
{
foreach (DataGridViewColumn col in dgv1.Columns)
{
temp.Append(dataGridView2.Rows[currentRow].Cells[col].Value.ToString());
currentRow++;
}
}
However, I don’t think this is quite right. Can someone offer some advice?
Don’t think it’s not right, honestly.
One thing that I can think about is actually, if you use a DataBinding to bind to DataGrid, iterate over data binded values and not UI control content. In this case you potentially can use LINQ, which will lead to more clear code….
Regards.