2015-01-21 73 views
0

我正在創建綁定到網格視圖的動態數據表。每行填充按鈕。當我確定在該行上單擊哪個按鈕時,我想要獲取該行中單元格的當前值,以修改她。如何從dataTable中獲取單元格值Asp.Net

標記:

<asp:GridView ID="GridView2" runat="server" 
OnRowDataBound="GridView2_RowDataBound"> 
<Columns> 
    <asp:TemplateField> 
     <ItemTemplate> 
      <asp:Button ID="btnTest" runat="server" 
       CommandName="odzemi" 
       CssClass="button2" 
       Font-Bold="True" 
       Text="-" 
       Width="100px" 
       OnClick="btnTest_Click" /> 
     </ItemTemplate> 
    </asp:TemplateField> 
</Columns> 

創建行:

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++) 
      { 
       HttpCookie cookie = Request.Cookies["Democookie"]; 
       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"] = Label3.Text; 
       drCurrentRow["Cena1"] = Label4.Text; 
       //this is where i need to make changes 
       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; 
      } 


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

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

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

//確定被點擊數據表

//和h哪個按鈕ERE

protected void btnTest_Click(object sender, EventArgs e) 
{ 
     DataTable dtCurrentTable = (DataTable)ViewState["Markici"]; 

     var clickedRow = ((Button)sender).NamingContainer as GridViewRow; 
     var clickedIndex = clickedRow.RowIndex; 
     count--; 
     decimal noofcount = count; 

     //and here i want to get current value and modify her. 
     dtCurrentTable.Rows[clickedIndex]["Kolicina"] = "88"; 

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

那麼你的方法有什麼問題(除了在'ViewState'中存儲一個'DataTable',它正在吹掉它)? – 2015-01-21 11:10:48

+0

我不知道這裏如何獲得單元格的當前值.. dtCurrentTable.Rows [clickedIndex] [「Kolicina」] =「」; – programmingcenter 2015-01-21 11:13:50

回答

0

如果唯一的問題是,你不知道如何讀取舊值注意這裏:

//and here i want to get current value and modify her. 
dtCurrentTable.Rows[clickedIndex]["Kolicina"] = "88"; 

那麼這個工程:

object oldValue = dtCurrentTable.Rows[clickedIndex]["Kolicina"]; 
// cast/convert it to whatever it is 

或(什麼我更喜歡):

string old = dtCurrentTable.Rows[clickedIndex].Field<string>("Kolicina"); 
int oldValue = int.Parse(old); // in case that you don't know 
int newValue = oldValue + count; // just an example 
dtCurrentTable.Rows[clickedIndex].SetField("Kolicina", newValue.ToString()); 

我正在使用Field/SetFieldDataRow的擴展方法,因爲它是強類型的,甚至支持可空類型。 順便說一下,爲什麼你將int s存儲爲string s?

+0

非常感謝你!但是在那個舊價值中,我需要將這個值減1,我;新的,你能幫我嗎? – programmingcenter 2015-01-21 11:42:37

+0

已完成,對於愚蠢的問題感到抱歉 – programmingcenter 2015-01-21 11:45:44

相關問題