我知道有很多关于这个主题的帖子,但它们似乎都没有帮助解决我的问题。
点击标题标题时,我正在尝试对gridview进行排序 我通过搜索这里和其他网站找到了一些解决方案。但它似乎没有做到这一点 我希望有人可以告诉我,如果我错过了什么或问题是什么,因为什么都没发生,我真的不知道问题是什么。
我的网格视图:
<asp:GridView ID="GridView1" AllowSorting="true" OnSorting="GridView1_Sorting" runat="server" HeaderStyle-BackColor="#ffffff" HeaderStyle-ForeColor="black"
RowStyle-BackColor="#F2EFDF" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
AutoGenerateColumns="false" CellPadding="5">
<Columns>
<asp:BoundField DataField="Filename" HeaderText="Filnavn">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Time" HeaderText="Uploadet"/>
<asp:TemplateField ItemStyle-HorizontalAlign = "Center">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="DownloadFile"
CommandArgument='<%# Eval("Id") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
背后的代码:
public string SortingExpression
{
get
{
if (this.ViewState["SortExpression"] == null)
return "";
else
return (string)this.ViewState["SortExpression"];
}
set
{
this.ViewState["SortExpression"] = value;
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable m_DataTable = GridView1.DataSource as DataTable;
if (m_DataTable != null)
{
DataView m_DataView = new DataView(m_DataTable);
SortingExpression = e.SortExpression + " " + (SortingExpression.Contains("ASC") ? "DESC" : "ASC");
m_DataView.Sort = SortingExpression;
GridView1.DataSource = m_DataView;
GridView1.DataBind();
}
}
gridview数据来自数据表 也许这就是问题?
答案 0 :(得分:1)
添加SorteEpression =&#34; ColumnName&#34;在你的绑定字段中。
<Columns>
<asp:BoundField DataField="Filename" HeaderText="Filnavn" SortExpression="Filename">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Time" HeaderText="Uploadet" SortExpression="Time"/>
<asp:TemplateField ItemStyle-HorizontalAlign = "Center">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="DownloadFile"
CommandArgument='<%# Eval("Id") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
答案 1 :(得分:0)
问题出在分类时创建的数据表中:
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e){
DataTable m_DataTable = GridView1.DataSource as DataTable;
if (m_DataTable != null)
{
DataView m_DataView = new DataView(m_DataTable);
SortingExpression = e.SortExpression + " " + (SortingExpression.Contains("ASC") ? "DESC" : "ASC");
m_DataView.Sort = SortingExpression;
GridView1.DataSource = m_DataView;
GridView1.DataBind();
}}
出于某种原因,“Gridview1.Datasource”显示为null。哪个没有发生的原因。
我最终解决了这个问题:
DataTable m_DataTable = new DataTable();
string theMagicWord = Request["k"];
using (SqlConnection con = new SqlConnection("Connection information"))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select mBank_file.Id, mBank_file.Filename, CONVERT(varchar, mBank_file.TimeUploaded, 105) AS Time from mBank_file INNER JOIN mBank_keywords ON mBank_file.Id = mBank_keywords.fileId WHERE mBank_keywords.Keyword = @key ";
cmd.Parameters.AddWithValue("@key", theMagicWord);
cmd.Connection = con;
con.Open();
var dataReader = cmd.ExecuteReader();
m_DataTable.Load(dataReader);
con.Close();
}
}
if (m_DataTable != null)
{
DataView m_DataView = new DataView(m_DataTable);
SortingExpression = e.SortExpression + " " + (SortingExpression.Contains("ASC") ? "DESC" : "ASC");
m_DataView.Sort = SortingExpression;
GridView1.DataSource = m_DataView;
GridView1.DataBind();
}
通过再次从数据库中检索信息,它就像一个魅力。