2012-10-17 3 views
0

안녕하세요, 저는 프로그래밍을 시작한 후 문제가 있습니다. 새로운 Datatable 나는 button1 클릭하면 GridView2에서 나던 보여줬습니다. "계획"테이블의 데이터로 채워져 있지만 (dtPlanning이 주석의 TextBoxes로 채워지는지 확인).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"> 

는 당신의 도움을 주셔서 감사합니다.

+0

dtPlanning은 비어 있습니다. 모든 코드가 아닙니다. – codingbiz

+0

계획 수표를 확인하십시오. 행 개수 및 데이터 – andy

+0

이걸로 무엇을하고 싶니? – codingbiz

답변

0

이 표가 방금 초기화 되었기 때문에 논리적으로 dtPlanning.Rows.Count = 0! 둘째, 당신이 할 수없는 Add 다른 한 테이블의 행, 사용자 Import

for (int i = 0; i < planning.Rows.Count; i++) 
{ 
    dtPlanning.ImportRow(planning.Rows[i]); 
} 

내가 왜 그냥 테이블 planning를 사용하지 않는 궁금해? 당신은해야한다

+0

completly 새로운 테이블을 만들기 위해 더 많은 행과 checkfunctions를 추가하고 싶습니다. 계획 테이블 내부의 정보 만 사용합니다. – Dave

0

:

  1. 추가 새로운 DataTable을
  2. 는 DataTable에 새로운 열을 추가 DataSet에
  3. 그리고 GridView2.DataSource = YourNameDataSet.Tables [index_of_DataTable_in_DataSet] .DefaultView;
관련 문제