简体   繁体   English

在填充当前行之前阻止新行出现在DataGridView中?

[英]Preventing a new row from appearing in a DataGridView before current row is filled in?

I have a DataGridView in my WinForm application in C# 3.5. 我在C#3.5的WinForm应用程序中有一个DataGridView。

AllowUserToAddNewRow property is set true. AllowUserToAddNewRow属性设置为true。 When user types any text in DataGridView, another new row is automatically added to DataGridView. 当用户在DataGridView中键入任何文本时,另一个新行会自动添加到DataGridView中。 I do not want this new row added until some checks are performed on the current row and all the necessary information has been filled in there. 我不希望在当前行上执行某些检查并添加所有必要信息之前添加此新行。

Example : I have a DataGridView with a blank row: 示例:我有一个带空行的DataGridView: DataGridView有一个空行

When I begin typing a new row is added, which is too soon: 当我开始输入时,添加了一个新行,这太快了:

What I want is that the new row be added only after the user enters a Quantity: 我想要的是只有在用户输入数量后才添加新行:

Set AllowUserToAddNewRow = false Now add a blank row initially to your datasource for eg. 设置AllowUserToAddNewRow = false现在最初在数据源中添加一个空行,例如。 if you are binding the DataGridView to a DataTable called DT then just before 如果你将DataGridView绑定到一个名为DT的DataTable,那么就在之前

dataGridView1.DataSource = DT;

Do something like 做点什么

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

This is to have one blank row initially so that the first record can be entered. 这是最初有一个空行,以便可以输入第一个记录。 Then handle the event dataGridView1.CellEndEdit, in that event write something like this: 然后处理事件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());                    
            }
        }
    }

Basically it's a simple play with some events and enabling/disabling the AllowUserToAddRow property: 基本上它是一个简单的游戏,包含一些事件并启用/禁用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;
        }

Now the events... When the cell recognize the editing it will fire a event called CellBeginEdit. 现在事件......当单元格识别编辑时,它将触发一个名为CellBeginEdit的事件。 When it's in editing mode set the AllowUserToAddRows to false 当它处于编辑模式时,将AllowUserToAddRows设置为false

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

When the cell recognize the end of editing it will fire a event called CellEndEdit. 当单元格识别出编辑结束时,它将触发一个名为CellEndEdit的事件。 When it ends the editing mode check for your conditions. 当它结束编辑模式时检查您的条件。 Based on the result set the AllowUserToAddRows to true ot keep it false. 根据结果​​集,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;
}

I know this is old. 我知道这是旧的。 The easiest way is, uncheck "Enable Adding" from the design view 最简单的方法是,从设计视图中取消选中“启用添加”

在此输入图像描述

我想在事件CellClick上你可以检查你是哪一列,然后添加一个新行,如: DataGridView1.Rows.Add()

This is how it is achieved. 这就是它的实现方式。

a) You can check contents of current row in RowLeave event using a)您可以使用在RowLeave事件中检查当前行的内容

String.IsNullOrWhiteSpace(GridPGLog.Rows[e.RowIndex].Cells[0].value.toString())
using (or) Cells[0] || cells[1] || cell[2] || ..

if any error is found, set focus to the error cell and force user to enter data. 如果发现任何错误,请将焦点设置为错误单元格并强制用户输入数据。

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

b) or you can place a Save button and check all newly added rows using a foreach loop b)或者您可以使用foreach循环放置“保存”按钮并检查所有新添加的行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM