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
Solved

DataGrid SelectedIndex

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Device Location 4 35
Hey Audio! 9 44
converto datetime always changes format 9 41
What can cause the styling on a .NET site to not be found? 17 29
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

860 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