Solved

DataGrid SelectedIndex

Posted on 2006-06-20
3
343 Views
Last Modified: 2009-12-16
I have an application which makes use of a datagrid.  My problem is that if a row in the datagrid is selected, and I then programmatically select a row, I end up with 2 rows selected.  If I continue selecting other rows programmatically, they get selected, but the other rows do not deselect.  I was under the impression that there was a property called SelectedIndex that I could set to -1 to clear my row selections....but I cannot find that property anywhere for a datagrid within Visual Studio.  The object browser does not even see it within the datagrid class.  How can I accomplish this???????????  Basically to only allow one row selection at a time, whether it be UI selection or programmatically???????????

Thanks,
Kendal
0
Comment
Question by:gvector1
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 500 total points
ID: 16943074
Hi, try using: the CurrentRowIndex property:

this.dataGrid1.CurrentRowIndex = <index here>;

if your already using that and its still selecting more than one row, try the following:

this.dataGrid1.UnSelect(this.dataGrid1.CurrentRowIndex); //Unselect the currently selected row
this.dataGrid1.Select(<new index>); //Select the new one



0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16943308
Sorry, just noticed you mentioned multiple rows maybe selected, in that case you could iterate through the rows and deselect each one before selecting the new, you can use the following method (subtitute dataGrid1 with your datagrids name):

private void SelectSingleRow(int index)
{
      int rowCount = ((CurrencyManager) this.dataGrid1.BindingContext[this.dataGrid1.DataSource, this.dataGrid1.DataMember]).Count;

      for (int i = 0; i < rowCount; i++)
      {
             if (this.dataGrid1.IsSelected(i))
                   this.dataGrid1.UnSelect(i);
      }

      this.dataGrid1.Select(index);
      this.dataGrid1.CurrentRowIndex = index;
}
0
 

Author Comment

by:gvector1
ID: 16943350
Thanks Zephyr!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now