none
How can i get the index of the SelectedRow in a DataGridView

    Question

  • Hi all

    One small and probably a simple question.

    How can i get the index of the DataGridView1.SelectedRow?

     

    Lars

    Tuesday, September 12, 2006 6:46 PM

All replies

  • DataGridView.SelectedIndex.ToString();

    Convert.ToInt32(DataGridView.SelectedIndex.ToString());

    Best of luck to you. 

    Tuesday, September 12, 2006 7:39 PM
  • actually you dont need to do any conversions.... the SelectedIndex just gives you back an Int, so no need for unnecessary conversions from Int to String then back to Int
    • Proposed as answer by UtsavGoswami Friday, January 04, 2013 10:44 AM
    Tuesday, September 12, 2006 8:03 PM
  • I don't get the SelectedIndex as a choice.

    My testprogram. (displaying a right-click menu. When right clicking the row is selected and some detalis will occur)

     

    public Form1()
    {
    InitializeComponent();
    }
    private void detailsToolStripMenuItem_Click(object sender, EventArgs e)
    {
    int index = this.dataGridView.???????????????????
    DataGridViewRow dgvrow = this.dataGridView1.Rows[index];
    DataRowView drvrow = (DataRowView)dgvrow.DataBoundItem;
    if (drvrow != null)
    {
    String contract = drvrow["ttttt"].ToString();
    ShowContractDetail(contract);
    }
    }
    private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
    if (e.Button == MouseButtons.Right)
    {
    this.dataGridView1.Rows[e.RowIndex].Selected = true;  //the rows get selected. Goodie
    }
    }

    I know i can set the rowindex public for the entire class but i don't want to do that...i want to read the "selected row" from the datagrid

     

    Thank you for trying.

    Tuesday, September 12, 2006 8:24 PM
  • do you mean you want to get the entire row?

    DataGridViewSelectedRowCollection theRowsSelected = this.theDataGridView.SelectedRows;

     

    would get you a collection of rows selected.

    Tuesday, September 12, 2006 8:26 PM
  • Can you just use the Row or Col properties?
    Tuesday, September 12, 2006 8:27 PM
  • No i do not want the enire row. I want the value of columne[0] in the selected row.

     

     

    Tuesday, September 12, 2006 9:35 PM
  • to get the column value of the selected row:

    this.theDataGridView.Rows[index].Cells[Index].Value

    you would have to probably take the first index  from using my example earlier of obtaining the selected rows, something like maybe:

    (untested)

    DataGridViewSelectedRowCollection t = this.theDataGridView.SelectedRows;

    object theValue = this.theDataGridView.Rows[t[0].Index].Cells[index].Value;

     

    Or:

    object theValue = this.theDataGridView[columnIndex, rowIndex].Value;

    does this help?

    • Proposed as answer by sanjayk2010 Monday, March 26, 2012 6:19 AM
    Tuesday, September 12, 2006 9:40 PM
  • Hi Lars.

    First you need to choose which event is necessary to get this value.
    Below I present you 2 samples:

    By datagridview_CellMouseDown

            private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
            {
                textBox1.Text = dataGridView1.CurrentRow[e.RowIndex].ToString();
            }
     
    By Button_Click

            private void button2_Click(object sender, EventArgs e)
            {
                textBox1.Text = dataGridView1.CurrentRow.Index.ToString();
            }

    Hope this can help you Smile
     







    Wednesday, November 14, 2007 10:55 AM
  • DataGridView1.SelectedRows(0).Index.ToString

     

    Sunday, February 03, 2008 8:55 PM
  • This also seems to work.

    DGV.CurrentRow.Index.ToString()

    Thursday, August 07, 2008 9:50 PM
  • Hello Lars,

    Well, first of all, there is no "SelectedRow", but there is "SelectedRows"!
    To get the index of one of the SelectedRows, you write DataGridView1.SelectedRows(i).Index
    where i is which one of the selected rows you are referring to.
    In our case, however, we got only one selected row, so you just have to get the index of the first row of those selected.
    So you just put DataGridView1.SelectedRows(0).Index

    I hope this helps!
    Tuesday, August 12, 2008 4:32 PM
  • This works for me.  DataGridView.CurrentCell.RowIdex.  I hope this helps.
    Monday, March 09, 2009 8:37 PM
  • hi ?

    you can use this code

    int x=int.prase(this.datagridview.rows[e.RowIndex].cell[e.columnIndex].ToString());

    but in the first you must change the datagridviewRow properties to full select row

    now x will return the index of the datagridview SelectRow

    Thursday, August 19, 2010 6:35 PM
  • Hi Lars E.NEs

     

    You can try DataGridview1.Rows[e.RowIndex].Cells[0];

    Thursday, January 06, 2011 5:44 AM
  • try to increment the index value to get the filnaly number

     

     

    or you can count manualy

     

    1. if(grid1.SelectedRows.Count > 0)
    2.      textBox1.Text = grid1.SelectedRows[0].Cell[1].Value.ToString();

    Banus PRO Founder, CEO banus_pro@hotmail.com Tech Server Lda
    Wednesday, April 27, 2011 10:01 PM
  • This works for me:

    int rowIndex = <dataGridView>.CurrentRow.Index;

    Where <dataGridView> is the name of your DataGridView. Then any row a user selects data from can be referenced by the int rowIndex.

    All the best

    Chris


    To fix it or not to fix it, that is the question
    • Proposed as answer by Microfarad Monday, October 31, 2011 8:48 AM
    Monday, October 31, 2011 8:48 AM
  • hi guys dont answer to question. if you have not a opinion....
    Friday, March 23, 2012 1:02 PM