使用Linq to SQL更新GridView中的行 [英] Update row in GridView using Linq to SQL

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

问题描述

当我单击GridView行时,更新后将显示它们
它会更新gridview中的行.

When I click on the GridView row it shows them after I update
it update the row in gridview

推荐答案

请参阅此

Linq到SQL查询 [
see this

Linq to SQL Queries[^]



本示例将指导您.
Hi ,
This Example Will Guid you .
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!IsPostBack)
            {
                Getdata();
            }
        }
    }
    DataClassesDataContext db = new DataClassesDataContext();    //Data Context
    //Bind Method
    void Getdata() {
        var res = from x in db.cates
                  select x;
        GridView1.DataSource = res;
        GridView1.DataBind();
    }


    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

        GridView1.EditIndex = -1;
        Getdata();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int Id = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("Label2") as Label).Text);
        if (Id == 0)
        {
            return;
        }
        var result = (from x in db.cates where x.id == Id select x).FirstOrDefault();
        result.cate1 = (GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox).Text;

        db.SubmitChanges();
        GridView1.EditIndex = -1;
        Getdata();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int Id = Convert.ToInt32((GridView1.Rows[e.NewEditIndex].FindControl("Label2") as Label).Text);
        if (Id == 0)
        {
            return;
        }
        GridView1.EditIndex = e.NewEditIndex;
        Getdata();
    }





<div>

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

        onrowcancelingedit="GridView1_RowCancelingEdit"

        onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
        <Columns>
            <asp:TemplateField HeaderText="ID">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text="<%# Bind('cate1') %>"></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text="<%# Bind('cate1') %>"></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowEditButton="True" />
        </Columns>
    </asp:GridView>

</div>


这篇关于使用Linq to SQL更新GridView中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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