我最终试图对gridview进行排序,但是当我转换时:
DataTable dt = (DataTable)gridAllTests.DataSource;
有一个例外 - 无法将对象 DateTime 转换为字符串。
数据源来自数据库sql server,使用Entity GridView:
<asp:GridView ID="gridAllTests" runat="server" AutoGenerateColumns="false"
DataKeyNames="testId" AllowSorting="true">
<Columns>
<asp:BoundField DataField="courseName" HeaderText="Course" SortExpression="courseName"/>
<asp:BoundField DataField="onDate" HeaderText="Date" SortExpression="onDate"
DataFormatString="{0:d}" HtmlEncode="false"/>
<asp:BoundField DataField="lastRegisterDate" HeaderText="LastDate"
SortExpression="lastRegisterDate" DataFormatString="{0:d}" HtmlEncode="false"/>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnRegister" runat="server" text="Register"
CommandName="Register" CommandArgument='<%#Eval("testId") %>' />
<asp:Literal ID="litAlreadyRegisterd" runat="server" Text="Registered"/>
<asp:Literal ID="litTooLate" runat="server" Text="Registration Over"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
SecondTestEntities1 db = new SecondTestEntities1();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
User currUser = (User)Session["user"];
gridAllTests.DataSource = from test in db.Tests
select new
{
testId= test.TestId,
courseName = test.Course.Name,
onDate = test.OnDate,
lastRegisterDate = test.LastRegisterDate
};
try
{
gridAllTests.DataBind();
DataTable dt = (DataTable)gridAllTests.DataSource;
Session["taskTable"] = dt;
}
catch (Exception err)
{
lblError.Text = err.Message.ToString();
}
}
if (gridAllTests.Rows.Count < 1)
{
lblMessage.Visible = true;
}
}