473,326 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

ASP.NET Dynamic Gridview Question

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
4 7327
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
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
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
sunrei
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Larry Grady | last post by:
Anyone up for a challenge? I've been struggling with this for a few days and was hoping someone could help me. Pouring through all the messageboards I just can't find the solution. We have a...
3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
0
by: K B | last post by:
PLEASE be patient and read this. I believe I've actually reached the end of the Web trying to find the answer! I have an aspx using a MasterPage with a ContentPlaceholder. I have a datatable...
4
by: Ken Wigle | last post by:
All, I would be very grateful for any help on this question. I have an application in asp.net 2.0 where I dynamically create a datatable and then bind that to a gridview. Unfortunately, the...
7
by: =?Utf-8?B?SmVmZiBCZWVt?= | last post by:
The default paging behavior of the gridview doesn't work well with very large sets of data which means we have to implement some sort of custom paging. The examples I have seen (4guysfromrolla,...
1
by: =?Utf-8?B?cGVsZWdrMQ==?= | last post by:
i have a GridView which i can Page on (go to page 2,10,...) the thing is that i have a dynamic query which set the GridView from starts on page 1. the thing is that when i start to go over the...
1
by: jimmysjams | last post by:
I'm modifying someone else's code and I have hit a snag. I don't want to rewrite what's already there but I might have to. I have a gridview, in the gridview is a set number of columns that...
0
by: Craig Buchanan | last post by:
I am adding templates to a gridview dynamically. these columns are based on data values that are generated prior to calling the gridviews databind. the challenge is when the form does a...
14
by: agamoto | last post by:
hello guys, i don't know if this is the right tread to post but i have the following problem: i've built a dynamic gridview in run time because the gried values (columns, headers, etc) are...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.