DataTable -> DataView -> Sort -> DataGrid : need related DataTable index

D

Diamonds

Hello,

In my DataGrid for ASP.NET C#,

In the UI the user adds rows to the DataGrid and the values are saved
in a DataTable.

The DataTable is displayed by using a DataView. The DataView is sorted
then bound to the DataGrid.

The DataGrid allows editing. It knows which row to change based on
Item.ItemIndex. However, it is using the ItemIndex of the DataView. I
need to map the DataView index to the DataTable index to edit the
correct row.

The sort is = "Col1, Col2 ASC".


Am I doing this correctly?
How would I get the related DataTable row?
 
D

Diamonds

I came up with a solution but it seems that I am doing it wrong.

My DataTable now has a column called "PK" for primary key which it
increments on its own each time a value is added. In the DataGrid I
have a hidden Column which has a label with a text value of the PK
value.

On a delete the following code is executed:

Label PK = (Label) e.Item.FindControl("PK");
DataRow[] dr = Seats.Select("[Primary Key] = '"+PK.Text+"'");
if (dr.Length > 0)
Seats.Rows.Remove(dr[0]);

Please let me know if there is a more elegant solution to this. Perhaps
assigning the PK value the correlated DataTable index, instead of a
textual representation that I have to 'Select'.

Cheers,
 
C

Cor Ligthert [MVP]

Diamonds,

To get datarow from the current datarowview (the rows from a dataview) is
very simple.

dr = drv.Row;

I hope this helps,

Cor
 
D

Diamonds

So I could use dr to .Remove() from the datatable?
Even though the datarow came from the view, they are still the same
reference?

Thnx for your help. I'm really worreid that I'm doing this the long
way.

Cheers,
 
C

Cor Ligthert [MVP]

Diamonds,

This you did not ask. But don't use the remove if you wants to update a
database later.

A "remove" removes a row from a datatable, so the update does not know
anything anymore from that row.

A "delete" set a rowstate, so that the update can use that to delete the row
from the database.

(This rowstate is not set as the row was new inserted).

The rowstates are cleaned and the rows really removed by the dataset or by
the command. Datatable.acceptchanges.

I hope this helps,

Cor
 

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