2015-01-13 1 views
1

안녕하세요 데이터 테이블에 문제가 있습니다. 사용자가 button을 클릭 할 때마다 데이터 테이블에 새로운 행을 삽입하고 싶습니다. 저는 10 개의 버튼을 가지고 있는데, 버튼 1을 클릭하면 새로운 행이 삽입됩니다. 그러나 버튼 2를 클릭하면 기존 행이 새로운 행으로 바뀝니다. 도움!다른 단추에서 dataTable의 행 추가 asp.net]

여기에 내가 버튼에서 새 행을 추가하기위한 메소드를 호출 ////이 행을 추가

private void RemoveDuplicates (Data_Table dt)       
     { 

    if (ViewState["Markici"] != null) 
    { 
     dtCurrentTable = (DataTable)ViewState["Markici"]; 

     if (dtCurrentTable.Rows.Count > 0) 
     { 

      for (int i = dtCurrentTable.Rows.Count - 1; i >= 0; i--) 
      { 
       if (i == 0) 
       { 
        break; 
       } 
       for (int j = i - 1; j >= 0; j--) 
       { 
        if (dtCurrentTable.Rows[i]["Cena1"].ToString() == dtCurrentTable.Rows[j]["Cena1"].ToString()) 
        { 

         dtCurrentTable.Rows[i].Delete(); 
         break; 


        } 
       } 
      } 
     } 
     dtCurrentTable.AcceptChanges(); 

     // ViewState["Markici"] = dtCurrentTable; 
     // Repeater1.DataSource = dtCurrentTable; 
     // Repeater1.DataBind(); 

      AddNewRecordRowToGrid();        
      AddNewRecordRowToGridf(); 


     decimal vkupno = 0m; 
     vkupno += Convert.ToDecimal(Label6.Text); 

     decimal vkupno2 = 0m; 
     vkupno2 += Convert.ToDecimal(Label17.Text); 
     vkupno += vkupno2; 

     Label26.Text = vkupno.ToString(); 
     //xml table 
    //  DataSet ds = new DataSet(); 
    // ds.Tables.Add(dtCurrentTable); 

     //binding Gridview with New Row 
     GridView2.DataSource = dtCurrentTable; 
     GridView2.DataBind(); 

    } 
} 

// 방법을 클릭, dtCurrentTable.AcceptChanges 추가 버튼 클릭으로 9

private void AddNewRecordRowToGrid() 
    {   
     int counter; 

     if (Request.Cookies["kasa"] == null) 

      counter = 0; 


     else 
     { 
      counter = int.Parse(Request.Cookies["kasa"].Value); 

     } 
     counter++; 

     Response.Cookies["kasa"].Value = counter.ToString(); 
     Response.Cookies["kasa"].Expires = DateTime.Now.AddYears(2); 

     if (ViewState["Markici"] != null) 
     { 
      DataTable dtCurrentTable = (DataTable)ViewState["Markici"]; 
      DataRow drCurrentRow = null; 

      if (dtCurrentTable.Rows.Count > 0) 
      { 

       for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) 
       { 
        drCurrentRow = dtCurrentTable.NewRow(); 

        drCurrentRow["FirmaID"] = Request.Cookies["firma"].Value; 
        drCurrentRow["Godina"] = Request.Cookies["godina"].Value; 
        drCurrentRow["KasaID"] = Request.Cookies["kasa"].Value; 
        drCurrentRow["MarkicaID"] = counter; 
        drCurrentRow["Datum"] = DateTime.Now; 
        drCurrentRow["Masa"] = Session["masa39"]; 
        drCurrentRow["VrabotenID"] = Session["New"]; 
        drCurrentRow["Artikal"] = Label3.Text; 
        drCurrentRow["Cena1"] = Label4.Text; 
        drCurrentRow["Kolicina"] = Label5.Text; 
        drCurrentRow["Smena"] = Session["smena1"]; 
        drCurrentRow["VkIznos"] = Label6.Text; 
        drCurrentRow["VkDanok"] = Label8.Text; 
        drCurrentRow["SySDatum"] = DateTime.Now; 
        drCurrentRow["Vid"] = Label23.Text; 
        drCurrentRow["Edmera"] = Label10.Text; 
        drCurrentRow["ArtikalID"] = Label33.Text; 
       } 
       /* 
       for (int i = 0; i < dtCurrentTable.Rows.Count; i++) 
       { 
        if (dtCurrentTable.Rows[i].IsNull(0) == true) 
        { 
         dtCurrentTable.Rows[i].Delete(); 
         dtCurrentTable.AcceptChanges(); 
        } 
       }*/ 

       //Removing initial blank row 
       if (dtCurrentTable.Rows[0][0].ToString() == "") 
       { 
        dtCurrentTable.Rows[0].Delete(); 
        dtCurrentTable.AcceptChanges(); 

       } 

       //Added New Record to the DataTable 
       dtCurrentTable.Rows.Add(drCurrentRow); 
       //storing DataTable to ViewState 
       ViewState["Markici"] = dtCurrentTable; 

       //binding Gridview with New Row 
       GridView2.DataSource = dtCurrentTable; 
       GridView2.DataBind(); 


      } 
     } 
    } 

    // call this in button 10 click 
    private void AddNewRecordRowToGridf() 
    { 

     if (ViewState["Markici"] != null) 
     { 
      DataTable dtCurrentTable = (DataTable)ViewState["Markici"]; 
      DataRow drCurrentRow = null; 

      if (dtCurrentTable.Rows.Count > 0) 
      { 

       for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) 
       { 


        //Creating new row and assigning values 
        drCurrentRow = dtCurrentTable.NewRow(); 

        drCurrentRow["FirmaID"] = Request.Cookies["firma"].Value; 
        drCurrentRow["Godina"] = Request.Cookies["godina"].Value; 
        drCurrentRow["KasaID"] = Request.Cookies["kasa"].Value; 
        drCurrentRow["MarkicaID"] = Request.Cookies["kasa"].Value; 
        drCurrentRow["Datum"] = DateTime.Now; 
        drCurrentRow["Masa"] = Session["masa39"]; 
        drCurrentRow["VrabotenID"] = Session["New"]; 
        drCurrentRow["Artikal"] = Label12.Text; 
        drCurrentRow["Cena1"] = Label13.Text; 
        drCurrentRow["Kolicina"] = Label11.Text; 
        drCurrentRow["Smena"] = Session["smena1"]; 
        drCurrentRow["VkIznos"] = Label17.Text; 
        drCurrentRow["VkDanok"] = Label18.Text; 
        drCurrentRow["SySDatum"] = DateTime.Now; 
        drCurrentRow["Vid"] = Label24.Text; 
        drCurrentRow["Edmera"] = Label16.Text; 
        drCurrentRow["ArtikalID"] = Label34.Text; 
       } 
       for (int i = 0; i < dtCurrentTable.Rows.Count; i++) 
       { 
        if (dtCurrentTable.Rows[i].IsNull(0) == true) 
        { 
         dtCurrentTable.Rows[i].Delete(); 
         dtCurrentTable.AcceptChanges(); 
        } 
       } 
       /* 
       //Removing initial blank row 
       if (dtCurrentTable.Rows[0][0].ToString() == "") 
       { 
        dtCurrentTable.Rows[0].Delete(); 
        dtCurrentTable.Rows[1].Delete(); 
        dtCurrentTable.AcceptChanges(); 

       } 
       */ 
       //Added New Record to the DataTable 
       dtCurrentTable.Rows.Add(drCurrentRow); 
       //storing DataTable to ViewState 
       ViewState["Markici"] = dtCurrentTable; 
       //binding Gridview with New Row 
       GridView2.DataSource = dtCurrentTable; 


       GridView2.DataBind(); 
      } 
     } 
    protected void Button9_Click(object sender, EventArgs e) 
    { 
     RemoveDuplicates(dt); 
     } 
    protected void Button10_Click(object sender, EventArgs e) 
    { 
     RemoveDuplicates(dt); 
     } 
     // create DataTable 
    private void AddDefaultFirstRecord() 
    { 

     //creating DataTable 
     DataTable dt = new DataTable(); 
     DataRow dr; 
     dt.TableName = "Markici"; 
     //creating columns for DataTable 
     dt.Columns.Add(new DataColumn("FirmaID", typeof(System.Int32))); 
     dt.Columns.Add(new DataColumn("Godina", typeof(System.Int32))); 
     dt.Columns.Add(new DataColumn("KasaID", typeof(System.Int32))); 
     dt.Columns.Add(new DataColumn("MarkicaID", typeof(System.Int64))); 
     dt.Columns.Add(new DataColumn("Datum", typeof(System.DateTime))); 
     dt.Columns.Add(new DataColumn("Masa", typeof(System.Int32))); 
     dt.Columns.Add(new DataColumn("VrabotenID", typeof(System.Int32))); 
     dt.Columns.Add(new DataColumn("Artikal", typeof(System.String))); 
     dt.Columns.Add(new DataColumn("Cena1", typeof(System.String))); 
     dt.Columns.Add(new DataColumn("Kolicina", typeof(System.Decimal))); 
     dt.Columns.Add(new DataColumn("Smena", typeof(System.Int32))); 
     dt.Columns.Add(new DataColumn("VkIznos", typeof(System.Decimal))); 
     dt.Columns.Add(new DataColumn("VkDanok", typeof(System.Decimal))); 
     dt.Columns.Add(new DataColumn("SysDatum", typeof(System.DateTime))); 
     dt.Columns.Add(new DataColumn("Vid", typeof(System.String))); 
     dt.Columns.Add(new DataColumn("EdMera", typeof(System.String))); 
     dt.Columns.Add(new DataColumn("ArtikalID", typeof(System.String))); 
     dr = dt.NewRow(); 
     dt.Rows.Add(dr); 

     ViewState["Markici"] = dt; 

     GridView2.DataSource = dt; 
     GridView2.DataBind(); 
    } 
+0

데이터 행에 고유 ID와 같은 것이 있어야합니다. 그래서 각 행을 고유하게 식별 할 수 있습니다. 그러면 button2에서 클릭 한 행을 대체 할 수 있습니다. button2 – tarzanbappa

+0

일부 예제 코드를 클릭하십시오. – programmingcenter

답변

0

를이 전화(); dtCurrentTable.Rows.Add (drCurrentRow) 이후;

DataTable을 ViewState에 보관하지 마십시오. DataTable은 큰 개체입니다. 페이지 성능에 영향을줍니다. 캐시 또는 세션을 유지할 수 있습니다.

내 제안은 데이터 테이블 개체가 아닌 데이터 테이블 데이터 원본을 캐시하는 것입니다.

+0
관련 문제