Passing a control name? Visual Studio C# WM5

44 views Asked by At

Been out of program for long time. Just learning VB 2008 and things have changed since 1999.

I have 3 Datagrids. I want to group all 3 row clicks into one area and get the CurrentRowIndex.

So can you pass a control name to get CurrentRowIndex of the 3 grid?

I know this does not work:

private void tblCollectionDataGrid_Click(object sender, EventArgs e)
{
    Control control = (Control)sender;
    MessageBox.Show(control.Name);

    row = control.Name.CurrentRowIndex;
}

or do you have to type each one out? This works but ... want it simpler if possible.

private void tblCollectionDataGrid_Click(object sender, EventArgs e)
{
    Control control = (Control)sender;
    MessageBox.Show(control.Name);

    switch (control.Name)
    { 
        case "tblMemoDataGrid":
            row = tblMemoDataGrid.CurrentRowIndex;
            break;
        case "tblReportDataGrid":
            row = tblReportDataGrid.CurrentRowIndex;
            break;
        case "tblInfoDataGrid":
            row = tblInfoDataGrid.CurrentRowIndex;
            break;
    }
}
1

There are 1 answers

1
JohnG On

I am not sure exactly what you mean by

"group all 3 row clicks into one area and get the CurrentRowIndex"

if there are 3 DataGridViews there will be possibly 3 DIFFERENT row indexes.

using the same "Click" event for all 3 DatagridViews

private void dataGridView_Click(object sender, EventArgs e)
{
  int index1 = dataGridView1.CurrentCell.RowIndex;
  int index2 = dataGridView2.CurrentCell.RowIndex;
  int index3 = dataGridView3.CurrentCell.RowIndex;
  MessageBox.Show("i1: " + index1 + "  i2: " + index2 + "  i3: " + index3);
}

this will allow you to see which rows are selected for all the DatagridViews. Maybe I am not understanding the question....