在DataGridView中的组合框单元格上进行选择将添加新行 [英] Making selection on combobox cell in DataGridView is adding a new row

查看:81
本文介绍了在DataGridView中的组合框单元格上进行选择将添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在Visual Studio(Winforms)中使用datagridview并遇到此问题。

I am learning how to use the datagridview in Visual Studio (Winforms) and have this problem.

当我运行程序时,单击第一列(包含一个名为Item的组合框列,然后选择第一行单元格并进行选择,则会在其下自动添加一个新行,我不想发生此行。

When I run the program, and click on the first column (containing a combobox column named Item) and select the first row cell and make a selection, a new row is being automatically added below it which I do not want to happen.

我的代码:

// set values to combobox column cells in datagridview
DataGridViewComboBoxColumn cmbItems = (DataGridViewComboBoxColumn)GridSellProducts.Columns["Item"];

cmbItems.DataSource = productNames;
cmbItems.AutoComplete = true;

GridSellProducts.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(GridSellProducts_EditingControlShowing);

private void LastColumnComboSelectionChanged(object sender, EventArgs e)
{
      string itemValue = GridSellProducts.Rows[GridSellProducts.CurrentCell.RowIndex].Cells["Item"].FormattedValue.ToString();

      // get item price
      foreach (var item in itemListing)
      {
           if (item.name == itemValue)
           {
                    unitPrice = item.selling;
                    break;
           }
      }
}

private void GridSellProducts_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
     if (GridSellProducts.CurrentCell.ColumnIndex == 0 && e.Control is ComboBox)
     {
          ComboBox comboBox = e.Control as ComboBox;
          comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
     }
}

我相信这是原因(行加法) string itemValue 始终返回。而且我需要获取itemValue才能设置适当的价格。
是什么导致行添加?

I believe this is the reason (the row addition) that string itemValue is always returning "". And I need to get the itemValue in order to set the appropriate price. What is causing the row addition?

推荐答案

当用户可以向网格中添加新行时,这是默认行为。插入的行称为 NewRow ,等待新输入。要禁用此功能,必须将 AllowUserToAddRows 设置为false,但是这种方式必须实现添加新行的逻辑。

It is default behaviour when your user may add new rows to grid. Inserted row is so called NewRow that awaits for new input. To disable this you have to set AllowUserToAddRows to false but this way you have to implement your logic to add new rows.

还应注意以这种方式将事件添加到内部控件:

Also beware for adding event to internal controls this way:

private void GridSellProducts_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
     if (GridSellProducts.CurrentCell.ColumnIndex == 0 && e.Control is ComboBox)
     {
          ComboBox comboBox = e.Control as ComboBox;
          comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
     }
}

起初看起来还不错,但每次单击时在combobox上,您要添加新的事件处理程序,从而导致触发不止一次!正确方法:

At first it looks okay but every time you are clicking on combobox you are adding new event handler resulting in firing more than one times! Correct way:

private void GridSellProducts_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
     if (GridSellProducts.CurrentCell.ColumnIndex == 0 && e.Control is ComboBox)
     {
          ComboBox comboBox = e.Control as ComboBox;
          comboBox.SelectedIndexChanged -= LastColumnComboSelectionChanged; //remove event if it was added before
          comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
     }
}

这篇关于在DataGridView中的组合框单元格上进行选择将添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆