开发者

How can I get certain columns from datatable to datagridview?

I have a datatable that I want to extract certain information from (only certain rows and only certain columns). I'm trying to use the code below, but I'm getting an index out of range error when I run it, and I'm not sure if what I'm doing is the best way to get only certain data from a datatable to a datagridview. Any ideas?

        currentRow = 0;
        int dataGridRow = 0;
        foreach (DataRow row in resultsDT.Rows)
        {
            string value = resultsDT.Rows[currentRow]["HighLow"].ToString();
            if (value.Equals("High") | value.Equals("Low"))
            {
                dataGridView2.Rows[dataGridRow].Cells["colHighLow"].Value = resultsDT.Rows[currentRow]["HighLow"];
                dataGridView2.Rows[dataGridRow].Cells["colDifference"].Value = resultsDT.Rows[currentRow]["Difference"];
                dataGridView2.Rows[dataGridRow].Cells["colMbrSep"].Value = resultsDT.Rows[currentRow]["MBRSEP"];
                dataGridView2.Rows[dataGridRow].Cells["colLocation"].Value = resultsDT.Rows[currentRow]["LOCATION"];
                dataGridView2.Rows[dataGri开发者_运维知识库dRow].Cells["colDistrict"].Value = resultsDT.Rows[currentRow]["DIST"];
                dataGridView2.Rows[dataGridRow].Cells["colAddress"].Value = resultsDT.Rows[currentRow]["ADDR1"];
                dataGridView2.Rows[dataGridRow].Cells["colMeter"].Value = resultsDT.Rows[currentRow]["METER"];
                dataGridView2.Rows[dataGridRow].Cells["colKWh"].Value = resultsDT.Rows[currentRow]["KWH"];
                dataGridRow++;
            }
            currentRow++;
        }


I can only guess what you need is

   currentRow = 0;
        int dataGridRow = 0;
        foreach (DataRow row in resultsDT.Rows)
        {
            string value = resultsDT.Rows[currentRow]["HighLow"].ToString();
            if (value.Equals("High") | value.Equals("Low"))
            {
                //THIS LINE
                dataGridView2.Rows.Add();

                dataGridView2.Rows[dataGridRow].Cells["colHighLow"].Value = resultsDT.Rows[currentRow]["HighLow"];
                dataGridView2.Rows[dataGridRow].Cells["colDifference"].Value = resultsDT.Rows[currentRow]["Difference"];
                dataGridView2.Rows[dataGridRow].Cells["colMbrSep"].Value = resultsDT.Rows[currentRow]["MBRSEP"];
                dataGridView2.Rows[dataGridRow].Cells["colLocation"].Value = resultsDT.Rows[currentRow]["LOCATION"];
                dataGridView2.Rows[dataGridRow].Cells["colDistrict"].Value = resultsDT.Rows[currentRow]["DIST"];
                dataGridView2.Rows[dataGridRow].Cells["colAddress"].Value = resultsDT.Rows[currentRow]["ADDR1"];
                dataGridView2.Rows[dataGridRow].Cells["colMeter"].Value = resultsDT.Rows[currentRow]["METER"];
                dataGridView2.Rows[dataGridRow].Cells["colKWh"].Value = resultsDT.Rows[currentRow]["KWH"];
                dataGridRow++;
            }
            currentRow++;
        }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜