Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.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# DataGridView列排序异常_C#_Winforms_Sorting_Datagridview - Fatal编程技术网

C# DataGridView列排序异常

C# DataGridView列排序异常,c#,winforms,sorting,datagridview,C#,Winforms,Sorting,Datagridview,我想在加载表单时按列对DataGridView进行排序,但出现了一个异常 private void frm_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'dataSetExclusion.Exclude' table. You can move, or remove it, as needed. this.excludeTable

我想在加载表单时按列对DataGridView进行排序,但出现了一个异常

    private void frm_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'dataSetExclusion.Exclude' table. You can move, or remove it, as needed.
        this.excludeTableAdapter.Fill(this.dataSetExclusion.Exclude);
        this.dgv.Sort(this.dgv.Columns["ID"], ListSortDirection.Ascending); 
    }
DataGridView dgv中列的标题文本为“ID”。其DataPropertyName为“ExcludeID”。我尝试将“ID”和“ExcludeID”作为列名,但仍然得到了异常

Value cannot be null.Parameter name: dataGridViewColumn

列的名称可能与其文本表示形式不同。根据文档,该索引器查看的是
Name
属性,而不是
HeaderText
属性。列设计器应允许您更改名称或查看当前名称。

尝试检查该列的name属性,如下所示


此问题有两个可能的原因,要么您没有绑定DataGridView,要么您使用了不正确的列名

在您的表单中,您的加载似乎永远不会绑定网格的数据源。您需要将代码更改为以下内容(猜测dataSetExclusion.Exclude是您在网格中想要的内容?)

检查列名有多种方法—您可以始终在此处放置断点方法并查看调试器,也可以查看数据集设计器。网格中的列名将与设计器中的列名匹配(如果您使用的是表单设计器列和dataproperty名称,请在表单设计器中查找名称)


请注意,名称和HeaderText不必匹配,因此使用HeaderText通常不起作用。

如果在排序行设置断点并检查this.dgv.Columns,是否在列表中看到“ID”或“ExcludeID”?您在哪里设置了dgv.DataSource?
private void frm_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'dataSetExclusion.Exclude' table. You can move, or remove it, as needed.
    this.excludeTableAdapter.Fill(this.dataSetExclusion.Exclude);
    this.dgv.DataSource = this.dataSetExclusion.Exclude;
    this.dgv.Sort(this.dgv.Columns["ID"], ListSortDirection.Ascending); 
}