Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Non-invocable member 'System.Windows.Forms.DataGridView.Rows' cannot be used like a method.

what is the solution for my error?

private void Form1_Load(object sender, EventArgs e)
{
    dataGridView1.Rows.Add(5);
}

private void dataGridView1_CellContentClick(object sender, 
    DataGridViewCellEventArgs e)
{
}
private void dataGridView1_MouseDown(object sender, 
    System.Windows.Forms.DataGridViewCellMouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        foreach (DataGridViewRow row in dataGridView1.SelectedRows)
        {
            row.Selected = false;
        }
        dataGridView1.Rows(e.RowIndex).Selected = true;
    }
}


dataGridView1.Rows can't be running
Posted
Updated 2-Jan-17 22:30pm
v2
Comments
ProEnggSoft 23-Mar-12 12:29pm    
Edit: Indentation adjusted - PEs

In C# to access an element of Array or Indexed property, [ ] brackets are to be used. So set the Selected property as follows

C#
dataGridView1.Rows[e.RowIndex].Selected = true;


The ( ) parentheses are used to invoke a method. Since, Rows is a collection the above error is thrown, as it is not a method to be invoked with ( ) parentheses.
 
Share this answer
 
v3
Comments
Steve Maier 23-Mar-12 12:22pm    
You beat me to the answer. You get a 5 for speed. ;-)
ProEnggSoft 23-Mar-12 12:26pm    
Thank you
[no name] 23-Mar-12 13:25pm    
thanks
Sergey Alexandrovich Kryukov 23-Mar-12 21:46pm    
Exactly, a 5.
--SA
ProEnggSoft 23-Mar-12 21:50pm    
Thank you.
The Rows property is a DataGridViewRowCollection type that inherits from IList and IEnumerable. To get to one of the items in the like you can use square brackets.
dataGridView1.Rows[e.RowIndex].Selected = true;
 
Share this answer
 
Comments
ProEnggSoft 23-Mar-12 12:26pm    
My 5 for instant reply to the point.
[no name] 23-Mar-12 13:26pm    
thanks
[no name] 23-Mar-12 13:31pm    
still error in dataGridView1.Rows
Steve Maier 23-Mar-12 13:35pm    
The same error? Or a different one? It should not be the same error if you switched to []
[no name] 23-Mar-12 13:39pm    
oohh yeahh... it's success

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900