Gridview Sorting and Paging problem

Y

yefei

In my web design, I display records from a SQL DataBase according to
some filters
the GridView is defined with select commands and selest parameters

however, I also want to display all records in the database when the
user press a "View All" button.

In this case, I need to change the select comand and clear the
parameters. And I did this in the button_click() event. the GridView
can display desired results when I press the "View All" button,

However, when I click the header to sort, the GridView gone away, like
redered without any data. Press the button again, can show, but not
sorted.

i think it is because of the select command.

when sorting, the SqlDataSource.Select command is the one I
declaratively defined one, not the new one I assigned in the
button_Click() event.

So what Can I do with this to make the select command changed to the
one i want?
 
A

Aytaç ÖZAY

Hi,

I can't see your code so I suggest you an advise for this situation. The
best way for doing this is using GridView's paging and sorting mechanisms
but giving a data source is in code behind files. If you make like this you
control your code easily and not having problems like this. Also I suggest
you to use a DataView objects for Data Source. If you don't know how to do
it as again and I will post a code.

Have a nice work,

Aytaç ÖZAY
Software Developer
 
Y

yefei

Thanks very much
i try to define a gridview and a SqlDataSource without connectionstring
and select command like this
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True"
CellPadding="4" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None" EmptyDataText="No Data Record" >
<FooterStyle BackColor="#990000" Font-Bold="True"
ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True"
ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333"
HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True"
ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Button" />
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"></asp:SqlDataSource>

and a little operation in the .cs file
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataSource1.ConnectionString =
ConfigurationManager.ConnectionStrings["ServiceConnectionString"].ToString();
SqlDataSource1.SelectCommand = "select * from [tblService]";
}

it displays the gridview when i click the button. However, when I click
the column header or the page index to change, the gridview displays,
and the "No Data Record" is displayed, like no data in the data source.

I also tried to define the gridview with connection string and select
command,. but change the select command when the button is clicked. The
same problem came out.

I wrote a line to write the select commad when in Sorting or
PageIndexChanging event, it display the original one defined in the
..aspx file, instead of the one changed to in .aspx.cs file in the
Button_Click() event.

what could be the reason for this?
I am very appreciated for your help.
 
Y

yefei

the one i defined above, i enabled the sorting and paging but not
define the sort expression explicitly in the code file
 
Y

yefei

i found out that no matter how you define the gridview, when you change
the select command in the event of some controls, when sorting and
pageIndexChanging, the gridview is load again, and the select command
is set back to the original one.
 
A

Aytaç ÖZAY

Hi,

Maybe your SqlDataSource can not bound to your GridView when you click a
column header or a page index. I advise you not to to use a Toolbox
Components for this action, if you coded this, you have no problem occured I
think.

Have a good work,

Aytaç ÖZAY
Software Engineer
 

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