winforms datagridview行选择isssue

发布于 2025-02-13 23:40:34 字数 659 浏览 3 评论 0原文

我正在执行一个功能,例如我在usercontrol中具有datagridview,并且它以基本形式托管。此网格具有3列,例如ID,名称,年龄和此年龄列是combobox控制。应用程序启动后,网格将带有来自数据库表学生的数据。选择模式是单行明智的。首先,它将显示ID和名称,但是年龄列是空的,用户可以单击该列 - 这是combobox控制。

假设用户填充的数据源于数据库,只有10行。用户单击第四行,并尝试从combobox指定值。很好,没关系。现在查看当前行是第四,然后选择了该行。然后,用户尝试单击第六行。但是它突然选择了第一行[0索引]。我单击了第6行,但没有选择第六行。相反,第一行是自动选择的。

DGV_ROWENTER是我们单击该行时发射的第一个事件。但是,在我不知道的dgv_rowenter事件之前,还有另一个事件正在调用。

那是哪个事件?

首先,我选择一行,然后从combobox [年龄列]中选择一个数字,此后,当我单击任何其他行时 - 当时它正在选择[突出显示]第一行[ 0th rowindex]。

如何防止这种情况?

I am doing a function like I have a DataGridView in my UserControl, and it is hosted in Base Form. This Grid has 3 columns such as ID, Name, Age and this Age column is a ComboBox control. When the application starts, the Grid is populated with data from the database table Students. The selection mode is single Row wise. At first, it will show IDs and Names, but the Age column is empty, and the user can click on that column - which is a ComboBox control.

Suppose that the user populated data stems from the DB, and there are only 10 rows. The User clicks on the 4th row and tries to specify a value from the ComboBox. Fine, that is ok. Now see that the current row is 4th and that row is selected. Then the user tries to click on the 6th row. But it suddenly selects the very first row [0 index]. I clicked on 6th row, but it is not selecting the 6th row. Instead, the very first row is selected automatically.

The dgv_RowEnter is the first event that is fired when we clicked on the row. But there is another event that is invoking before the dgv_RowEnter event that I don't know.

Which is that event?

First, I select a row and picked a number from a ComboBox [Age Column] and after that, when I click on any other row - at that time it is selecting [highlighting] the very first row [0th rowindex].

How to prevent this ?

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

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

发布评论

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

评论(1

一抹苦笑 2025-02-20 23:40:34

在您的帖子中,您问:

DGV_ROWENTER是当我们单击该行时发射的第一个事件。但是,我不知道的DGV_Rowenter事件发生了另一个事件。 那是哪个事件?

您要问的问题表明datagridview的默认行为没有按照您想要的顺序提供所需的事件。使用dataSource属性的数据绑定是使DGV以不错的方式进行的最快方法,这将是我的第一个建议。但是听起来您直接与DGV进行交互,这意味着您是一名高级用户,他想成为DGV发送的事件的“在线前部”。拦截selectedrowselectedColumnselectedcell出门之前的方法,因为事件是为了制作pasterdatagridview:datagridview并覆盖setSelectedRowcoresetSelectedColumnCore,并且setSelectedCellcore方法。

您将需要在form.designer.cs文件中更改两行,

// private System.Windows.Forms.DataGridView dataGridView1;
private CustomDataGridView dataGridView1;

而:

private void InitializeComponent()
{
    // this.dataGridView1 = new System.Windows.Forms.DataGridView();
    this.dataGridView1 = new CustomDataGridView();
    .
    .
    .
}

下面显示的控制台输出表明,如今所示的自定义类是“第一响应者”。

class CustomDataGridView : DataGridView
{
    protected override void SetSelectedRowCore(
        int rowIndex, 
        bool selected)
    {
        Debug.WriteLine($"{MethodBase.GetCurrentMethod().Name} Row {rowIndex}");
        base.SetSelectedRowCore(rowIndex, selected);
    }
    protected override void SetSelectedCellCore(
        int columnIndex, 
        int rowIndex, 
        bool selected)
    {
        Debug.WriteLine($"{MethodBase.GetCurrentMethod().Name} Column {columnIndex} Row {rowIndex}");
        base.SetSelectedCellCore(columnIndex, rowIndex, selected);
    }
    protected override void SetSelectedColumnCore(
        int columnIndex, 
        bool selected)
    {
        Debug.WriteLine($"{MethodBase.GetCurrentMethod().Name} Column {columnIndex}");
        base.SetSelectedColumnCore(columnIndex, selected);
    }
    protected override bool SetCurrentCellAddressCore(
        int columnIndex, 
        int rowIndex, 
        bool setAnchorCellAddress, 
        bool validateCurrentCell, 
        bool throughMouseClick)
    {
        Debug.WriteLine($"{MethodBase.GetCurrentMethod().Name} Column {columnIndex} Row {rowIndex}");
        return base.SetCurrentCellAddressCore(
            columnIndex, 
            rowIndex, 
            setAnchorCellAddress, 
            validateCurrentCell, 
            throughMouseClick);
    }
} 

In your post, you ask:

The dgv_RowEnter is the first event that is fired when we clicked on the row. But there is another event that is invoking before the dgv_RowEnter event that I don't know. Which is that event?

The question you're asking indicates that the default behavior of DataGridView is not providing the events you want in the order you want them. Using data binding with the DataSource property is the fastest way to make DGV behave in a decent manner out of the box and that would be my first suggestion. But it sounds as though you are interacting with the DGV directly which implies that you are an advanced user who wants to be "at the front of the line" for the events that DGV is sending. The way to intercept SelectedRow, SelectedColumn, and SelectedCell before they go out as events is to make a CustomDataGridView : DataGridView and override the SetSelectedRowCore, SetSelectedColumnCore, and SetSelectedCellCore methods respectively.

You will need to change two lines in the Form.Designer.cs file:

// private System.Windows.Forms.DataGridView dataGridView1;
private CustomDataGridView dataGridView1;

and:

private void InitializeComponent()
{
    // this.dataGridView1 = new System.Windows.Forms.DataGridView();
    this.dataGridView1 = new CustomDataGridView();
    .
    .
    .
}

The console output shown below demonstrates that the custom class as shown is now a "first responder" when these states occur.

class CustomDataGridView : DataGridView
{
    protected override void SetSelectedRowCore(
        int rowIndex, 
        bool selected)
    {
        Debug.WriteLine(
quot;{MethodBase.GetCurrentMethod().Name} Row {rowIndex}");
        base.SetSelectedRowCore(rowIndex, selected);
    }
    protected override void SetSelectedCellCore(
        int columnIndex, 
        int rowIndex, 
        bool selected)
    {
        Debug.WriteLine(
quot;{MethodBase.GetCurrentMethod().Name} Column {columnIndex} Row {rowIndex}");
        base.SetSelectedCellCore(columnIndex, rowIndex, selected);
    }
    protected override void SetSelectedColumnCore(
        int columnIndex, 
        bool selected)
    {
        Debug.WriteLine(
quot;{MethodBase.GetCurrentMethod().Name} Column {columnIndex}");
        base.SetSelectedColumnCore(columnIndex, selected);
    }
    protected override bool SetCurrentCellAddressCore(
        int columnIndex, 
        int rowIndex, 
        bool setAnchorCellAddress, 
        bool validateCurrentCell, 
        bool throughMouseClick)
    {
        Debug.WriteLine(
quot;{MethodBase.GetCurrentMethod().Name} Column {columnIndex} Row {rowIndex}");
        return base.SetCurrentCellAddressCore(
            columnIndex, 
            rowIndex, 
            setAnchorCellAddress, 
            validateCurrentCell, 
            throughMouseClick);
    }
} 

event log

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