开发者

From DataGridView to StringBuilder obj

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[co开发者_如何学Cl].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.


you do not need this:

dataGridView2.Rows[currentRow]

because the same object is already here:

dgr

in this way using directly dgr as you should, you simplify getting rid of currentRow

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜