如何将 DataGridViewComboBoxColumn 绑定到 OnChange 事件 (C#)

发布于 2024-10-14 21:23:43 字数 174 浏览 5 评论 0原文

我有一个标准的 DataGridView,我的最后一列是 DataGridViewComboBoxColumn。 我想添加一个事件,以便当该列中任何行的选定索引发生更改时,都会触发一个事件并将该数据保存到数据库中。

我为此苦苦挣扎了一个小时左右,找不到任何会触发此问题的事件...

任何帮助将不胜感激!

I have a standard DataGridView, and my last column is a DataGridViewComboBoxColumn.
I would like to add an event so that when the selected index of any of the rows in that column changes, an event is triggered and I save that data to db.

I'm struggling with this for an hour or so and couldn't find any event that would trigger this...

Any help would be appreciated!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2024-10-21 21:23:43

DataGridViewEditingControlShowing 事件中,将一个方法附加到组合框 SelectedIndexChanged 事件。

例如:

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

现在在下面的方法中你可以做任何你想做的事情:

private void LastColumnComboSelectionChanged(object sender, EventArgs e)
{
  // Do saving work here
}

In the EditingControlShowing event of the DataGridView attach a method to the combobox SelectedIndexChanged event.

For example:

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

Now in the below method you can do whatever you want:

private void LastColumnComboSelectionChanged(object sender, EventArgs e)
{
  // Do saving work here
}
梦明 2024-10-21 21:23:43

你可以尝试一下这些行
组合框是一个编辑控件,所以

private void dg_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
  if (dg.CurrentCell.ColumnIndex == [yourcolumnindex])
  {
    ComboBox cmbox = e.Control as ComboBox;
    cmbox.SelectedValueChanged -= new EventHandler(cmbox_SelectedValueChanged);
    cmbox.SelectedValueChanged += new EventHandler(cmbox_SelectedValueChanged);
  }
}

现在在这种情况下你可以做你的事情
但是否要求每次索引更改都需要访问数据库?

You can try something on these lines
The combobox is an editing control, so

private void dg_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
  if (dg.CurrentCell.ColumnIndex == [yourcolumnindex])
  {
    ComboBox cmbox = e.Control as ComboBox;
    cmbox.SelectedValueChanged -= new EventHandler(cmbox_SelectedValueChanged);
    cmbox.SelectedValueChanged += new EventHandler(cmbox_SelectedValueChanged);
  }
}

Now in that event you can do your stuff
But is it required that for every index change you would be hitting the database?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文