如何从dataTable Asp.Net获取单元格值

时间:2015-01-21 11:08:43

标签: c# asp.net

我正在创建绑定到Grid View的动态数据表。每行都填充了按钮。当我确定在行上单击了哪个按钮时,我想获取该行中单元格的当前值来修改她。

标记:

<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();
        }
    }
}

//确定在数据表中单击了哪个按钮

//在这里

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();
}

1 个答案:

答案 0 :(得分: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的{​​{1}} / SetField扩展方法,因为它是强类型的,甚至支持可空类型。 顺便说一下,为什么要将DataRow存储为int s?