【问题标题】:asp.net Gridview with objects带有对象的asp.net Gridview
【发布时间】:2015-07-17 07:15:49
【问题描述】:

大家好,我的 GridView 需要一些帮助,我刚开始做这个 asp.net 的事情,我不确定这是否可能。

所以这是我的问题:

我有一个带有“日期时间”对象的 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();

这就是我添加第一行的方式,直到这一点,它工作正常。 所以我在我的其他 Gridviews 上所做的是始终获取当前数据,如果页面已加载。

    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;


    }

但是现在我无法从 GridView 中取回我的 DateTime 对象,无论如何我可以得到它吗?这东西适用于字符串,但现在我找不到如何获取我的对象的答案。

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    不是这个

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

    使用下面的代码

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

    【讨论】:

    • 哇,我不知道为什么,但是现在可以了!我昨天用了这个,但我没有工作..突然它确实......mindblow^^
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    相关资源
    最近更新 更多