?
Solved

DataGrid SelectedIndex

Posted on 2006-06-20
3
Medium Priority
?
382 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

765 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