views:

25

answers:

1

i have a datable and like this i have searched a datarow from the datable on the basis of some primary now i want to add that searched row to another datatable how can i achieve this please let me know

 DataTable findRows = (DataTable)ViewState["dt"];
 List<int> selectedList=(List<int>)ViewState["selectedList"];
 DataTable temp = new DataTable();

 foreach (int id in selectedList)
 {
   DataRow dr=findRows.Rows.Find(id);

 }

now i want it to add to datatable temp how can i achieve this?

+4  A: 

First, when creating temp don't just instantiate it as a new DataTable but instead call .Clone() on findrows to create a structurally identical DataTable.

Second, use .ImportRow() on the second DataTable and pass it the row from the first DataTable that you'd like to copy. This should create an entirely new row in the second table with the same values as the row from the first table.

David
would u please code a bit ...i am not getting what u say
NoviceToDotNet
DataTable findRows = (DataTable)ViewState["dt"]; List<int> selectedList=(List<int>)ViewState["selectedList"]; DataTable temp = findRows.Clone(); foreach (int id in selectedList) { DataRow dr=findRows.Rows.Find(id); temp.ImportRow(dr); }
NoviceToDotNet
That looks to be about right, ya. Sorry, I've been commuting for the past hour or more (picking up kids, etc.) so I didn't see these comments until now :)
David