Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DataSet that has only one row, and I'm in the middle of a gridView. Now I have to bind a DataList with some of the DataSet Rows that will be generated from the gridView Rows.

I have used a DataTable like dis
C#
 DataTable dt = new DataTable();
foreach (GridViewRow row in GridView1.Rows)
{
// Some Lines Of Code
DataRow dr = ds.Tables[0].Rows[0];
dt.ImportRow(dr);
}
//After the foreach loop

DataSet ddss;
ddss.Tables[0] = dt;  //IS THIS RIGHT or IS THERE ANY OTHER WAY...???

DataList1.DataSource=dt;
DataList1.DataBind();



I don't know whether I explained it right. Just do le'me know.




>>>><<<<<<SORRYY GUYS>>>>>><<<<<<

Actually I missed to mention that the actual error I was getting in the
"Can't Find The SPECIFIC Columns Name"


And To resolve that I used ...

C#
dt.Columns.Add("abc", Type.GetType("System.String"));
dt.Columns.Add("def", Type.GetType("System.String"));
dt.Columns.Add("ghi", Type.GetType("System.String"));
// AND SO ON
Posted
Updated 19-Aug-11 8:22am
v2
Comments
the headless nick 19-Aug-11 14:23pm    
Thanks to everybody for help...

You can try this.



DataTable dt = new DataTable();
dt.Columns.Add("abc", Type.GetType("System.String"));
dt.Columns.Add("def", Type.GetType("System.String"));
dt.Columns.Add("ghi", Type.GetType("System.String"));

foreach (GridViewRow row in GridView1.Rows)
{
DataRow dr = ds.Tables[0].Rows[0];
dt.ImportRow(dr);
}

DataSet ddss = new DataSet();
ddss.Tables.Add(dt);

DataList1.DataSource = dt;
DataList1.DataBind();
 
Share this answer
 
Comments
the headless nick 20-Aug-11 13:18pm    
Thanks Man
but as I said it finally occurred to me...!!


Thanks a lot anyways
Try

C#
ddss.Tables.Add(dt);


Using "Add" will work, you just don't have an actual dataset created yet.

Change

C#
DataSet ddss;


to

C#
DataSet ddss = new DataSet();


Without setting ddss to a new dataset (or one that's previously created), there's nothing to add the datatable to.
 
Share this answer
 
v2
Comments
the headless nick 19-Aug-11 12:07pm    
Didn't Work ... Object reference not set to an instance of an object.
Bert Mitton 19-Aug-11 13:37pm    
See above solution... it's been expanded to fix another issue with the code.
the headless nick 20-Aug-11 13:20pm    
yup, :)) I have solved my prob.
Thanks to ME and yes of course you too...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900