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();
}
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();
}