简体   繁体   English

如何在填充上面一行的特定单元格之后将新行添加到datagridview?

[英]How to add new row to datagridview only after specific cells of the row above are filled?

I'm working on a desktop application in C# (.NET 4.0). 我正在使用C#(.NET 4.0)开发桌面应用程序。 I have a datagridview filled with custom objects, via custom (it inherits BindingList) BindingList (added sorting functionalities). 我有一个datagridview填充自定义对象,通过自定义(它继承BindingList)BindingList(添加排序功能)。 I use cellValidating events to properly increment first column (ID) and to validate input into other cells. 我使用cellValidating事件来正确地增加第一列(ID)并验证输入到其他单元格。

The problem is when I get to the new row / last row and enter something into one cell (I can even delete all the input before leaving the cell) the datagridview automatically adds this row to binding list and creates a new row below. 问题是当我到达新行/最后一行并在一个单元格中输入内容(我甚至可以在离开单元格之前删除所有输入)时,datagridview会自动将此行添加到绑定列表并在下面创建一个新行。 I want that the last / new row is only added when the row above (previous new row) is properly filled. 我希望只在正确填充上面的行(上一个新行)时才添加最后一个/新行。

Example

ID     NAME      PRICE    ...

1      Beer       2.6

2      Cheese      3.3

_      _______     ____      <- empty row

Now if I click (enter) the new row ID is automatically set to 3 and if I leave click to 'Beer' cell, new row ID is empty and all default values are empty (which is the way it should & does work). 现在,如果我单击(输入),新行ID将自动设置为3,如果我将“点击”保留为“啤酒”单元格,则新行ID为空,所有默认值均为空(这是它应该和工作的方式)。 The problem is if if click / enter new row and type something for name (and even if I delete the content before leaving cell) a new row is added below this row, which was new row. 问题是如果单击/输入新行并输入名称的内容(即使我在离开单元格之前删除了内容),也会在此行下方添加一个新行,即新行。 Thus this row is added to BindingList and is incomplete, it shouldn't be added until all required cells are properly filled (for instance price). 因此,此行被添加到BindingList并且不完整,在正确填充所有必需单元格(例如价格)之前不应添加该行。

I have no idea how to do this. 我不知道该怎么做。 Please help me out, I'm quite new to C#. 请帮帮我,我是C#的新手。

Thank you for your time and answers. 谢谢你的时间和答案。

You have to handle the RowValidating event and cancel the handling of the event after checking specific conditions ( validation rules ). 您必须在检查特定条件( 验证规则 )后处理RowValidating事件并取消对事件的处理。 Then, the currently being entered row will not be committed to the DataGridView and no new row will be added. 然后,当前正在输入的行将不会提交到DataGridView,也不会添加新行。

For example: 例如:

if (dgv[0, e.RowIndex].Value == DBNull.Value)
{
    dgv.Rows[e.RowIndex].ErrorText = "You must enter a value for this field!";

    // Tell the DataGridView not to accept this row
    e.Cancel = true;
}

If you are developing a windows application and have textboxes to get input from user to update to the DataGridView, you can do it in C# itself, consider the following: 如果您正在开发一个Windows应用程序并具有文本框以从用户获取输入以更新到DataGridView,您可以在C#中执行此操作,请考虑以下事项:

if(txtNAME.Text.Trim()!=String.Empty&&txtPRICE.Text.Trim()!=String.Empty)
{
  MessageBox.Show("Invalid type of input","Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

else
{
  SqlConnection _sqlconnectionOne=new SqlConnection(*DatabaseConnectionString*);

  SqlCommand _sqlcommandOne=new SqlCommand();

  _sqlcommandOne.CommandType=CommandType.Text;
  _sqlcommandOne.CommandText="*INSERTStatement*";
  _sqlcommandOne.Connection=_sqlconnectionOne;
  _sqlcommandOne.ExecuteNonQuery();

  SqlConnection _sqlconnectionSecond=new SqlConnection(*DatabaseConnectionString*);

  SqlCommand _sqlcommandSecond=new SqlCommand();

  _sqlcommandSecond.CommandType=CommandType.Text;
  _sqlcommandSecond.CommandText="*SELECT*Statement*";
  _sqlcommandSecond.Connection=_sqlconnectionSecond;

  SqlDataAdapter _sqldataadapter=new SqlDataAdapter(_sqlcommandSecond);

  DataTable _datatable=new DataTable();

  _sqldataadapter.fill(_datatable);
  *DataGridViewName*.DataSource=_datatable;
}

Or you can use the Leave Event for the textboxes but it becomes a little hustle sometimes. 或者你可以使用文本框的离开事件,但有时会变得有点喧嚣。 Suggestions are always welcomed! 建议随时欢迎!

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

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