DataGrid and Selected Rows

C

Chris Dunaway

I am writing a simple Windows Forms Application that accesses an Access
database. In the form I instantiate a DataTable object. I then read the
contents of a single table in the Access file into this DataTable. This
works as expected.

Next, I instantiate another DataTable and use the .Clone method on the
first to duplicate the structure into this new datatable. The user then
type a search value into a textbox. I then search the first datatable and
if the row is found, I copy it to the second datatable. This all works
fine.

Finally, I have a DataGrid on the form which is bound to the *second*
datatable, the one with the found rows. The DataGrid shows the rows that
were found. This part is working.

Now my question(s) (I'll bet you're glad I'm finally getting to the
point!!):

1. If the user adds a row to the second table by mistake, I want them to
be able to highlight it and delete it from the DataGrid as well as from the
second table. The DataGrid is ReadOnly. I can do this if only a single
row is selected, but how to do it if more than one row is selected? The
CurrentRowIndex property only seems to show a single row index regardless
of the number of indices actually selected.

2. Is there any way to turn multi select off on the grid?

3. How can I iterate through the rows of the grid and see which ones are
selected?

Keep in mind that the DataGrid is not bound to the Access database, it is
bound to a table that only has a few rows that were copied from another
table. My aim here is NOT to change the Access database. I am only using
the second DataTable as a tempory collection of rows.
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hello Chris,
1. If the user adds a row to the second table by mistake, I want them to
be able to highlight it and delete it from the DataGrid as well as from the
second table. The DataGrid is ReadOnly. I can do this if only a single
row is selected, but how to do it if more than one row is selected? The

You should test each row in the grid with the grid's IsSelected method.
Unfortunately, no SelectedRows collection or something similar is exposed.
2. Is there any way to turn multi select off on the grid?

There might be a simplier way, but what I can suggest off the top of
my head is inheriting from the DataGrid, overriding the OnMouseDown
and OnMouseUp methods and preventing the user from selecting more
then one row.
3. How can I iterate through the rows of the grid and see which ones are
selected?


See my answer on question #1.
 
C

Chris Dunaway

See my answer on question #1.

Thanks for the response.

After I posted, I slapped myself in the head because the answer came to me.
Since the grid always reflects the rows in the temporary table, it is a one
to one correlation.

I can just iterate through the rows of the table itself and then refresh
the grid after using the IsSelected method. Duh!!!

I should have thought of that sooner, but I was missing the forest for the
trees!

As for the multi-select, its not an issue for me.

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top