Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将某些列从datatable获取到datagridview?_C# - Fatal编程技术网

C# 如何将某些列从datatable获取到datagridview?

C# 如何将某些列从datatable获取到datagridview?,c#,C#,我有一个数据表,我想从中提取某些信息(只有某些行和某些列)。我试图使用下面的代码,但在运行它时,我得到了一个索引超出范围的错误,我不确定我所做的是否是从datatable到datagridview只获取某些数据的最佳方法。有什么想法吗 currentRow = 0; int dataGridRow = 0; foreach (DataRow row in resultsDT.Rows) { string va

我有一个数据表,我想从中提取某些信息(只有某些行和某些列)。我试图使用下面的代码,但在运行它时,我得到了一个索引超出范围的错误,我不确定我所做的是否是从datatable到datagridview只获取某些数据的最佳方法。有什么想法吗

        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[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++;
        }

我只能猜你需要的是

   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++;
        }

dataGridView2包含多少行?是否至少成功地将一个值放入网格中,或者一开始就失败了?就是这样,Haris!非常感谢!