none
selected row index

    Question

  • i am trying to get selected row index but i got error

     private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                MessageBox.Show(dataGridView1.SelectedRows[0].Index.ToString());
            }



    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index


    whats wrong. i try on other events like mouse click etc every time i got this error why this error occur
    • Moved by Naomi N Tuesday, September 10, 2013 1:38 AM May be better answer here
    Sunday, September 08, 2013 5:55 AM

All replies

  • Try this

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if(dataGridView1.SelectedRows != 0)
    		MessageBox.Show(dataGridView1.SelectedRows[0].Index.ToString());
    	    else
    		MessageBox.Show("No Rows Selected");
            }

    Sunday, September 08, 2013 6:16 AM
  • error Error  

      if(dataGridView1.SelectedRows != 0)

     1    Property or indexer 'System.Windows.Forms.DataGridView.SelectedRows' cannot be assigned to -- it is read only  


    Sunday, September 08, 2013 6:25 AM
  •   if(dataGridView1.SelectedRows != 0)

     1    Property or indexer 'System.Windows.Forms.DataGridView.SelectedRows' cannot be assigned to -- it is read only  

    Try:

    if(this.dataGridView1.SelectedRows.Count != 0)

    This question doesn't seem be related to Transact-SQL so you might post to a more appropriate forum if you need additional help.


    Dan Guzman, SQL Server MVP, http://www.dbdelta.com

    Sunday, September 08, 2013 1:11 PM
  • Hi,

    First, I want to inform that this forum is to discuss SQL Server questions, regarding the question about how to check the selected index of DataGridView control, we can post the question to Windows Forms forum. Additionally, you can refer to the following codes:

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                MessageBox.Show(e.RowIndex.ToString());
            }

    For more detail information, you can refer to the following link:

    Windows Forms forum
    http://social.msdn.microsoft.com/Forums/windows/en-US/home?forum=winformsdatacontrols


    Allen Li
    TechNet Community Support

    Tuesday, September 10, 2013 1:29 AM