filtering records a control binds to

M

MattB

I have a RadioButtonList then binds to a DataTable in my Web Form. The
DataTextField is called "descrip" and I want to bind only records that don;t
have an empty descrip field.

My initial approach was to loop through the rows collection, test each row's
descrip field and if it's empty delete it. That didn;t work because I guess
you can't delete from a collection you are currently looping on (makes
sense).

I've got some other ideas, but before I get too far, is there an established
"best way" to do this? I'm working on looping through my table and just
creating an array list of each row with a blank descrip, and then looping
through that array list and deleting rows listed there. This kind of works,
but seems to end up a row off. I wish I could just "delete from mytable
where trim(descrip) = ''". Is there a better way to do this? Thanks!

Matt
 
K

Karl

MattB:
You were probably using a foreach, I believe you can delete in a for i loop.

Anyways, the way I would recommend is to create a DataView from the
datatable, add the appropriate filter and bind to that

Dim dv as DataView = myDataTable.DefaultView
dv.RowFilter = "descrip <> ''" 'those are two single quotes which
represents an empty string

radList.Datasource = dv
radList.DataBind

Karl
 
G

Guest

Matt,

Just use the RowFilter property of the DataTable's DefaultView to filter out
the rows which have empty "descrip" fields.
Then get your RadioButtonList to bind to the DefaultView - Job done!

Example:
DataSet ds = GetDataForDataSet();
ds.Table[0].DefaultView.RowFilter = "descrip <> ''";
radioButtonList.DataSource = ds.Table[0].DefaultView;

Hope this helps,
Mark
 
M

MattB

Thanks to you both. Perfect!

Mark said:
Matt,

Just use the RowFilter property of the DataTable's DefaultView to
filter out the rows which have empty "descrip" fields.
Then get your RadioButtonList to bind to the DefaultView - Job done!

Example:
DataSet ds = GetDataForDataSet();
ds.Table[0].DefaultView.RowFilter = "descrip <> ''";
radioButtonList.DataSource = ds.Table[0].DefaultView;

Hope this helps,
Mark

MattB said:
I have a RadioButtonList then binds to a DataTable in my Web Form.
The DataTextField is called "descrip" and I want to bind only
records that don;t have an empty descrip field.

My initial approach was to loop through the rows collection, test
each row's descrip field and if it's empty delete it. That didn;t
work because I guess you can't delete from a collection you are
currently looping on (makes sense).

I've got some other ideas, but before I get too far, is there an
established "best way" to do this? I'm working on looping through my
table and just creating an array list of each row with a blank
descrip, and then looping through that array list and deleting rows
listed there. This kind of works, but seems to end up a row off. I
wish I could just "delete from mytable where trim(descrip) = ''". Is
there a better way to do this? Thanks!

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top