C# Can';t将数据表绑定到GridView

C# Can';t将数据表绑定到GridView,c#,asp.net,C#,Asp.net,嘿,伙计们,我刚开始编程,遇到了一个问题。单击按钮1时,我创建的新数据表不会显示在GridView2中。尽管它是用“计划”表中的数据填充的(检查dtPlanning是否用注释中的文本框填充) 简而言之:我想将DataTable planning放入新的DataTable dtPlanning中,并在gridview中显示它 代码隐藏: protected void Button1_Click(object sender, EventArgs e) { DataTable dtPlanni

嘿,伙计们,我刚开始编程,遇到了一个问题。单击按钮1时,我创建的新数据表不会显示在GridView2中。尽管它是用“计划”表中的数据填充的(检查dtPlanning是否用注释中的文本框填充)

简而言之:我想将DataTable planning放入新的DataTable dtPlanning中,并在gridview中显示它

代码隐藏:

protected void Button1_Click(object sender, EventArgs e)
{
    DataTable dtPlanning = new DataTable();
    dtPlanning.Columns.Add("Courseday", typeof(int));
    dtPlanning.Columns.Add("Part", typeof(string));
    dtPlanning.Columns.Add("Design", typeof(string));
    dtPlanning.Columns.Add("Lesson", typeof(string));

    //DataRow dr = planning.Rows[1];  
    //TextBox2.Text = (dr["Daynumber"]).ToString();
    //TextBox3.Text = (dr["Part"]).ToString();
    //TextBox4.Text = (dr["Design"]).ToString();
    //TextBox5.Text = (dr["Lesson"]).ToString();

    for (int i = 0; i < dtPlanning.Rows.Count; i++)
    {
        DataRow dr = dtPlanning.NewRow();
        foreach (DataRow datarow in planning.Rows)
        {
            dtPlanning.Rows.Add(datarow);
        }
    }
    GridView2.DataSource = dtPlanning;
    GridView2.DataBind();
}
<asp:GridView ID="GridView2" runat="server">
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
DataTable dtPlanning=新建DataTable();
dtPlanning.Columns.Add(“Courseday”,typeof(int));
dtPlanning.Columns.Add(“Part”,typeof(string));
dtPlanning.Columns.Add(“设计”,typeof(字符串));
dtPlanning.Columns.Add(“课程”,typeof(字符串));
//DataRow dr=计划行[1];
//TextBox2.Text=(dr[“Daynumber”]).ToString();
//TextBox3.Text=(dr[“Part”]).ToString();
//TextBox4.Text=(dr[“Design”]).ToString();
//TextBox5.Text=(dr[“Lesson”]).ToString();
对于(int i=0;i
源代码:

protected void Button1_Click(object sender, EventArgs e)
{
    DataTable dtPlanning = new DataTable();
    dtPlanning.Columns.Add("Courseday", typeof(int));
    dtPlanning.Columns.Add("Part", typeof(string));
    dtPlanning.Columns.Add("Design", typeof(string));
    dtPlanning.Columns.Add("Lesson", typeof(string));

    //DataRow dr = planning.Rows[1];  
    //TextBox2.Text = (dr["Daynumber"]).ToString();
    //TextBox3.Text = (dr["Part"]).ToString();
    //TextBox4.Text = (dr["Design"]).ToString();
    //TextBox5.Text = (dr["Lesson"]).ToString();

    for (int i = 0; i < dtPlanning.Rows.Count; i++)
    {
        DataRow dr = dtPlanning.NewRow();
        foreach (DataRow datarow in planning.Rows)
        {
            dtPlanning.Rows.Add(datarow);
        }
    }
    GridView2.DataSource = dtPlanning;
    GridView2.DataBind();
}
<asp:GridView ID="GridView2" runat="server">


感谢您的帮助。

从逻辑上讲,
dtPlanning.Rows.Count=0
,因为此表刚刚初始化! 其次,您不能将一行从一个表添加到另一个表,用户不能导入

for (int i = 0; i < planning.Rows.Count; i++)
{
    dtPlanning.ImportRow(planning.Rows[i]);
}
for(int i=0;i
我想知道您为什么不直接使用表
计划

您必须:

  • 向DataSet添加一个新的DataTable
  • 向DataTable添加新列
  • 和GridView2.DataSource=YourNameDataSet.Tables[数据集中数据表的索引].DefaultView
    dtPlanning为空,但这不是全部的codeCheck计划。行计数和数据您想用它实现什么?因为我想添加更多行和检查函数以创建一个全新的表。我只使用计划表中的信息。