当 DataGridViewComboBox 的选定索引更改时,如何立即更改行颜色?

发布于 2024-12-11 00:46:51 字数 280 浏览 0 评论 0原文

我正在使用 Windows 窗体,并且有一个 DataGridView 和绑定到数据源的 DataGridViewComboBoxColumn。

当用户从组合框中选择不同的项目时,我想立即更改行颜色以指示此新选择。

我已经测试了多个事件,例如 CellValueChanged 和 RowPrePaint,但这些事件要求用户在做出选择后单击行外的按钮。

该行似乎不会立即更新。相反,它会在用户单击该行后更新。 (即这是大多数网格的工作方式,但我想改变这种行为并为用户提供即时反馈)

I'm using Windows Forms and have a DataGridView with a DataGridViewComboBoxColumn that is bound to a data source.

When the user chooses a different item from the combo box, I'd like to immediately change the row color to indicate this new selection.

I've tested several events such as CellValueChanged and RowPrePaint, but these requires that the user clicks off the row after making the selection.

It seems like the row doesn't update immediately. Instead, it updates after the user clicks off the row. (i.e. this is how most grids work but I'd like to change this behavior and give the user immediate feedback)

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

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

发布评论

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

评论(1

左秋 2024-12-18 00:46:51

您可以使用 DataGridView 的 EditingControlShowing 事件并为 ComboBox.SelectedIndexChanged 事件添加事件处理程序:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    ComboBox cb = e.Control as ComboBox;

    if (cb != null)
    {
        cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
    }
}

并在事件处理程序中设置 CurrentRow 的颜色:

void cb_SelectedIndexChanged(object sender, EventArgs e)
{
    ComboBox cb = sender as ComboBox;

    if (cb != null)
    {
        // check the selected index, update the DataGridView.CurrentRow.DefaultCellStyle.BackColor
    }
}

You can use the EditingControlShowing event of the DataGridView and add an event handler for the ComboBox.SelectedIndexChanged event:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    ComboBox cb = e.Control as ComboBox;

    if (cb != null)
    {
        cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
    }
}

and in the event handler, set the color for the CurrentRow:

void cb_SelectedIndexChanged(object sender, EventArgs e)
{
    ComboBox cb = sender as ComboBox;

    if (cb != null)
    {
        // check the selected index, update the DataGridView.CurrentRow.DefaultCellStyle.BackColor
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文