Solved

DataGrid SelectedIndex

Posted on 2006-06-20
3
358 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

770 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