【问题标题】:Delete Top Row from DataGridView Without Clicking无需单击即可从 DataGridView 中删除第一行
【发布时间】:2016-07-21 11:21:37
【问题描述】:

我想在 datagridview 加载到表单之前删除第一行。 我现在拥有的是:

dataGridView1.Rows.Remove(dataGridViewRow[0]);

这不起作用。谁能告诉我应该如何调整我的代码以使其正常工作?

【问题讨论】:

  • 在加载任何数据之前,您有 0 行,因此您无法删除任何内容。

标签: c# datagridview datagridviewrow


【解决方案1】:

你可以试试这个:

dataGridView1.DeleteRow(dataGridViewRow[0].RowIndex);

【讨论】:

    【解决方案2】:

    也许你可以试试这个并将它应用到你的表单加载事件中。

    if(dataGridView1.Rows.Count > 0)
                dataGridView1.Rows.RemoveAt(0);
    

    【讨论】:

      【解决方案3】:

      首先,确保数据已加载。如果您不确定 - 调试、单步执行并确保存在第 0 行。

      然后,尝试以下操作:

          dataGridView1.Rows.Remove(dataGridView1.Rows[0]);
      

      进一步讨论行删除here。可能会给你一些其他的想法。

      【讨论】:

        【解决方案4】:

        数据网格数据绑定到集合吗?如果是这样,您可以通过执行以下操作来排除第一个元素:

        List<string> list = GetSomeData();
        dataGridView1.DataSource = list.GetRange(1, list.Count-1);
        dataGridView1.DataBind();
        

        【讨论】:

          猜你喜欢
          • 2017-12-02
          • 2016-08-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-10-06
          相关资源
          最近更新 更多