Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.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# 如何获得一个列单元格值,然后与1相加?_C#_Asp.net_Sql_Gridview - Fatal编程技术网

C# 如何获得一个列单元格值,然后与1相加?

C# 如何获得一个列单元格值,然后与1相加?,c#,asp.net,sql,gridview,C#,Asp.net,Sql,Gridview,我有一个gridview: 列(int类型) 如何从当前编辑行的单元格中获取值。 如何获得int类型的单元格值,然后是值+1?处理在单击行的更新按钮时,但在GridView控件更新行之前发生的。如果不使用TemplateFields则使用单元格的Text属性,否则使用FindControl获取对控件的引用 protected void GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e) { GridViewRow

我有一个gridview: 列(int类型) 如何从当前编辑行的单元格中获取值。 如何获得int类型的单元格值,然后是值+1?

处理在单击行的更新按钮时,但在
GridView
控件更新行之前发生的。如果不使用
TemplateFields
则使用单元格的Text属性,否则使用
FindControl
获取对控件的引用

protected void GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
    GridViewRow currentRow = ((GridView)sender).Rows[e.RowIndex];
    String intColumnText = currentRow.Cells[5].Text; //assuming it's the first cell
    int value;
    if(int.TryParse(intColumnText, out value))
    {
        //if you want to increment that value and override the old
        currentRow.Cells[5].Text = (value++).ToString();
    } 
}

显示GridView的aspx标记。“编辑行”表示您要处理
行编辑
事件或
行更新
事件?您尚未显示GridView的aspx部分。您是否使用TemplateFields、BoundFields、AutoGenerateColumns、SqlDataSource、ADO.NET等?我使用SqlDataSource将数据从表绑定到gridview,gridview上的选项设置为AutoGenerateColumns。不,我没有使用模板字段,但如果需要,我可以这样做编辑按钮来自gridview而不是从0创建的,我需要显示gridview吗?它是一个简单的gridview,使用sqldatasource将数据绑定到itIt,如果您一开始就发布它,它会有很大帮助。这意味着什么:“如何获得int类型的单元格值,然后是值+1”?是要将1添加到该值还是该行下一个单元格的int值?无论如何,我认为我的答案应该有助于做到这两个方面。它不起作用,它说:参数2必须与out键盘一起传递,并且要匹配int的最佳重载方法。TryParse(string,out)有一些无效参数。是的,列是一个模板字段,而有些不是。2.是的,我想在int的值上加1。我找到了一种获得该值的方法,但如何才能在该值上加1?如果单元格的值为1,则它将为2,依此类推on@InaaInzii当前位置我不确定我是否正确地理解了你。是否要将1添加到当前值?然后只需编写
currentRow.Cells[0].Text=(value++).ToString()。很抱歉,我错过了
TryParse
(已编辑)中的
out
。是的,我希望每次编辑单元格时,单元格中的值都随1升高,这应该在更新事件中。已尝试,但未成功受保护的void GridView1\u行更新(对象发送方,GridViewUpdateEventArgs e){GridViewRow currentRow=((GridView)发送方)。行[e.RowIndex];字符串intColumnText=currentRow.Cells[5]。文本;int值;if(int.TryParse(intColumnText,out值)){(value++).ToString();}