2014-03-06 67 views
1

我有一個GridView薩德的頂部在網格末尾添加新行吧....對於幹什麼這麼代碼是添加新行對電網

protected void addRow(object sender, EventArgs e) 
    { 
     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++) 
       { 
        TextBox amount = (TextBox)Grid_AccEntry.Rows[rowIndex].Cells[0].FindControl("amount"); 
        DropDownList DrCr = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[1].FindControl("DrCr"); 
        DropDownList account = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[2].FindControl("account"); 
        DropDownList oprUnit = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[3].FindControl("oprUnit"); 
        DropDownList dept = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[4].FindControl("dept"); 
        DropDownList affiliate = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[5].FindControl("affiliate"); 
        DropDownList openItem = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[6].FindControl("openItem"); 
        TextBox lineNar = (TextBox)Grid_AccEntry.Rows[rowIndex].Cells[7].FindControl("lineNar"); 

        drCurrentRow = dtCurrentTable.NewRow(); 

        dtCurrentTable.Rows[i - 1]["amount"] = amount.Text.Trim() == "" ? 0 : Convert.ToDecimal(amount.Text); 
        dtCurrentTable.Rows[i - 1]["DrCr"] = DrCr.Text; 
        dtCurrentTable.Rows[i - 1]["account"] = account.Text; 
        dtCurrentTable.Rows[i - 1]["oprUnit"] = oprUnit.Text; 
        dtCurrentTable.Rows[i - 1]["dept"] = dept.Text; 
        dtCurrentTable.Rows[i - 1]["affiliate"] = affiliate.Text; 
        dtCurrentTable.Rows[i - 1]["openItem"] = openItem.Text; 
        dtCurrentTable.Rows[i - 1]["lineNar"] = lineNar.Text; 
        rowIndex++; 
       } 

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

       Grid_AccEntry.DataSource = dtCurrentTable; 
       Grid_AccEntry.DataBind(); 
      } 
     } 
     else 
     { 
      Response.Write("ViewState is null"); 
     } 
     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 amount = (TextBox)Grid_AccEntry.Rows[rowIndex].Cells[0].FindControl("amount"); 
        DropDownList DrCr = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[1].FindControl("DrCr"); 
        DropDownList account = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[2].FindControl("account"); 
        DropDownList oprUnit = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[3].FindControl("oprUnit"); 
        DropDownList dept = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[4].FindControl("dept"); 
        DropDownList affiliate = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[5].FindControl("affiliate"); 
        DropDownList openItem = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[6].FindControl("openItem"); 
        TextBox lineNar = (TextBox)Grid_AccEntry.Rows[rowIndex].Cells[7].FindControl("lineNar"); 

        amount.Text = dt.Rows[i]["amount"].ToString(); 
        DrCr.Text = dt.Rows[i]["DrCr"].ToString(); 
        account.Text = dt.Rows[i]["account"].ToString(); 
        oprUnit.Text = dt.Rows[i]["oprUnit"].ToString(); 
        dept.Text = dt.Rows[i]["dept"].ToString(); 
        affiliate.Text = dt.Rows[i]["affiliate"].ToString(); 
        openItem.Text = dt.Rows[i]["openItem"].ToString(); 
        lineNar.Text = dt.Rows[i]["lineNar"].ToString(); 

        //to bind long narr text to gridview line narr 
        TextBox lineNar1 = (TextBox)Grid_AccEntry.Rows[rowIndex].Cells[7].FindControl("lineNar"); 
        if (lineNar1.Text == "") 
         lineNar1.Text = lngNar.Text; 

        rowIndex++; 
       } 
      } 
     } 
    } 

這增加了新的行在網格結尾....但我想在頂部添加新行...我必須做什麼更改,以此代碼?

回答

3

由於您在DataTable的最後一個索引處添加了新行,因此您的代碼會在網格末尾添加新行。而不是你應該在0指數。您可以使用該InsertAt方法,如在下面的例子:

var dtCurrentTable = (DataTable)ViewState["CurrentTable"]; 
var newRow = dtCurrentTable.NewRow(); 
dtCurrentTable.Rows.InsertAt(newRow, 0); 
+0

workd pefectly .....üjumpstrtd我的一天:)謝謝兄弟... – karz

+0

其我的榮幸。祝你今天愉快 ;) – SpiderCode