Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid: How to achieve multiple selection in a windows application?

Status
Not open for further replies.

mthakershi

Programmer
Aug 24, 2001
79
US
Hi,

I am trying to achieve multiple selection like the following in my datagrid in a windows application.

Select Col1 Col2
Val1 Val2
Val3 Val4

This could be done by having a check mark button under the select column or simply by retrieving currently selected rows by some property on the datagrid. I don't know any of that. Most help available seems to be for web forms.

Here are some of the details about my application.
1. Datagrid is bound.
2. Datagrid is readonly.
3. I get data in a dataset and assign it as a datasource of the grid.
4. It's a C# application.
5. Backend is MS Access.

Thanks for any help in advance.

Malay Thakershi
 
Use the IsSelected method to determine which rows are selected. Unfortunately, you have to loop through all of the rows to get a full list from your DataSource.

Users must select multiple rows by ctrl-clicking on the row headings.

For example:
Code:
DataGrid grd;
DataTable tbl;

grd.DataSource = tbl;

//You must use the default view here because some rows
//might get deleted and the table sort order may not match
//the grid sort order
for (int intRow = 0; intRow < tbl.DefaultView.Count; intRow++) {
   if (grd.IsSelected(intRow)) {
      DataRow row = tbl.DefaultView[intRow].Row;

      //Do whatever it is you need to with the row
   }
}
 
Hi dalchri,

Thanks for your help.

Malay Thakershi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top