Datagrid error when selecting a header

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sacred18
    New Member
    • Jun 2012
    • 1

    Datagrid error when selecting a header

    First hello to all.
    I have the following code in the cellmouse event to get information from a selected row.

    Code:
    private void dt_Grid_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                datagrid.Rows[0][0] = dt_Grid.Rows[e.RowIndex].Cells[0].Value.ToString();
                datagrid.Rows[0][1] = dt_Grid.Rows[e.RowIndex].Cells[1].Value.ToString();
                datagrid.Rows[0][2] = dt_Grid.Rows[e.RowIndex].Cells[2].Value.ToString();
                datagrid.Rows[0][3] = dt_Grid.Rows[e.RowIndex].Cells[3].Value.ToString();
                datagrid.Rows[0][4] = dt_Grid.Rows[e.RowIndex].Cells[4].Value.ToString();
                datagrid.Rows[0][5] = dt_Grid.Rows[e.RowIndex].Cells[5].Value.ToString();
                datagrid.Rows[0][6] = dt_Grid.Rows[e.RowIndex].Cells[6].Value.ToString();
                datagrid.Rows[0][7] = dt_Grid.Rows[e.RowIndex].Cells[7].Value.ToString();
                datagrid.Rows[0][8] = dt_Grid.Rows[e.RowIndex].Cells[8].Value.ToString();
            }
    but when i select a collumn, i recive an error because i'm selecting a cell with the mouse and i dont have code for that selection. My question is how can i put an "if" on the beguining of the event so that he can determine if it's a header or not.
    Thanks too all in advance.. Any help is welcome.
    Last edited by PsychoCoder; Jun 5 '12, 08:44 PM. Reason: Code tags added
  • Aimee Bailey
    Recognized Expert New Member
    • Apr 2010
    • 197

    #2
    Can I ask how you are populating the DataGridView in the first place? It seems to me that if you are populating the control using a DataTable in the first place, then you could just use the row index to get a DataRow back so you wouldn't have to do this.

    Aimee

    Comment

    Working...