PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 5.00 average.

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

 
 
Diamonds
Guest
Posts: n/a
 
      26th Jun 2006
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?

 
Reply With Quote
 
 
 
 
Diamonds
Guest
Posts: n/a
 
      26th Jun 2006
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,

Diamonds wrote:
> 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?


 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      27th Jun 2006
Diamonds,

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

dr = drv.Row;

I hope this helps,

Cor

"Diamonds" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
> 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?
>



 
Reply With Quote
 
Diamonds
Guest
Posts: n/a
 
      28th Jun 2006
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,

Cor Ligthert [MVP] wrote:
> Diamonds,
>
> To get datarow from the current datarowview (the rows from a dataview) is
> very simple.
>
> dr = drv.Row;
>
> I hope this helps,
>
> Cor
>
> "Diamonds" <(E-Mail Removed)> schreef in bericht
> news:(E-Mail Removed)...
> > 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?
> >


 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      28th Jun 2006
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

"Diamonds" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
> 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,
>
> Cor Ligthert [MVP] wrote:
>> Diamonds,
>>
>> To get datarow from the current datarowview (the rows from a dataview) is
>> very simple.
>>
>> dr = drv.Row;
>>
>> I hope this helps,
>>
>> Cor
>>
>> "Diamonds" <(E-Mail Removed)> schreef in bericht
>> news:(E-Mail Removed)...
>> > 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?
>> >

>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataView row's index in DataTable? NorCan Microsoft ADO .NET 1 13th Sep 2005 03:19 PM
How to assign the changed datatable in dataview back to a datatable? TaeHo Yoo Microsoft C# .NET 1 8th Jul 2005 04:41 AM
Sorting datagrid-dataview-datatable =?Utf-8?B?c3ViVA==?= Microsoft ADO .NET 2 27th May 2004 03:56 PM
Does DataView(DataTable) constructor just return DataTable.DefaultView? Neil Price Microsoft ADO .NET 1 22nd Sep 2003 12:05 PM
Re: Correct DataTable index from sorted DataView... Nathan Baulch Microsoft ASP .NET 0 26th Jun 2003 01:16 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:18 PM.