DefaultDataView does not work?

  • Thread starter Thread starter Bill Todd
  • Start date Start date
B

Bill Todd

I have a simple Web app using the Northwind database. I have a typed
dataset that contains the Suppliers and Products tables. I need to
sort and filter the Products table. For reasons that are irrelevant to
this problem I cannot use a DataView component.

My WebForm contains the following code, however, after this code
executes the Product records in the DataGrid are not sorted by
ProductName. I have also tried setting the DataView's RowFilter
property and that does not work either. There are no error messages.

private DataView productDataView;
productDataView = northwindDataSet1.Tables["Products"].DefaultView;
productDataView.Sort = "ProductName";
productGrid.DataBind();

The properties of the DataGrid are:

DataSource = northwindDataSet1
DataMember = Products
DataKeyField = ProductId

I have also tried the following version of the code without success.

private DataView productDataView;
productDataView = northwindDataSet1.Tables["Products"].DefaultView;
productDataView.Sort = "ProductName";
productGrid.DataSource = productDataView;
productGrid.DataBind();

What do I have to do to get the DefaultView to work?

Thanks.
 
I have tested the with following code. it works fine.
Replace server name/Password/Userid of your setup.

private void Page_Load(object sender, System.EventArgs e)
{
DataSet ds = GetDataFromPubs();
DataView dv = ds.Tables
[0].DefaultView ;

dv.Sort = "ProductName";
dgGrid.DataSource = dv;
dgGrid.DataBind();


}

private DataSet GetDataFromPubs()
{
try
{
SqlConnection sqlConn =
new SqlConnection("Initial Catalog=NorthWind;Data Source=
[];User ID=[];Password=[];");

//Open the Connection.
sqlConn.Open();

//TODO: DataReader can be
used for faster access.
DataSet inboundTables =
new DataSet();


string query = "SELECT *
FROM PRODUCTS";


//Create DataAdapter
object and Attach the SqlCommand Object.
SqlDataAdapter sqlData =
new SqlDataAdapter(query,sqlConn);

//Fill the dataset object.
sqlData.Fill
(inboundTables);

sqlConn.Close();

return inboundTables;
}
catch(Exception ex)
{
throw ex;
}
}

Hope it helps.

-Vineet Batta
MCAD
 
Back
Top