简体   繁体   English

asp.net Gridview与对象

[英]asp.net Gridview with objects

Hey guys i need some help with my GridView, i just started this asp.net thing and i´m not sure if this is even possible. 大家好,我需要GridView的帮助,我只是启动了asp.net,我不确定这是否可能。

so here is my Problem: 所以这是我的问题:

I have a GridView with "DateTime" Objects. 我有一个带有“ DateTime”对象的GridView。

            DataTable Timetable = new DataTable();

            Timetable.Columns.Add("From", typeof(DateTime));
            Timetable.Columns.Add("To", typeof(DateTime));

            DataRow dr = Timetable.NewRow();

            dr[0] = Convert.ToDateTime(TextBox_TFrom.Text);
            dr[1] = Convert.ToDateTime(TextBox_TTo.Text);

            Timetable.Rows.Add(dr);

            trainingTimes.DataSource = Timetable;
            trainingTimes.DataBind();

This is how i add my first row and until this Point, it works fine. 这就是我添加第一行的方式,直到这一点,它都可以正常工作。 So what i did on my other Gridviews is to get always the current data, if the page is loaded. 因此,如果页面已加载,我在其他Gridview上所做的就是始终获取当前数据。

    public DataTable currentTimeGridView()
    {


        DataTable table = new DataTable();

        foreach (TableCell cell in trainingTimes.HeaderRow.Cells)
        {


            table.Columns.Add(cell.Text);

        }

        foreach (GridViewRow row in trainingTimes.Rows)
        {

            table.Rows.Add();

            for (int i = 0; i < row.Cells.Count; i++)
            {

                Label4.Text = trainingTimes.Columns.ToString();

                table.Rows[row.RowIndex][i] = row.Cells[i];

            }


        }


        return table;


    }

But now i cant get my DateTime Object back from the GridView, is there anyway i can get it? 但是现在我无法从GridView中获取我的DateTime对象,反正我还能得到它吗? this stuff worked with strings, but now i cant find the answer how to get my Object. 这些东西与字符串一起工作,但现在我找不到如何获取对象的答案。

Instead of this 代替这个

  table.Rows[row.RowIndex][i] = row.Cells[i]

Use Below code 使用以下代码

table.Rows[row.RowIndex][i] = row.Cells[i].Text;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM