如何从Gridview文本框中获取数据?

时间:2014-02-15 09:55:52

标签: c# asp.net

在editatatus中输入文本框表格gridview的值无法跟上。它是空的。

    testlinq1DataContext db = new testlinq1DataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            show();
        }
    }

    private void show()
    {
        var ace = from a in db.ITdevicedetails
                  join b in db.Brands
                  on a.Specid equals b.Specid
                  join c in db.Devicestatus
                  on a.Statusid equals c.Statusid
                  where a.Statusid == "ST03"
                  join d in db.Typedevices
                  on a.Type equals d.Type

                  select new
                  {
                      IDdevice = a.IDdevice,
                      Type = d.Type,
                      Brand = b.Brand1,
                      Spec = b.Spec,

                      Model = b.Model,
                      Accode = a.Accode,
                      IPaddress = a.IPaddress,
                      Serialnumber = a.Serialnumber,
                      Status = c.Status,
                      Comment = a.Comment
                  };

        GridView1.DataSource = ace.ToList();
        GridView1.DataBind();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        if (Page.IsPostBack)
        {
            show();
            GridView1.EditIndex = e.NewEditIndex;

            DataBind();
        }
    }


    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (Page.IsPostBack)
        {
            var ie =
                (from ac in db.ITdevicedetails where ac.IDdevice == "note01" select ac).FirstOrDefault();

            **//this's my problem the value of GridView1.Rows[0].Cells[8].Text; is null. 
                // but If I assign a value to it, ie.Comment = "nb123"; it's worked.** 

            ie.Comment = GridView1.Rows[0].Cells[8].Text;

            db.SubmitChanges();

            GridView1.EditIndex = -1;

            binded();
        }
    }

我不明白为什么我在文本框中的更新数据为空。

1 个答案:

答案 0 :(得分:0)

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    if (Page.IsPostBack)
    {
        var ie =
        (from ac in db.ITdevicedetails where ac.IDdevice == "note01" select ac).FirstOrDefault();
        //this is my answer !!!
        TextBox xp = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].Controls[0]));
        ie.Comment = xp.Text;
        db.SubmitChanges();
        GridView1.EditIndex = -1;
        binded();
    }

}

现在好了我可以更新我的gridview,因为我忘了在gridview中提到文本框。

相关问题