Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.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# 使用EF更新Gridview中的记录_C#_Asp.net_Entity Framework_Gridview - Fatal编程技术网

C# 使用EF更新Gridview中的记录

C# 使用EF更新Gridview中的记录,c#,asp.net,entity-framework,gridview,C#,Asp.net,Entity Framework,Gridview,我有一个附有gridview的网页。gridview允许用户更新单个记录。gridview如下所示: JobSiteID JobSite1 1 13-03 2 13-04 3 13-06 4 13-09 5 13-15 row.Cells[1].Controls[0] TextBox txtJobSite = (Tex

我有一个附有gridview的网页。gridview允许用户更新单个记录。gridview如下所示:

JobSiteID         JobSite1
1                 13-03
2                 13-04
3                 13-06
4                 13-09
5                 13-15
row.Cells[1].Controls[0]
TextBox txtJobSite = (TextBox)row.Cells[1].Controls[1]
我创建了以下记录更新事件处理程序:

protected void changeJobSiteRecordsGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = changeJobSiteRecordsGridView.Rows[e.RowIndex];
    TextBox txtJobSite = row.FindControl("txtJobSite") as TextBox;

    if (txtJobSite != null)
    {
        using (ABCEntities4 Context = new ABCEntities4())
        {
            int jobSiteID = Convert.ToInt32(changeJobSiteRecordsGridView.DataKeys[e.RowIndex].Value);
            JobSite obj = Context.JobSites.First(x => x.JobSiteID == jobSiteID);
            obj.JobSite1 = txtJobSite.Text;
            Context.SaveChanges();
            changeJobSiteRecordsGridView.EditIndex = -1;
            changeJobSiteRecordsGridView.DataSource = Context.JobSites;
            changeJobSiteRecordsGridView.DataBind(); 
        }
    }
}
这是我的问题:
当我选择更新时,比如第一行的第2行,本地“row”变量表示
RowIndex==1
。 但是,在第二行中,我希望txtJobSite变量填充为“13-04”,但VS为变量指定了“null”。 因此,代码在下面的if-then语句上流动,而这不是预期的

任何帮助都将不胜感激


谢谢。

检查行的单元格属性,如下所示:

JobSiteID         JobSite1
1                 13-03
2                 13-04
3                 13-06
4                 13-09
5                 13-15
row.Cells[1].Controls[0]
TextBox txtJobSite = (TextBox)row.Cells[1].Controls[1]
用于文本框。如果“0”索引不起作用,请尝试使用1索引。然后,您的代码将如下所示:

JobSiteID         JobSite1
1                 13-03
2                 13-04
3                 13-06
4                 13-09
5                 13-15
row.Cells[1].Controls[0]
TextBox txtJobSite = (TextBox)row.Cells[1].Controls[1]
我记得FindControl遇到了类似的问题。这样,您可以显式地找到单元格,然后在单元格中找到控件