dataGrid confusion

G

Guy Noir

I am having trouble wrapping my head around a datagrid and datasource.
I have it working, sort-of, but I'm having trouble refreshing my views.
Here's what I have:

A dataAdapter and dataSet generated by the wizards.

I click my Orders button, grid displays as expected.

I click my drop down box to filter out some specific records, every
thing works as expected.

When I click my Orders button again, it simply shows whatever the last
filter displayed. I've tried every little hack I can think of. Clearly,
I don;t have a good understanging of the C# datagrid yet. Any help is
GREATLY appriciated. Below is my code. Thanks in Advance!!!


private void btnOrders_Click(object sender, System.EventArgs e)
{
lblHeading.Text = "ORDERS: Supplier Num Part Num Quantity";

if (conn.State != ConnectionState.Open)
conn.Open();

// Set the datasource to the dataset
dataGrid.DataSource = dataSetSP.SP;
dataAdapterSP.Fill(dataSetSP);

// Populate the ComboBox
dataAdapterS.Fill(dataSetS);
}

private void cmbSupplier_SelectedIndexChanged(object sender,
System.EventArgs e)
{
if (conn.State != ConnectionState.Open)
conn.Open();

DataView dv = dataSetSP.Tables["SP"].DefaultView;
string filter = "`S#` = '" + cmbSupplier.SelectedValue + "'";
dv.RowFilter = filter;
dataGrid.DataSource = dv;

}
 
B

Bart Mermuys

Hi,
[Inline]

Guy Noir said:
I am having trouble wrapping my head around a datagrid and datasource.
I have it working, sort-of, but I'm having trouble refreshing my views.
Here's what I have:

A dataAdapter and dataSet generated by the wizards.

I click my Orders button, grid displays as expected.

I click my drop down box to filter out some specific records, every
thing works as expected.

When I click my Orders button again, it simply shows whatever the last
filter displayed. I've tried every little hack I can think of. Clearly,
I don;t have a good understanging of the C# datagrid yet. Any help is
GREATLY appriciated. Below is my code. Thanks in Advance!!!

There are some variations to solve this, here's one:

Even if you bind to a DataSet/DataTable the grid is always bound to a
DataView, it isn't DefaultView, but you can get the right DataView using the
CurrencyManager for the table :

/*
Make sure you bound the datagrid in the designer (or from code: eg.onload):
DataSource = dataSetSP;
DataMember = "SP";
*/

private void btnOrders_Click(object sender, System.EventArgs e)
{
lblHeading.Text = "ORDERS: Supplier Num Part Num Quantity";

if (conn.State != ConnectionState.Open)
conn.Open();

// refresh/ populate grid
dataSetSP.Clear();
dataAdapterSP.Fill(dataSetSP);

// refresh/ populate ComboBox
dataSetS.Clear();
dataAdapterS.Fill(dataSetS);

// instead of re-setting the datasource here, clear the rowfilter
DataView dv = (DataView)((CurrencyManager)BindingContext[dataSetSP,
"SP"]).List;
dv.RowFilter = "";

con.Close();
}

private void cmbSupplier_SelectedIndexChanged(object sender,
System.EventArgs e)
{
DataView dv = (DataView)((CurrencyManager)BindingContext[dataSetSP,
"SP"]).List;
string filter = "`S#` = '" + cmbSupplier.SelectedValue + "'";
dv.RowFilter = filter;
}

--- Another way though similar, is to bind the grid to the table's
DefaultView from the beginning (onload) and never change the binding, but
only change/clear its RowFilter.


HTH,
Greetings
 

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