filtering records a control binds to

  • Thread starter Thread starter MattB
  • Start date Start date
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
 
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
 
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
 
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
 
Back
Top