DataGridView selectionChanged event firing multiple times

99封情书 提交于 2019-11-30 05:14:52

问题


In my application I am using DataGridView to display the list. When user select a record in the datagridview, it should display details in the other panel. For selection I was asked to use DataGridView1_SelectionChanged event.

The DataGridView should contain only one column, but the details display may have more information of the selected record( We can get the all the details form the database by querying with the selected Primary key value).

Please tell Is it possible to get datakey(Primary key) of the selected DataGridView record. Does DataGridView will bind based on Primary key ?

Edit

I have implemented DataGridView1.Columns["ID"].Visible = false. It worked well. For selection I added DataGridView1_SelectionChanged event.

But DataGridView1_SelectionChanged event is firing multiple times.


回答1:


You can use DataGrid.focused property in selectionchanged event before executing the code like this :

private void dg_SelectionChanged(object sender, EventArgs e)
    {
        if (dg.Focused)
        {
            // your code
        }
    } 



回答2:


I got the same problems today, after hours of experiment with it, I found a solution or rather a workaround for this problem

this.dgvSearchResult.SelectionChanged -= dgvSearchResult_SelectionChanged;
this.onSearch();
this.dgvSearchResult.SelectionChanged += new EventHandler(dgvSearchResult_SelectionChanged);

Hope it's help




回答3:


I have tried with RowEnter event but, it works fine for me. How do you check it? using message box or placing break point in the event. Because, breakpoint and messagebox will loos the focus from the selected row. Then, when you continue after messagebox or breakpoint the grid will get focused back and the event will be executed again.

Try following step to check event execution.

To handle multiple execution.

int LastRowIndex =-1;

private void dgv_RowEnter(...)
{
    if (LastRowIndex != e.RowIndex)
    {
        //Place your code here.
    }
    LastRowIndex = e.RowIndex;
}



回答4:


If the data binding is reset, you can get one SelectionChanged notifying that the list is temporarily empty followed by another after the data has populated. Worse, the selected item may not even have changed since before the reset. I verified this by checking SelectedCells on each call.

One solution is to use an invalidation variable, and check in the Application.Idle event. This has the nice effect of grouping changes and avoiding unnecessary updates. Application.Idle runs as soon as the message pump is empty, so the user won't notice a delay. It's even better if you check if a change is even required after the grid has sorted itself out.

You'll need to set up the static form instance property in the constructor.

private bool isViewUpToDate = false;

private void DataGrid_SelectionChanged(object sender, EventArgs e) => isViewUpToDate = false;

public void CheckDataGridSelectionView()
{
    if (isViewUpToDate)
        return;

    // Logic goes here

    isViewUpToDate = true;
}

static void Main()
{
    Application.Idle += (sender, eventData) => MainForm.Instance?.CheckDataGridSelectionView();

    // ...
}


来源:https://stackoverflow.com/questions/22979149/datagridview-selectionchanged-event-firing-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!