Click here to Skip to main content
14,636,152 members
Rate this:
Please Sign up or sign in to vote.
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
 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 ...

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 headlessnick 19-Aug-11 14:23pm
   
Thanks to everybody for help...
Rate this:
Please Sign up or sign in to vote.

Solution 2

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();
   
Comments
the headlessnick 20-Aug-11 13:18pm
   
Thanks Man
but as I said it finally occurred to me...!!


Thanks a lot anyways
Rate this:
Please Sign up or sign in to vote.

Solution 1

Try

ddss.Tables.Add(dt);


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

Change

DataSet ddss;


to

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.
   
v2
Comments
the headlessnick 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 headlessnick 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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100