嘿,我剛剛開始編程,我遇到了一個問題。當button1被點擊時,我創建的新Datatable不會在GridView2中顯示。雖然它充滿了來自「計劃」表的數據(檢查dtPlanning在註釋中填充了文本框)。無法將DataTable綁定到GridView
所以簡而言之:我想將DataTable規劃到新的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">
謝謝您的幫助。
dtPlanning是空的,但這不是所有的代碼 – codingbiz
檢查planning.Rows計數和數據 – andy
你想用這個來實現什麼? – codingbiz