ASP.NET Dynamic Gridview Question

B

Bishop

I have a number of simple select queries that a user needs to be able to
execute and display the data on the screen in a gridview. My thought was
that I could use a Gridview set to dymanically generate columns and bound to
a SQLDataSource. Then the user can click buttons (or whatever) and it
changes the SelectCommand property of the SQLDataSource and re-binds the
GridView.

That worked great until I enabled paging. When paging is clicked, it always
reverts back to the default SelectCommand that's embeded with the control.

Any thoughts on what I can do to make this work?
 
G

Guest

Have you tried using the GridView.FilterExpression propety rather than
changing the SelectCommand, this will do what you want, it uses the syntax

GridView.FilterExpression = "FieldName = Value";

You can also add parameters by using the GridView.FilterParameters collection.

Below is some code from a page that i have written that updates the
datasource of a GridView based on which option is selected in a DropDownList
control.

ControlParameter cprViewState = new ControlParameter();
cprViewState.ControlID = ddlSelectView.ID.ToString();
cprViewState.PropertyName = "SelectedValue";
cprViewState.Name = "ViewState";
cprViewState.Type = TypeCode.Int32;
SqlDataSource1.FilterExpression = "Status = {0}";
SqlDataSource1.FilterParameters.Add(cprViewState);

the {0} in the filter expression represents the first item in the
FilterParameters collection. {1}, {2} etc would work for the second and
third items etc.
 
B

Bishop

I had not looked into that yet. When looking at it for the sqlDataSource it
looks like it's good for changing parts of the query such as the where
clause but in this case I need to change the entire query. Although it will
always be a select query, the fields I choose and the table I query could be
entirly different.
 
B

Bishop

I found one solution that looks like it might work. Set a session variable
to the current select string, then in the form load event set the
SelectCommand to the current select string. It seems to work but isn't the
most elegent. Any other ideas on this?
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top