0

我最终试图对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;
        }       
    }
4

3 回答 3

0

尝试将您的日期对象转换为字符串

test.OnDate.ToString(//format here);

test.LastRegisterDate.ToString(//format here);

我想我遇到了一个类似的问题,即 DataTable 不知道如何处理 DateTime 对象。

于 2013-08-21T15:27:31.123 回答
0
if (!Page.IsPostBack)
        {
            User currUser = (User)Session["user"];
           //  Dataset dsGrdDource = new Dataset();
            IEnumerable<DataRow> result = from test in db.Tests
                                  select new
                               {
                                  testId= test.TestId,
                                  courseName = test.Course.Name,
                                  onDate = test.OnDate.ToShortDateString() ,
                                  lastRegisterDate = test.LastRegisterDate
                               };

          DataTable dtgrdSource= query.CopyToDataTable<DataRow>();
       try
         { 

                 gridAllTests.DataSource =dtgrdSource;
                 gridAllTests.DataBind();
               //  DataTable dt = (DataTable)dsGrdDource.Tables[0];
                Session["taskTable"] = dtgrdSource;
            } 

            catch (Exception err)
            {
                lblError.Text = err.Message.ToString();
            }
        }

如果您使用的日期时间格式不能应用于字符串,ToShortDateString()请不要应用格式。boundfield如果您不想使用,请ToShortDateString()尝试上面的代码,ToShortDateString()并在 BoundField 使用格式。

于 2013-08-21T15:36:46.557 回答
0

试试这个方法

          DataView dv=(DataView) from test in db.Tests
                                     select new
                                     {
                                         testId= test.TestId,
                                         courseName = test.Course.Name,
                                         onDate = test.OnDate,
                                         lastRegisterDate = test.LastRegisterDate
                                     };
         gridAllTests.DataSource = dv;

        DataTable dt = new DataTable();      

        DataSourceSelectArguments args = new DataSourceSelectArguments();

        DataView dv = new DataView();
        dv = (DataView)dv.Select(args);// This SqlDataSourceObject means your sql query return object ,like dataset or dataview, etc

        dt = dv.ToTable();
于 2013-08-21T15:26:42.797 回答