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" AutoGenerateCol umns="False"
BackColor="Whit e"
BorderColor="#9 99999" BorderStyle="No ne" BorderWidth="1p x"
CellPadding="3"
GridLines="Vert ical" AllowPaging="Tr ue" AllowSorting="t rue"
PagerSettings-Mode="Numeric"
PagerSettings-Position="Botto m" PagerStyle-HorizontalAlign ="Left"
OnPageIndexChan ging="GridView1 _PageIndexChang ing"
OnSorting="Grid View1_Sorting">
<FooterStyle BackColor="#CCC CCC" ForeColor="Blac k" />
<Columns>
<asp:BoundFie ld DataField="Job Name" HeaderText="Job Name"
ReadOnly="True" SortExpression= "Job Name" />
<asp:BoundFie ld DataField="Agen cy Number" HeaderText="Age ncy Number"
ReadOnly="True" SortExpression= "Agency Number" />
<asp:BoundFie ld DataField="Trac king Number" HeaderText="Tra cking
Number" ReadOnly="True" SortExpression= "Tracking Number" />
<asp:BoundFie ld DataField="Mail ing Date" HeaderText="Mai ling Date"
HtmlEncode="Fal se" ReadOnly="True" SortExpression= "Mailing Date"
DataFormatStrin g="{0:MM/dd/yyyy}" />
</Columns>
<RowStyle BackColor="#EEE EEE" ForeColor="Blac k" />
<SelectedRowSty le BackColor="#008 A8C" Font-Bold="True" ForeColor="Whit e"
/>
<PagerStyle BackColor="#999 999" ForeColor="Blac k"
HorizontalAlign ="Center" />
<HeaderStyle BackColor="#000 084" Font-Bold="True" ForeColor="Whit e" />
<AlternatingRow Style BackColor="Gain sboro" />
</asp:GridView>
The code-behind sort event is:
protected void GridView1_Sorti ng(object sender, GridViewSortEve ntArgs e)
{
DataTable dataTable = GridView1.DataS ource as DataTable; <<<<<<<<<< THIS
IS NULL EVEN THOUGH SET AFTER A SUBMIT QUERY (below)
if (dataTable != null)
{
int pidx = GridView1.PageI ndex;
string sortDirection = GetSortDirectio n();
DataView dataView = new DataView(dataTa ble);
dataView.Sort = e.SortExpressio n + " " + sortDirection;
GridView1.DataS ource = dataView;
GridView1.DataB ind();
GridView1.PageI ndex = pidx;
}
}
The datasource binding occurs after a button click as follows:
protected void SearchTable_Cli ck(object sender, ImageClickEvent Args e)
{
string conn =
ConfigurationMa nager.Connectio nStrings["TST"].ConnectionStri ng;
TSTBLL bll = new TSTBLL();
TSTMLBE mt = bll.SubmitQuery (tbJobName.Text , tbAgencyNumber. Text,
tbOrderDateFrom .Text, tbOrderDateTo.T ext, conn);
GridView1.DataS ource = mt.Tst_Table;
GridView1.DataB ind();
}
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" AutoGenerateCol umns="False"
BackColor="Whit e"
BorderColor="#9 99999" BorderStyle="No ne" BorderWidth="1p x"
CellPadding="3"
GridLines="Vert ical" AllowPaging="Tr ue" AllowSorting="t rue"
PagerSettings-Mode="Numeric"
PagerSettings-Position="Botto m" PagerStyle-HorizontalAlign ="Left"
OnPageIndexChan ging="GridView1 _PageIndexChang ing"
OnSorting="Grid View1_Sorting">
<FooterStyle BackColor="#CCC CCC" ForeColor="Blac k" />
<Columns>
<asp:BoundFie ld DataField="Job Name" HeaderText="Job Name"
ReadOnly="True" SortExpression= "Job Name" />
<asp:BoundFie ld DataField="Agen cy Number" HeaderText="Age ncy Number"
ReadOnly="True" SortExpression= "Agency Number" />
<asp:BoundFie ld DataField="Trac king Number" HeaderText="Tra cking
Number" ReadOnly="True" SortExpression= "Tracking Number" />
<asp:BoundFie ld DataField="Mail ing Date" HeaderText="Mai ling Date"
HtmlEncode="Fal se" ReadOnly="True" SortExpression= "Mailing Date"
DataFormatStrin g="{0:MM/dd/yyyy}" />
</Columns>
<RowStyle BackColor="#EEE EEE" ForeColor="Blac k" />
<SelectedRowSty le BackColor="#008 A8C" Font-Bold="True" ForeColor="Whit e"
/>
<PagerStyle BackColor="#999 999" ForeColor="Blac k"
HorizontalAlign ="Center" />
<HeaderStyle BackColor="#000 084" Font-Bold="True" ForeColor="Whit e" />
<AlternatingRow Style BackColor="Gain sboro" />
</asp:GridView>
The code-behind sort event is:
protected void GridView1_Sorti ng(object sender, GridViewSortEve ntArgs e)
{
DataTable dataTable = GridView1.DataS ource as DataTable; <<<<<<<<<< THIS
IS NULL EVEN THOUGH SET AFTER A SUBMIT QUERY (below)
if (dataTable != null)
{
int pidx = GridView1.PageI ndex;
string sortDirection = GetSortDirectio n();
DataView dataView = new DataView(dataTa ble);
dataView.Sort = e.SortExpressio n + " " + sortDirection;
GridView1.DataS ource = dataView;
GridView1.DataB ind();
GridView1.PageI ndex = pidx;
}
}
The datasource binding occurs after a button click as follows:
protected void SearchTable_Cli ck(object sender, ImageClickEvent Args e)
{
string conn =
ConfigurationMa nager.Connectio nStrings["TST"].ConnectionStri ng;
TSTBLL bll = new TSTBLL();
TSTMLBE mt = bll.SubmitQuery (tbJobName.Text , tbAgencyNumber. Text,
tbOrderDateFrom .Text, tbOrderDateTo.T ext, conn);
GridView1.DataS ource = mt.Tst_Table;
GridView1.DataB ind();
}
Comment