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

ASP.NET Dynamic Gridview Question

P: n/a
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?
Mar 23 '06 #1
Share this Question
Share on Google+
4 Replies


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

"Bishop" wrote:
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?

Mar 23 '06 #2

P: n/a
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.
"clickon" <cl*****@discussions.microsoft.com> wrote in message
news:1B**********************************@microsof t.com...
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.

"Bishop" wrote:
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?

Mar 23 '06 #3

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

"Bishop" <no****@nospam.com> wrote in message
news:e3****************@TK2MSFTNGP12.phx.gbl...
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.
"clickon" <cl*****@discussions.microsoft.com> wrote in message
news:1B**********************************@microsof t.com...
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.

"Bishop" wrote:
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?


Mar 24 '06 #4

P: 1
Bishop,

Let me know if you find another solution. I'm having the same problem and because I'm building the select statement based on the user selection of a form. Depending on the options selected, I may be querying different database tables. For now, I'm going to try to implement the solution you suggested using session variables.

Regards,
Apr 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.