【问题标题】:How to add blank rows in a DataGridView in C# desktop application如何在 C# 桌面应用程序的 DataGridView 中添加空白行
【发布时间】:2013-01-07 11:24:21
【问题描述】:

我正在尝试在 datagridview 中创建一些空白行。不幸的是,我无法做到这一点。

如何在网格中实现空白行?

我就是这样实现的..

dgvGetData.AllowUserToAddRows = true;
dgvGetData.Rows.Add(5);

但事情不正常....

【问题讨论】:

  • 你的 DataGridView 是否绑定了数据?如果是这样,您可能需要将数据源的 AllowNew 属性设置为 true。
  • 你写的代码没问题。问题在于您对DataGridView 所做的修改。
  • 无法做到这一点并不是一个足够好的解释。是否抛出错误?

标签: c# datagridview desktop-application


【解决方案1】:

您是否在创建行之前添加了列?如果没有,则会引发错误。

添加空行的一种简单方法是这样。首先,您需要添加列。

//You can assign the Column types while initializing
DataGridViewColumn d1 = new DataGridViewTextBoxColumn();
DataGridViewColumn d2 = new DataGridViewCheckBoxColumn();
DataGridViewColumn d3 = new DataGridViewImageColumn();

//Add Header Texts to be displayed on the Columns
d1.HeaderText = "Column 1"; 
d2.HeaderText = "Column 2";
d3.HeaderText = "Column 3";

//Add the Columns to the DataGridView
DataGridView1.Columns.AddRange(d1, d2, d3);

//Finally add the Rows
DataGridView1.Rows.Add(5);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多