Solved

DataGrid SelectedIndex

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Suggested Courses

732 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