This is a migrated thread and some comments may be shown as answers.

Not select first row (CurrentRow, index out of range)

3 Answers 136 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 08 Jan 2013, 07:42 AM
Hi all,
Ive read on the forums that if I dont want the first row in my gridview to be selected whenever I bind to a datasource, i could set the "CurrentRow" to nothing.

I use version: 2012.3.1211.20

When I try this however, I get an "Index was out of range. Must be non-negative and less than the size of the collection"

I have binded my grid to a list of my custom object.
Then ive tried the following:

gridview1.CurrentRow = nothing
and
gridview1.ClearSelection()

Both commands result in the "Index out of range"

Anybody else experienced the same problem?

Thanks.




3 Answers, 1 is accepted

Sort by
0
Accepted
konrad
Top achievements
Rank 1
answered on 09 Jan 2013, 07:22 AM
Hi,

Just after binding grid simple set CurrentRow = null.
If this will not work, that mean something is wrong with rest of yours codes (events).

DataSet ds = new DataSet();
ds.Tables.Add("table");
ds.Tables[0].Columns.Add("a");
ds.Tables[0].Columns.Add("b");
 
DataRow dr = ds.Tables[0].NewRow();
dr[0] = "row 1 col1";
dr[1] = "row 1 col2";
ds.Tables[0].Rows.Add(dr);
 
DataRow dr2 = ds.Tables[0].NewRow();
dr2[0] = "row 2 col1";
dr2[1] = "row 2 col2";
ds.Tables[0].Rows.Add(dr2);
 
grid.DataSource = ds.Tables[0];
 
grid.CurrentRow = null;
 
Console.WriteLine(grid.SelectedRows.Count);
// Output: 0
0
Thomas
Top achievements
Rank 1
answered on 09 Jan 2013, 07:43 AM
Thanks for your answer.
I never thought about my datasource as the problem as it works for everything ive done so far, but just not "CurrentRow".
When i try to make an example like yours, its works though.

So thanks, ill look into my object :)
0
Plamen
Telerik team
answered on 10 Jan 2013, 04:29 PM
Hi guys,

@Dominik. Thank you for your cooperation.

@Thomas. Indeed, if you want to clean the current row, you could set the CurrentRow property to null, as already suggested by Dominik:
this.radGridView1.CurrentRow = null;

More information about CurrentRow and selected rows you can find in our documentation.

As to the issue with the "Index out of range" exception, I did not manage to reproduce it by following the supplied information. Everything is working as expected on my end. Here is the sample code, which I am using:

void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
{
    this.radGridView1.ClearSelection();
    radGridView1.CurrentCell.IsSelected = false;
    radGridView1.CurrentRow = null;
}

If you continue to experience this issue, please open a support ticket and send me your application. I will try to locate the issue and find a proper solution.

Greetings,
Plamen
the Telerik team
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
Tags
GridView
Asked by
Thomas
Top achievements
Rank 1
Answers by
konrad
Top achievements
Rank 1
Thomas
Top achievements
Rank 1
Plamen
Telerik team
Share this question
or