使用Linq在gridview中插入,更新删除? [英] Insert,Update Delete in gridview using Linq?

查看:65
本文介绍了使用Linq在gridview中插入,更新删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在Details.aspx.cs中的代码隐藏文件



This is my code behind file in Details.aspx.cs

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

public partial class Details : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    Data_Acess da = new Data_Acess();
    Register rg=new Register();
    protected void Page_Load(object sender, EventArgs e)
    {

        Display();
     
    }
    private void Display()
    {
        var query = from c in db.Registers where c.Name == Convert.ToString(Session["Name"]) select c;
        GridView1.DataSource = query;
        GridView1.DataBind();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Display();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = (int)GridView1.DataKeys[e.RowIndex].Value;
     
  
        TextBox name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
        TextBox mobile = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
        TextBox address = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");
        TextBox city = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4");
        TextBox gender = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5");
        TextBox email = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox6");
        TextBox dateofbirth = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox7");

        Register reg = (from c in db.Registers where c.Id == id select c).FirstOrDefault();
        reg.Name = name.Text;
        reg.Mobile = Convert.ToInt32(mobile.Text);
        reg.Address = address.Text;
        reg.City = city.Text;
        reg.Gender = gender.Text;
        reg.Email = email.Text;
        reg.Dateofbirth = dateofbirth.Text;
        db.SubmitChanges();



        GridView1.EditIndex = -1;
        GridView1.DataBind();
        

    }





我无法更新数据,我正在使用visual studio 2012 pls帮助我

这是我的HTML代码的详细信息.Aspx页面





I am unable to update data, i am using visual studio 2012 pls help me with that
this is my HTML code for details.Aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Details.aspx.cs" Inherits="Details" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 136px;
        }
        .auto-style2 {
            width: 1048px;
            text-align: center;
        }
        .auto-style3 {
            width: 136px;
            height: 43px;
        }
        .auto-style4 {
            width: 1048px;
            text-align: center;
            height: 43px;
            font-size: x-large;
        }
        .auto-style5 {
            height: 43px;
        }
        .auto-style6 {
            width: 136px;
            height: 37px;
        }
        .auto-style7 {
            width: 1048px;
            text-align: center;
            height: 37px;
        }
        .auto-style8 {
            height: 37px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table style="width:100%;">
            <tr>
                <td class="auto-style3"></td>
                <td class="auto-style4">Details Page</td>
                <td class="auto-style5"></td>
            </tr>
            <tr>
                <td class="auto-style1"> </td>
                <td class="auto-style2">
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" style="text-align: center" Width="1031px" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="Id" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" >
                        <Columns>
                            <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
                            <asp:TemplateField HeaderText="Name" SortExpression="Name">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Mobile" SortExpression="Mobile">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Mobile") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Mobile") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Address" SortExpression="Address">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Address") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="City" SortExpression="City">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("City") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Gender" SortExpression="Gender">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Gender") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("Gender") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Email" SortExpression="Email">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label6" runat="server" Text='<%# Bind("Email") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Date Of Birth" SortExpression="Dateofbirth">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("Dateofbirth") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label7" runat="server" Text='<%# Bind("Dateofbirth") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <FooterStyle BackColor="#CCCCCC" />
                        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
                        <RowStyle BackColor="White" />
                        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                        <SortedAscendingCellStyle BackColor="#F1F1F1" />
                        <SortedAscendingHeaderStyle BackColor="#808080" />
                        <SortedDescendingCellStyle BackColor="#CAC9C9" />
                        <SortedDescendingHeaderStyle BackColor="#383838" />
                    </asp:GridView>
                </td>
                <td> </td>
            </tr>
            <tr>
                <td class="auto-style6"></td>
                <td class="auto-style7"></td>
                <td class="auto-style8"></td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>

推荐答案

Hi,



From your codes I can see that your LINQ select statement can return you read-only object collection in an anoymous class collection, So this has nothing to do with original databases. Maybe in my mind I think you can use EntityFramework DataSource to bind to the GridView, and then do modifications.



In your query you are not updating the values before saving the changes. Use the following line before you submit the changes.



Hi,

From your codes I can see that your LINQ select statement can return you read-only object collection in an anoymous class collection, So this has nothing to do with original databases. Maybe in my mind I think you can use EntityFramework DataSource to bind to the GridView, and then do modifications.

In your query you are not updating the values before saving the changes. Use the following line before you submit the changes.

Db.Registers.InsertOnSubmit(reg);





Thanks

Sisir Patro



Thanks
Sisir Patro


这篇关于使用Linq在gridview中插入,更新删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆