How to refesh a listbox bind to a dataview?

G

Guest

I have a listbox with datasource from a dataview. When a user selects a
different item in a combobox then I need to refresh the listbox to the
appropriate listing based on that combobox's selected value which is included
in the listbox's filtering statement.

Is the only way to do this is to dispose the dataview and then create a new
one and then bind it to the listbox? Is there a better way than this?

Thanks, Alpha
 
C

Cor Ligthert [MVP]

Alpha,

Is the datasource of your listbox a "new DataView" or is it the DefaultView
of a table.

If I understand you right is it the first, therefore try to use that
DefaultView.

I hope this helps,

Cor
 
G

Guest

It's not a defaultview. The dataview is created at the loading of the form.
It has a filter value that is determined by the user's selection of the
comboBox above it. I need to refresh the listbox to reflect the proper
listing according to what is selected in the comboBox.

Both comboBox and the listbox dataview are getting their data from the same
dataset but different table. The dataview is used to filter the table
information to the corrsponding selection in comboBox.

Is there a way to do this with dataview? I guess I could always use the the
dataset.table as the datasource for the listbox and then clear and fill each
time the new selection is made with the comboBox. I just thought that's not
very efficient way to do it and would like to see if I can do this with
dataview.

Thanks, Alpha
 
C

Cor Ligthert [MVP]

Alpha,

Normally you can do that using the indexchange event of the first control to
set the dataview.rowfilter of the second one with the text or the
selectedvalue.

While you can as well use the currencymanager change event for that.

For a combobox or listbox I normally use the first. Be aware that you set
the indexchange handler after that you have set the datasource, the
displaymember and eventualy the valuemember of the combobox.

I hope this helps,

Cor
 
G

Guest

Hi Cor,

I already coded before the following in the cmbScheudle_SelectedIndexChanged
event but the listbox still doesn't reflect the new listing according to the
new id selected in the comboBox. Can you tell what I'm doing wrong?

int SelSch = Convert.ToInt32(cmbScheudle.SelectedValue.ToString());
strScheudleID =
dsSchItems.Tables["Schedule"].Rows[SelSch]["SchID"].ToString();

dvSchItems.Dispose();
DataView dvNewSchItems = new DataView();
int SelSch = Convert.ToInt32(cmbScheudle.SelectedValue.ToString());
strScheudleID =
dsSchItems.Tables["Schedule"].Rows[SelSch]["SchID"].ToString();
dvNewSchItems.Table = tblSchItems;
dvNewSchItems.RowFilter = FilterExp + strScheudleID;
dvNewSchItems.Sort = SortExp;

lstSchItem.DataSource = dvNewSchItems;
lstSchItem.DisplayMember = "SchItemCode";
lstSchItem.ValueMember = "SchItemID";

lstSchItem.Refresh();
 
C

Cor Ligthert [MVP]

Alpha,

If you use a new project, drag on that a combobox and a listbox and than
past in this code, than you will see how it goes (you have to make the load
event as well)

\\\
private void Form1_Load(object sender, EventArgs e)
{
//building of sample tables
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
dt1.Columns.Add("Id",Type.GetType("System.String"));
dt1.Columns.Add("ListID", Type.GetType("System.String"));
dt1.LoadDataRow(new Object[] { 1,"Field1" }, true);
dt1.LoadDataRow(new Object[] { 2, "" }, true);
dt1.LoadDataRow(new Object[] { 3, "Field3" }, true);
dt2.Columns.Add("ListId", Type.GetType("System.String"));
dt2.Columns.Add("Show", Type.GetType("System.String"));
dt2.LoadDataRow(new Object[] { "Field1", "The Fields one" }, true);
dt2.LoadDataRow(new Object[] { "Field3", "Another Field" }, true);
dt2.LoadDataRow(new Object[] { "Field3", "Again Antoher Field" },
true);
//end building tables

//start sample
comboBox1.DataSource = dt1;
comboBox1.DisplayMember = "Id";
comboBox1.ValueMember = "ListId";
dt2.DefaultView.RowFilter = "ListId = '" + dt1.Rows[0][1] + "'";
listBox1.DataSource = dt2.DefaultView;
listBox1.DisplayMember = "Show";
listBox1.ValueMember = "ListId";
comboBox1.SelectedIndexChanged +=
new System.EventHandler(this.Combobox1_SelectedIndexChanged);
}
private void Combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
((DataView)listBox1.DataSource).RowFilter =
"ListId = '" + comboBox1.SelectedValue +"'";
}
///

I hope this helps,

Cor
 
G

Guest

Sorry it took me so long to respond. I was pull away to take care of
something else. My earlier error was trying to just setting the rowfilter of
the dataview and that didn't work. I used the code you have at the end which
tied in the control and dataview and it worked. Thank you very much!
 

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

Top