9

I have a DataGridView on a TabPage. When the user clicks on a row, a second DGV appears. Each row is associated with its own DGV filled with data. What I want is for when the user goes from one row to another, the DataGridView changes too. So far I've tried the SelectionChanged event but I don't want the DGV to reload in the event that the user clicks on a separate cell in the same row. Any help would greatly be appreciated.

void dgv1_CellClick(object sender, DataGridViewCellEventArgs e)           
{    
    int rowIndex = dgv1.Rows[e.RowIndex].Index;

    if (dgv1 == null)
        return;
    if (dgv1.Rows[e.RowIndex].Cells[rowIndex].Selected == true);
    {
        dgv2.Size = new Size(dgv2.Width + 800, dgv2.Height);
        dgv2.Location = new Point(0, 500);   

        tp.Controls.Add(dgv2);

        Console.WriteLine("Row clicked");
    }
}

-Tatiana

4
  • 1
    Can you create a property that represents the currently selected row, and then check if the newly selected row is the same or not? Commented Jan 14, 2014 at 22:04
  • There actually is a property for the currently selected row which is dgv.CurrentRow the problem is how to check for the newly selected row. Secondly, CurrentRow is of type DataGridViewRow which cannot be converted to type int and therefore I get an error when trying to compare it to any int. Commented Jan 14, 2014 at 22:31
  • 1
    DataGridViewRow has an Index property, so it'd be dgv.CurrentRow.Index. MSDN: msdn.microsoft.com/en-us/library/… Commented Jan 14, 2014 at 22:38
  • use e.rowIndex inside rowenter and rowleave. they trigger when you navigate to a row up or down (doesnt require you to enter in to editing mode of that cell/row ) gotcha if you use dgv.currentRow it will give you previous row it may confuse you. be sure to use e.rowIndex that comes from event. Commented Jun 6, 2018 at 10:28

2 Answers 2

15
    int curRow = -1;

    private void dgv1_SelectionChanged(object sender, EventArgs e)
    {
        if (dgv1.CurrentRow.Index != curRow)
        {
            curRow = dgvPatientDetail.CurrentRow.Index;        
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Or set dgv1.SelectMode = FullRowSelect; Shouldn't fire SelectionChanged when click is within same row
3

I know the post is old but for others reading: Setting ..

DataGridView1.FullRowSelect = true

should resolve this issue.

1 Comment

Do you mean dgv1.SelectionMode = FullRowSelect, which is already mentioned above?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.