如何从网格箱中检索更新的数据并在数据库中更新它

时间:2023-01-29 16:57:39

My Code actually returns the data which it has previously got from database but am unable to get the updated data from the gridcontrol.

我的代码实际上返回它以前从数据库获得的数据,但无法从gridcontrol获取更新的数据。

This is my Default.aspx where designing has been done of gridview.

这是我的Default.aspx,其中已经完成了gridview的设计。

 <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
 </asp:Content>
 <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowupdating="GridView1_RowUpdating" onrowediting="GridView1_RowEditing"
    onrowupdated="GridView1_RowUpdated" onrowcommand="GridView1_RowCommand">
    <Columns>
        <asp:CommandField ShowEditButton="true" ShowDeleteButton="true" ShowInsertButton="true" />
        <asp:TemplateField HeaderText="Id" SortExpression="Id">
            <ItemTemplate>
                <asp:Label ID="lbl_Id" Text='<%# Bind("id")%>' runat="server">  </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name" SortExpression="Name">
            <ItemTemplate>
                <asp:Label ID="lbl_name" Text='<%# Bind("Name")%>' runat="server"></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txt_name" Text='<%# Bind("Name")%>' runat="server"></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Address" SortExpression="address">
            <ItemTemplate>
                <asp:Label ID="lbl_address" Text='<%# Bind("address")%>' runat="server"></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txt_address" Text='<%# Bind("address")%>'    runat="server"></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
   </asp:GridView>
   <asp:Label ID="lbl_msg" runat="server"></asp:Label>
   </asp:Content>



 My.cs File

My Code is row updating method in the below class. I have used entity framework as a method for having manipulations in the data base.

我的代码是以下类中的行更新方法。我已经使用实体框架作为在数据库中进行操作的方法。

 using System; 
 using System.Collections.Generic;
 using System.Linq;
  using System.Web;
  using System.Web.UI;
 using System.Web.UI.WebControls;
     using DatabaseModel;

   public partial class _Default : System.Web.UI.Page
   {
   DatabaseEntities obj;

   protected void Page_Load(object sender, EventArgs e)
  {
   obj = new DatabaseEntities();
   GridView1.DataSource = obj.Records.ToList();
  GridView1.DataBind();
  }
  string data;

  protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  {

  }

  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
   GridViewRow row = GridView1.Rows[e.RowIndex];
   TextBox name = row.FindControl("txt_name") as TextBox;
  TextBox address = row.FindControl("txt_address") as TextBox;
  Label id = row.FindControl("lbl_Id") as Label;
  int no = int.Parse(id.Text);
   Record rec = obj.Records.First(x => x.Id == no);
  rec.Name = name.Text;
  rec.Address = address.Text;
   obj.SaveChanges();
  data = name.Text;
   lbl_msg.Text = name.Text;
  }

  protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
  {
  }

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
 {
  }
 } 

2 个解决方案

#1


1  

You have some problem in linq query, use lambda expression and use this:

你在linq查询中有一些问题,使用lambda表达式并使用它:

protected void Page_Load(object sender, EventArgs e)
{
    // do not rebind the data on postback
    if (!IsPostBack)
    {
        obj = new DatabaseEntities();
        GridView1.DataSource = obj.Records.ToList();
        GridView1.DataBind();
    }
}

#2


0  

That is probably because data is rebound before GridView1_RowUpdating is triggered. Try to change the Page_Load method like this:

这可能是因为在触发GridView1_RowUpdating之前数据会反弹。尝试更改Page_Load方法,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    // do not rebind the data on postback
    if (!IsPostBack)
    {
        obj = new DatabaseEntities();
        GridView1.DataSource = obj.Records.ToList();
        GridView1.DataBind();
    }
}

#1


1  

You have some problem in linq query, use lambda expression and use this:

你在linq查询中有一些问题,使用lambda表达式并使用它:

protected void Page_Load(object sender, EventArgs e)
{
    // do not rebind the data on postback
    if (!IsPostBack)
    {
        obj = new DatabaseEntities();
        GridView1.DataSource = obj.Records.ToList();
        GridView1.DataBind();
    }
}

#2


0  

That is probably because data is rebound before GridView1_RowUpdating is triggered. Try to change the Page_Load method like this:

这可能是因为在触发GridView1_RowUpdating之前数据会反弹。尝试更改Page_Load方法,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    // do not rebind the data on postback
    if (!IsPostBack)
    {
        obj = new DatabaseEntities();
        GridView1.DataSource = obj.Records.ToList();
        GridView1.DataBind();
    }
}