2013-07-03 28 views
0

我有這樣的代碼在這裏:C#網格視圖設置行回數爲1

private void SetInitialRow() 
    { 
     DataTable dt = new DataTable(); 
     DataRow dr = null; 
     dt.Columns.Add(new DataColumn("Question", typeof(string))); 
     dt.Columns.Add(new DataColumn("Answer", typeof(string))); 

     dr = dt.NewRow(); 
     dr["Question"] = string.Empty; 
     dr["Answer"] = string.Empty; 


     dt.Rows.Add(dr); 


     //Store the DataTable in ViewState 
     ViewState["CurrentTable"] = dt; 

     GridView1.DataSource = dt; 
     GridView1.DataBind(); 
    } 

    private void AddNewRowToGrid() 
    { 
     int rowIndex = 0; 

     if (ViewState["CurrentTable"] != null) 
     { 
      DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; 
      DataRow drCurrentRow = null; 
      if (dtCurrentTable.Rows.Count > 0) 
      { 
       for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) 
       { 
        //extract the TextBox values 
        TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1"); 
        TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2"); 


        drCurrentRow = dtCurrentTable.NewRow(); 


        dtCurrentTable.Rows[i - 1]["Question"] = box1.Text; 
        dtCurrentTable.Rows[i - 1]["Answer"] = box2.Text; 


        rowIndex++; 
       } 
       dtCurrentTable.Rows.Add(drCurrentRow); 
       ViewState["CurrentTable"] = dtCurrentTable; 

       GridView1.DataSource = dtCurrentTable; 
       GridView1.DataBind(); 
      } 
     } 
     else 
     { 
      Response.Write("ViewState is null"); 
     } 

     // Set Previous Data on Postbacks 
     SetPreviousData(); 
    } 

    private void SetPreviousData() 
    { 
     int rowIndex = 0; 
     if (ViewState["CurrentTable"] != null) 
     { 
      DataTable dt = (DataTable)ViewState["CurrentTable"]; 
      if (dt.Rows.Count > 0) 
      { 
       for (int i = 0; i < dt.Rows.Count; i++) 
       { 
        TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1"); 
        TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2"); 


        box1.Text = dt.Rows[i]["Question"].ToString(); 
        box2.Text = dt.Rows[i]["Answer"].ToString(); 

        Session["Question1"] = box1.Text; 
        rowIndex++; 
       } 
      } 
     } 
    } 

    protected void btnAdd_Click1(object sender, EventArgs e) 
    { 
     AddNewRowToGrid(); 

    } 

,現在我有另一個按鈕調用的BtnCreate。我點擊添加按鈕在網格視圖中添加行,意味着每次點擊都添加一行等等。我點擊創建按鈕後,我想行數回到一,現在,行將卡住的次數,我點擊添加按鈕,直到我刷新頁面。在點擊創建按鈕後,我需要重新設置的行數爲1。

回答

1

可以direcly調用SetInitialRow()在你的BtnCreate單擊事件的方法,這意味着你要綁定一個行的GridView這意味着行數reseted爲1

0

您可以將其添加到btnAdd_Click1方法:

0

我試過你的代碼,它在我的工作。


像這樣。

protected void Page_Load(object sender, EventArgs e) 
    { 
     SetInitialRow(); 
    } 
    private void SetInitialRow() 
    { 
     DataTable dt = new DataTable(); 
     DataRow dr = null; 
     dt.Columns.Add(new DataColumn("Question", typeof(string))); 
     dt.Columns.Add(new DataColumn("Answer", typeof(string))); 
     dr = dt.NewRow(); 
     dr["Question"] = string.Empty; 
     dr["Answer"] = string.Empty; 
     dt.Rows.Add(dr); 
     //Store the DataTable in ViewState 
     ViewState["CurrentTable"] = dt; 
     GridView1.DataSource = dt; 
     GridView1.DataBind(); 
    } 
    private void AddNewRowToGrid() 
    { 
     int rowIndex = 0; 
     if (ViewState["CurrentTable"] != null) 
     { 
      DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; 
      DataRow drCurrentRow = null; 
      if (dtCurrentTable.Rows.Count > 0) 
      { 
       for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) 
       { 
        //extract the TextBox values 
        TextBox box1 = (TextBox)GridView1.Rows[i].FindControl("TextBox1"); 
        TextBox box2 = (TextBox)GridView1.Rows[i].FindControl("TextBox2"); 
        drCurrentRow = dtCurrentTable.NewRow(); 
        dtCurrentTable.Rows[i - 1]["Question"] = box1.Text; 
        dtCurrentTable.Rows[i - 1]["Answer"] = box2.Text; 
        rowIndex++; 
       } 
       dtCurrentTable.Rows.Add(drCurrentRow); 
       ViewState["CurrentTable"] = dtCurrentTable; 

       GridView1.DataSource = dtCurrentTable; 
       GridView1.DataBind(); 
      } 
     } 
     else 
     { 
      Response.Write("ViewState is null"); 
     } 
     // Set Previous Data on Postbacks 
     SetPreviousData(); 
    } 

    private void SetPreviousData() 
    { 
     int rowIndex = 0; 
     if (ViewState["CurrentTable"] != null) 
     { 
      DataTable dt = (DataTable)ViewState["CurrentTable"]; 
      if (dt.Rows.Count > 0) 
      { 
       for (int i = 0; i < dt.Rows.Count; i++) 
       { 
        TextBox box1 = (TextBox)GridView1.Rows[rowIndex].FindControl("TextBox1"); 
        TextBox box2 = (TextBox)GridView1.Rows[rowIndex].FindControl("TextBox2"); 
        box1.Text = dt.Rows[i]["Question"].ToString(); 
        box2.Text = dt.Rows[i]["Answer"].ToString(); 

        Session["Question1"] = box1.Text; 
        rowIndex++; 
       } 
      } 
     } 
    } 
    protected void btnAdd_Click(object sender, EventArgs e) 
    { 
     AddNewRowToGrid(); 
    } 
    protected void prevRow_Click(object sender, EventArgs e) 
    { 
     SetPreviousData(); 
    } 

我索裏索裏如果我的回答是根據您的要求,錯誤的軌道。歡迎評論和提問。謝謝

相關問題