我想允许在GridView中的字段(日期)中进行排序。但即使我添加AllowSorting =“true”和SortExpression,它的标题文本也不会更改为超链接。我在其创建的行事件中添加了gridview的标题文本。因为我想合并单元格和行。这是问题吗?
<asp:GridView
ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="student_id"
onrowdatabound="GridView1_RowDataBound" onrowcreated="GridView1_RowCreated"
AllowSorting="true" OnSorting="GridView1_Sorting">
<Columns>
<asp:TemplateField HeaderText="S/no" >
<ItemTemplate>
<%# ((GridViewRow)Container).RowIndex + 1%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="get_date" ItemStyle-Width="110px" HeaderText="Date" DataFormatString="{0:d}"
ReadOnly="True" SortExpression="get_date" >
</asp:BoundField>
<asp:BoundField DataField="student_id" HeaderText="Student ID"
ReadOnly="True" >
</asp:BoundField>
<asp:BoundField DataField="previous_student_id" HeaderText="Previous ID"
ReadOnly="True" >
</asp:BoundField>
<asp:BoundField DataField="score_english" HeaderText="English"
ReadOnly="True" >
</asp:BoundField>
<asp:BoundField DataField="score_science" HeaderText="Science"
ReadOnly="True" >
</asp:BoundField>
<asp:BoundField DataField="mark" HeaderText="Marks"
ReadOnly="True" >
</asp:BoundField>
</Columns>
</asp:GridView>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// Invisibling the columns of second row header (normally created on binding)
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Visible = false; // Invisibiling s/no Header Cell
e.Row.Cells[1].Visible = false;
e.Row.Cells[2].Visible = false;
e.Row.Cells[3].Visible = false;
e.Row.Cells[6].Visible = false;
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
// Adding a column manually once the header created
if (e.Row.RowType == DataControlRowType.Header) // If header created
{
GridView DSGrid = (GridView)sender;
// Creating a Row
GridViewRow HeaderRow1 = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
GridViewRow HeaderRow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
//Adding first header
TableHeaderCell HeaderCell1 = new TableHeaderCell();
HeaderCell1.Text = "Student Details";
HeaderCell1.HorizontalAlign = HorizontalAlign.Center;
HeaderCell1.ColumnSpan =7;
HeaderRow1.Cells.Add(HeaderCell1);
//Adding S/no Column
TableHeaderCell HeaderCell = new TableHeaderCell();
HeaderCell.Text = "S/no";
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderCell.RowSpan = 2; // For merging first, second row cells to one
HeaderRow.Cells.Add(HeaderCell);
//Adding Date Column
HeaderCell = new TableHeaderCell();
HeaderCell.Text = "Date";
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderCell.RowSpan = 2;
HeaderRow.Cells.Add(HeaderCell);
//Adding Student ID Column
HeaderCell = new TableHeaderCell();
HeaderCell.Text = "Student ID";
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderCell.RowSpan = 2;
HeaderRow.Cells.Add(HeaderCell);
//Adding Previous ID Column
HeaderCell = new TableHeaderCell();
HeaderCell.Text = "Previous ID";
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderCell.RowSpan = 2;
HeaderRow.Cells.Add(HeaderCell);
//Adding Subject
HeaderCell = new TableHeaderCell();
HeaderCell.Text = "Subject";
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderCell.ColumnSpan = 2; // For merging two columns (English,Science)
HeaderRow.Cells.Add(HeaderCell);
//Adding Score Column
HeaderCell = new TableHeaderCell();
HeaderCell.Text = "Score";
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderCell.RowSpan = 2;
HeaderRow.Cells.Add(HeaderCell);
//Adding the Row at the 0th position (first row) in the Grid
DSGrid.Controls[0].Controls.AddAt(0, HeaderRow1);
//Adding the Row at the 1st position (second row) in the Grid
DSGrid.Controls[0].Controls.AddAt(1, HeaderRow);
}
}