简体   繁体   中英

which DataGridView event to call after a row is selected

My DataGridView contains ~200 rows, each of them containing some user info (such as Last name). When an user selects any of these rows, I need to retrieve the ID of selected row in order to query my database for more info. As such, when the user selects a row, I iterate through the whole list untill I find matching ID. Here's the code to visualize what I'm talking about:

if (e.RowIndex > -1)
{
    int selectedId = Convert.ToInt32(dataGridView.Rows[e.RowIndex].Cells["IdColumn"].Value);
    foreach (var user in Users)
        if (user.Id == selectedId)
        {
            SelectedUser = GetUserById(user.Id);
            break;
        }
}

Now, the event I'm using here is DataGridView.RowEnter and it's a problem because this event fires even when the list is loaded for the first time, thus executing foreach loop at the very least a hundred times.
So the question is: which event should I use so that the code above is executed only once, after a row is selected?

Here is the resolution for your problem , you can use the below event to select the row effectively do what we want , I have posted an example below based on your code for better understanding.

Example:

private void dataGridView_SelectionChanged(object sender, EventArgs e)
    {
    DataGridView gv = sender as DataGridView;
    if (gv != null && gv.SelectedRows.Count > 0)
        {
        DataGridViewRow row = gv.SelectedRows[0];
        if (row != null)
            {
            if(Convert.ToInt32(row.Cells["IdColumn"].Value)==user.Id)
              {

                      //do whatever we want

               }

            }
        }
    }

Hope it will surely helpful,kindly let me know if any further clarification or doubts. Happy sharing

thanks karthik

Karthik Elumalai just got first but just to point his solution out: You used

public event DataGridViewCellEventHandler RowEnter

This event occurs when the DataGridView is initially loaded, as well as when the user selects a row other than the current row.

msdn Documentation for rowenter

the behavior you explained is just what this event was made up for. So you better try it with the suggestion from Karthik Elumalai:

public event EventHandler SelectionChanged

This event occurs whenever cells are selected or the selection is canceled, whether programmatically or by user action. For example, this event is useful when you want display the sum of the currently selected cells.

msdn Documentation for selectionchanged

The most viable option was proposed by @LarsTech in a comment: connecting the event only after all data is loaded. And since my data is sorted after being loaded, I executed it like this:

private void dataGridView_Sorted(object sender, EventArgs e)
    {
        this.dataGridView.SelectionChanged -= new System.EventHandler(this.dataGridView_SelectionChanged);
        this.dataGridView.SelectionChanged += new System.EventHandler(this.dataGridView_SelectionChanged);
    }

(I also used the code provided by @Karthik to make the event SelectionChanged -based instead of RowEnter -based)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM