无法在网格视图的RowUpdating事件中跟踪e.NewValues

时间:2014-06-09 07:15:54

标签: asp.net c#-4.0 gridview

我的网格视图有问题。我有这个代码用于网格视图绑定:

public void FillGrid1(string StartAlpha1, string ColumnName1, string SearchText1)
    {
        using (DataClassesDataContext db = new DataClassesDataContext())
        {
            var query = Enumerable.Repeat(new
                {
                    Id = default(int),
                    ReasonText = string.Empty
                }, 0).ToList();
            if (StartAlpha1 == "All")
            {
                query = db.Reasons.Select(r => new { Id = r.Id, r.ReasonText }).FilterForColumn(ColumnName1, SearchText1).ToList();
            }
            else
            {
                query = db.Reasons.Select(r => new { Id = r.Id, r.ReasonText }).FilterForColumn(ColumnName1, SearchText1).Where(x => x.ReasonText.StartsWith(StartAlpha1)).ToList();
            }
            DataSet myDataSet = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Id", typeof(int)));
            dt.Columns.Add(new DataColumn("ReasonText", typeof(string)));
            foreach (var item in query)
            {
                if (item != null)
                {
                    DataRow dr = dt.NewRow();
                    dr["Id"] = int.Parse(item.Id.ToString());
                    dr["ReasonText"] = item.ReasonText.ToString();
                    dt.Rows.Add(dr);
                }
            }
            myDataSet.Tables.Add(dt);
            lbl_count_reason.Text = myDataSet.Tables[0].Rows.Count.ToString();
            if (myDataSet.Tables[0].Rows.Count > 0)
            {
                DataView myDataView = new DataView();
                myDataView = myDataSet.Tables[0].DefaultView;
                if (this.ViewState["SortExp1"] != null)
                {
                    myDataView.Sort = this.ViewState["SortExp1"].ToString()
                             + " " + this.ViewState["SortOrder1"].ToString();
                }
                GV_ViewReasons.DataSource = myDataView;
                GV_ViewReasons.DataBind();
            }
            else
            {
                myDataSet.Tables[0].Rows.Add(myDataSet.Tables[0].NewRow());
                GV_ViewReasons.DataSource = myDataSet;
                GV_ViewReasons.DataBind();
                int columncount = GV_ViewReasons.Rows[0].Cells.Count;
                GV_ViewReasons.Rows[0].Cells.Clear();
                GV_ViewReasons.Rows[0].Cells.Add(new TableCell());
                GV_ViewReasons.Rows[0].Cells[0].ColumnSpan = columncount;
                GV_ViewReasons.Rows[0].Cells[0].Text = "No Records Found";
            }
            if (GV_ViewReasons.Rows.Count != 0)
            {
                SetPageNumbers1();
            }
        }
    }

这是行更新事件:

 protected void GV_ViewReasons_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int reasonid = Convert.ToInt32(GV_ViewReasons.DataKeys[e.RowIndex].Value.ToString());
        if (!string.IsNullOrEmpty(e.NewValues["ReasonText"].ToString()))
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                db.UpdateReason(reasonid, e.NewValues["ReasonText"].ToString().Trim());
                db.SubmitChanges();
                GV_ViewReasons.EditIndex = -1;
                this.FillGrid1((String)Session["StartAlpha1"] ?? null, (String)Session["ColumnName1"] ?? null, (String)Session["SearchText1"] ?? null);
                UpdatePanel18.Update();
                MPE1.Show();
            }
        }
    }

此处if (!string.IsNullOrEmpty(e.NewValues["ReasonText"].ToString()))此行抛出Object refrences not instance....我无法使用此密钥获取新值。

我的FillGrid1怎么会有这个字段。我在这里出错了什么......

请帮助我.....

1 个答案:

答案 0 :(得分:1)

因为您在检查值是否为null之前将e.NewValues["ReasonText"]的值转换为字符串,因此您需要执行以下操作:

    if(e.NewValues["ReasonText"] != null && 
         !string.IsNullOrEmpty(e.NewValues["ReasonText"].ToString())) { //... }

这将首先检查e.NewValues["ReasonText"]的空值,如果它不为null,则它将进入下一个条件。