【问题标题】:Preventing a new row from appearing in a DataGridView before current row is filled in?在填充当前行之前防止新行出现在 DataGridView 中?
【发布时间】:2012-04-03 06:43:28
【问题描述】:

我在 C# 3.5 的 WinForm 应用程序中有一个 DataGridView。

AllowUserToAddNewRow 属性设置为真。当用户在 DataGridView 中键入任何文本时,另一个新行会自动添加到 DataGridView。在对当前行执行一些检查并在其中填写所有必要信息之前,我不希望添加此新行。

示例:我有一个带有空白行的 DataGridView:

当我开始输入一个新行时,这还为时过早:

我想要的是仅在用户输入数量后才添加新行:

【问题讨论】:

    标签: c# winforms datagridview datagridviewrow


    【解决方案1】:

    设置 AllowUserToAddNewRow = false 现在首先向您的数据源添加一个空白行,例如。如果您将 DataGridView 绑定到名为 DT 的 DataTable,那么就在之前

    dataGridView1.DataSource = DT;
    

    做类似的事情

     DT.Rows.Add(DT.NewRow());
    

    这是最初有一个空白行,以便可以输入第一条记录。 然后处理事件 dataGridView1.CellEndEdit,在该事件中编写如下内容:

    void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)//The index of your Quantity Column
            {
                int qty = (int)DT.Rows[e.RowIndex][e.ColumnIndex];
                if (qty > 0)//Your logic if required
                {
                    DT.Rows.Add(DT.NewRow());                    
                }
            }
        }
    

    【讨论】:

    • 抱歉无法理解你...你是什么意思抑制行添加?如果您的意思是不应该在 DataGrid 中添加新行,那么只需设置 AllowUserToAddNewRow = false 就像我在上面第一行中提到的那样
    • 我希望 Row 有条件地添加
    • 我认为这就是我们要在这里实现的目标。这个想法是 Rows 永远不会自动添加(您可以通过将 AllowUserToAddNewRow 设置为 False 来实现这一点,我感觉您错过了这一点)。一旦您将此属性设置为 false 就永远不会添加新行,那么您如何添加新行呢?您将新行添加到基础数据源并“有条件地”执行此操作,即当说 Qty 大于 0 时,正如我在上面的 CellEndEdit ebent 处理程序代码中所示。
    • 是的!我已经实现了这个解决方案,但是在这个解决方案中,我们没有任何 NewRow,所以我们无法确定哪一行是新的。但是虽然我已经用这个解决方案解决了我的问题,但谢谢
    【解决方案2】:

    基本上这是一个简单的游戏,包含一些事件并启用/禁用 AllowUserToAddRow 属性:

    public Form1()
            {
                InitializeComponent();
                //creating a test DataTable and adding an empty row
                DataTable dt = new DataTable();
                dt.Columns.Add("Column1");
                dt.Columns.Add("Column2");
                dt.Rows.Add(dt.NewRow());
    
                //binding to the gridview
                dataGridView1.DataSource = dt;
    
                //Set  the property AllowUserToAddRows to false will prevent a new empty row
                dataGridView1.AllowUserToAddRows = false;
            }
    

    现在事件... 当单元格识别编辑时,它将触发一个名为 CellBeginEdit 的事件。当它处于编辑模式时,将 AllowUserToAddRows 设置为 false

    private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
        dataGridView1.AllowUserToAddRows = false;
    }
    

    当单元格识别到编辑结束时,它将触发一个名为 CellEndEdit 的事件。当它结束编辑模式时,检查您的条件。根据结果​​,将 AllowUserToAddRows 设置为 true 或将其保持为 false。

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        //instead of MessageBox there could be as well your check conditions
        if (MessageBox.Show("Cell edit finished, add a new row?", "Add new row?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            dataGridView1.AllowUserToAddRows = true;
        else dataGridView1.AllowUserToAddRows = false;
    }
    

    【讨论】:

      【解决方案3】:

      我知道这是旧的。 最简单的方法是,从设计视图中取消选中“启用添加”

      【讨论】:

      • 这是迄今为止解决不需要时出现的额外空行的最简单方法。
      【解决方案4】:

      我认为在 CellClick 事件中,您可以检查您所在的列,然后添加一个新行,例如:DataGridView1.Rows.Add()

      【讨论】:

      • 用户不需要用鼠标导航,可能是键盘界面
      【解决方案5】:

      这就是它的实现方式。

      a) 您可以使用

      在 RowLeave 事件中检查当前行的内容
      String.IsNullOrWhiteSpace(GridPGLog.Rows[e.RowIndex].Cells[0].value.toString())
      using (or) Cells[0] || cells[1] || cell[2] || ..
      

      如果发现任何错误,将焦点设置到错误单元格并强制用户输入数据。

      DataGridViewRow rowToSelect = this.dgvJobList.CurrentRow;
      rowToSelect.Selected = true;
      rowToSelect.Cells[0].Selected = true;
      this.dgvJobList.CurrentCell = rowToSelect.Cells[0];
      

      b) 或者您可以放置​​一个保存按钮并使用 foreach 循环检查所有新添加的行

      【讨论】:

      • 你上面提到的这可以通过 e.Cancel = true 在 RowValidating 事件上很容易地实现,但我希望如果当前行未填充,则不应添加新行。
      • 如果你执行它,用户将无法在行外聚焦添加新行
      • 但这不是我要找的答案
      • 我只想禁止添加行
      猜你喜欢
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多