Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从dataTable Asp.Net获取单元格值_C#_Asp.net - Fatal编程技术网

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

C# 如何从dataTable Asp.Net获取单元格值,c#,asp.net,C#,Asp.net,我正在创建绑定到网格视图的动态数据表。每一行都填充有按钮。当我确定在该行上单击哪个按钮时,我希望获得该行中单元格的当前值 标记: <asp:GridView ID="GridView2" runat="server" OnRowDataBound="GridView2_RowDataBound"> <Columns> <asp:TemplateField> <ItemTemplate> <as

我正在创建绑定到网格视图的动态数据表。每一行都填充有按钮。当我确定在该行上单击哪个按钮时,我希望获得该行中单元格的当前值

标记:

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

如果唯一的问题是您不知道如何读取此处所述的旧值:

//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]。字段(“Kolicina”);
int oldValue=int.Parse(旧);//万一你不知道
int newValue=oldValue+count;//只是一个例子
dtCurrentTable.Rows[clickedIndex].SetField(“Kolicina”,newValue.ToString());
我正在为
DataRow
使用
字段
/
设置字段
扩展方法,因为它是强类型的,甚至支持可为空的类型。
顺便问一下,为什么要将
int
s存储为
string
s?

那么,您的方法有什么问题(除了在
ViewState
中存储一个
DataTable
,它正在放大)?我不知道如何获取单元格的当前值。。dtCurrentTable.Rows[单击索引][“Kolicina”]=”;非常感谢你!但在那个旧值中,我需要将那个值减1,i;我是新来的,你能帮我吗?
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());