GridView returning null datasource in sort event

T

Thirsty Traveler

I have a gridview where the datasource is bound after a selection. The
result of doing this is that sorting and paging do not work. I was given a
sample of how to resolve this, however my attempt to explicity enable
sorting failes because, unlike the sample, my sort event has a null in the
datasource and I am unsure why this is so.

Sample HTML code is:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3"
GridLines="Vertical" AllowPaging="True" AllowSorting="true"
PagerSettings-Mode="Numeric"
PagerSettings-Position="Bottom" PagerStyle-HorizontalAlign="Left"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnSorting="GridView1_Sorting">
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<Columns>
<asp:BoundField DataField="Job Name" HeaderText="Job Name"
ReadOnly="True" SortExpression="Job Name" />
<asp:BoundField DataField="Agency Number" HeaderText="Agency Number"
ReadOnly="True" SortExpression="Agency Number" />
<asp:BoundField DataField="Tracking Number" HeaderText="Tracking
Number" ReadOnly="True" SortExpression="Tracking Number" />
<asp:BoundField DataField="Mailing Date" HeaderText="Mailing Date"
HtmlEncode="False" ReadOnly="True" SortExpression="Mailing Date"
DataFormatString="{0:MM/dd/yyyy}" />
</Columns>
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White"
/>
<PagerStyle BackColor="#999999" ForeColor="Black"
HorizontalAlign="Center" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="Gainsboro" />
</asp:GridView>

The code-behind sort event is:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = GridView1.DataSource as DataTable; <<<<<<<<<< THIS
IS NULL EVEN THOUGH SET AFTER A SUBMIT QUERY (below)
if (dataTable != null)
{
int pidx = GridView1.PageIndex;
string sortDirection = GetSortDirection();
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + sortDirection;
GridView1.DataSource = dataView;
GridView1.DataBind();
GridView1.PageIndex = pidx;
}
}

The datasource binding occurs after a button click as follows:

protected void SearchTable_Click(object sender, ImageClickEventArgs e)
{
string conn =
ConfigurationManager.ConnectionStrings["TST"].ConnectionString;
TSTBLL bll = new TSTBLL();
TSTMLBE mt = bll.SubmitQuery(tbJobName.Text, tbAgencyNumber.Text,
tbOrderDateFrom.Text, tbOrderDateTo.Text, conn);
GridView1.DataSource = mt.Tst_Table;
GridView1.DataBind();
}
 
R

rjl

i would try not to instantane a new object where you say it is null,
this is wiping out the data and assinging a new object. perhaps, not
sure of further details though.
 
T

Thirsty Traveler

Actually, I was trying to manipulate the datasource that was created with
the filter request. I finally had to resort to sticking the entire dataset
into a session object and use that for sorting and paging.
 
R

rjl444

that is what MSFT would do. I dont like that though, I jsut as soon
read data again.
 

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,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top