By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,414 Members | 2,903 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,414 IT Pros & Developers. It's quick & easy.

How to refesh a listbox bind to a dataview?

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

"Alpha" <Al***@discussions.microsoft.com> schreef in bericht
news:6E**********************************@microsof t.com...
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

Nov 17 '05 #2

P: n/a
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

"Cor Ligthert [MVP]" wrote:
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

"Alpha" <Al***@discussions.microsoft.com> schreef in bericht
news:6E**********************************@microsof t.com...
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


Nov 17 '05 #3

P: n/a
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
Nov 17 '05 #4

P: n/a
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();

"Cor Ligthert [MVP]" wrote:
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

Nov 17 '05 #5

P: n/a
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_SelectedIndexCh anged);
}
private void Combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
((DataView)listBox1.DataSource).RowFilter =
"ListId = '" + comboBox1.SelectedValue +"'";
}
///

I hope this helps,

Cor
Nov 17 '05 #6

P: n/a
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!

"Cor Ligthert [MVP]" wrote:
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_SelectedIndexCh anged);
}
private void Combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
((DataView)listBox1.DataSource).RowFilter =
"ListId = '" + comboBox1.SelectedValue +"'";
}
///

I hope this helps,

Cor

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.