2014-03-06 1 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

이 pefectly workd ..... 유 : 덕분에 형제 ... – karz

+0

는 나의 기쁨 나의 일 jumpstrtd. 좋은 하루 되세요;) – SpiderCode