グリッドビューをソート可能にしようとしています。ソートを許可し、以下に示すオンソート属性を追加しました
<asp:GridView ID="GWCase" runat="server" DataKeyNames="detail, propertydetail,suspectdetail " BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" Width="100%" AutoGenerateSelectButton="True" OnSelectedIndexChanged="GWCase_SelectedIndexChanged" AutoGenerateColumns="False" AllowPaging="True" PageSize="5" OnPageIndexChanging="GWCase_PageIndexChanging" AllowSorting="True" OnSorting="gridView_Sorting" CurrentSortField="memberreportid" CurrentSortDirection="ASC">
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
<Columns>
<asp:BoundField DataField="memberreportid" HeaderText="MemberReportID" SortExpression="memberreportid"/>
<asp:BoundField DataField="typeofcrime" HeaderText="Type of Crime" SortExpression="typeofcrime" />
<asp:BoundField DataField="crdatetime" HeaderText="ReportDateTime" SortExpression="crdatetime" />
<asp:BoundField DataField="address" HeaderText="Address" SortExpression="address" />
<asp:BoundField DataField="incidentdate" HeaderText="Incident Date" SortExpression="incidentdate" />
<asp:BoundField DataField="incidenttime" HeaderText="Incident Time" SortExpression="incidenttime"/>
<asp:BoundField DataField="property" HeaderText="Property" SortExpression="Property"/>
<asp:BoundField DataField="victim" HeaderText="Victim" SortExpression="victim" />
<asp:BoundField DataField="suspect" HeaderText="Suspect" SortExpression="suspect"/>
<asp:BoundField DataField="detail" HeaderText="Detail" SortExpression="detail" Visible="false"/>
</Columns>
</asp:GridView>
これは、ソートを有効にするために使用した私のC#コードです
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
SortDirection sortDirection = SortDirection.Ascending;
string sortField = string.Empty;
SortGridview((GridView)sender, e, out sortDirection, out sortField);
string strSortDirection = e.SortDirection == SortDirection.Ascending ? "ASC" : "DESC";
//Error happens here
GWCase.DataSource = (e.SortExpression + " " + strSortDirection);
GWCase.DataBind();
}
private void SortGridview(GridView gridView, GridViewSortEventArgs e, out SortDirection sortDirection, out string sortField)
{
sortField = e.SortExpression;
sortDirection = e.SortDirection;
if (gridView.Attributes["CurrentSortField"] != null && gridView.Attributes["CurrentSortDirection"] != null)
{
if (sortField == gridView.Attributes["CurrentSortField"])
{
if (gridView.Attributes["CurrentSortDirection"] == "ASC")
{
sortDirection = SortDirection.Descending;
}
else
{
sortDirection = SortDirection.Ascending;
}
}
gridView.Attributes["CurrentSortField"] = sortField;
gridView.Attributes["CurrentSortDirection"] = (sortDirection == SortDirection.Ascending ? "ASC" : "DESC");
}
}
私が従うチュートリアルでは、グリッドビューをバインドフィールドにバインドしたため、使用しなかったデータレイヤーが必要でした。ただし、データソースを取得しようとすると、上記のエラーが表示されました。
これは私のグリッドビューをバインドする方法です
private void LoadGrid()
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI";
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("SELECT memberreportid, typeofcrime, crdatetime, address, detail, incidentdate, incidenttime, property, victim, suspect, detail, suspectdetail, propertydetail from memberreport where assignto = 'unassign'", conn);
da.Fill(ds);
GWCase.DataSource = ds.Copy();
GWCase.DataBind();
conn.Close();
}