How to populate DataSet from List Box?

  • Thread starter Thread starter bpl via DotNetMonster.com
  • Start date Start date
B

bpl via DotNetMonster.com

I want to store the result sets for the 10 drop-down list boxes in a DataSet
object. How I can do it?
 
Well, that's a pretty generic question but I think I can guide you in the
right direction. The reason I say it's generic is b/c Loading 10 Listbox's
worth of data can mean a lot of things. Are the tables related in anyway?
Do they each all have to go into tables independently? Is this a typed
DataSet?

Anyway, I would recursively poll the form (not sure if this is winforms or
web but the approach is similar)... Ok,
DataSet DataSetName = new DataSet("whatevername");
foreach(Control ctrl in myForms.Controls){
if(typeof(ctrl) is a listox{
DataTable dt = new DataTable(ctrl.Name);
DataColumn dc = new DataColumn("WhateverName");
dt.Columns.Add(dc);
for(int i = 0; i < ctrl.Items.Count; i++){
DataRow dro = dt.NewRow();
dro[0] = ctrl;// (Not sure if you want the Display or Value
obviously these are different properties, and they are different in Winforms
and WebForms
dt.Rows.Add(dro);
}
DataSetName.Tables.Add(dt);
}
}

Ok, first off, there's a bunch of optimizations I'd do with this code in
Real life but this should give you the basic idea. I may have a shortcut
for you though.. I'll post right back if i can get it to work (I think you
can use Datatable.Rows.Add(Object values) where values is an array you get
from each listbox. But looping through each will Definitely work. I'll
post back in a minute.
 
As a follow up, I played around with it and again, depending on what you
meant..

object[] o = new object[lb1.Items.Count];

lb1.Items.CopyTo(o,0);

DataTable dt = new DataTable("CuckooTable");

DataColumn dc = new DataColumn("CuckooColumn");

DataColumn dc2 = new DataColumn("CuckooColumn2");

dt.Columns.Add(dc);

dt.Columns.Add(dc2);

dt.Rows.Add(o);



However, this is another approach you can use if you have multiple columns.
Tell me a little more about what you need and I'll see what I can do:



private void GrabControls(Control ctrl)

{

foreach(Control ctrl in this.Controls)

{

if(ctrl.HasChildren)

{

GrabControls(ctrl);

}

else

{

if(typeof(ctrl) is ListBox)

{

object[] o = new object[(ctrl as ListBox).Items.Count];

lb1.Items.CopyTo(o,0);

DataTable dt = new DataTable("CuckooTable");//This needs to change each time

//If you want to add it to a dataset. Plus, you can just create a table

//and not have to add the columns at each pass, but if you're

//using a typed dataset than you don't need to do this at all.

DataColumn dc = new DataColumn("CuckooColumn");

DataColumn dc2 = new DataColumn("CuckooColumn2");

dt.Columns.Add(dc);

dt.Columns.Add(dc2);

dt.Rows.Add(o);

}

}

}

}
 
BPL,
I want to store the result sets for the 10 drop-down list boxes in a
DataSet
object. How I can do it?

If you mean with this that the current selected (by single selected listbox)
values in 10 boxes have to be put in a database, than it is just creating a
"NewRow" from your datatable and fill the items conform the way you have
told that in your datacolumns exist.

When that is done you can add the datarow to your DataTable.

I hope this helps,

Cor
 
Back
Top