网格视图由linq编辑和更新 [英] Grid view Edit and Update by linq

查看:67
本文介绍了网格视图由linq编辑和更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是网格视图,我想更新它

This is grid view and i want to update it

<asp:GridView ID="GridView1" runat="server" CellPadding="3"  OnRowCommand="GridView1_onrowcommand"

            GridLines="Horizontal" AutoGenerateColumns="False"

             OnRowCancelingEdit="gv1_RowCancelingEdit"

             OnRowDataBound="gv1_RowDataBound"

            OnRowEditing="gv1_RowEditing"

            OnRowUpdating="gv1_RowUpdating"

        Height="245px" Width="1088px" DataKeyNames="User_name" BackColor="White"

            BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px">
            <AlternatingRowStyle BackColor="#F7F7F7" />
            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
            <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
            <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />

            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
            <SortedAscendingCellStyle BackColor="#F4F4FD" />
            <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
            <SortedDescendingCellStyle BackColor="#D8D8F0" />
            <SortedDescendingHeaderStyle BackColor="#3E3277" />
            <Columns>

           <asp:TemplateField HeaderText="Delete">
           <ItemTemplate>
           <asp:Button ID="btn_delete" runat="server" Text=" Delete" CommandName="Delete" CommandArgument='<%#Eval("User_Name") %>' />

           </ItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="Activete Id" >

            <ItemTemplate>
           <asp:Button ID="btn_activate" runat="server" Text=" Activate" CommandName="update" CommandArgument='<%#Eval("User_Name") %>' />
            </ItemTemplate>

          </asp:TemplateField>

           <asp:TemplateField HeaderText="update">
          <EditItemTemplate>
         <asp:Button runat="server" Text="Udate" CommandName="updata" CommandArgument='<%#Eval("User_Name") %>' />

          </EditItemTemplate>
          <ItemTemplate>
          <asp:Button ID="btn_updata" runat="server" Text="Edit" CommandName="Edit" />
          </ItemTemplate>

          </asp:TemplateField>

           <asp:TemplateField HeaderText="Name">
            <ItemTemplate >

            <asp:Label ID="User_NameLabel" runat="server" Text='<%# Eval("User_Name") %>' />
            <br />
             </ItemTemplate>
           </asp:TemplateField>

           <asp:TemplateField HeaderText="Email_id:">
             <ItemTemplate>
              <asp:Label ID="Email_idLabel" runat="server" Text='<%# Eval("Email_id") %>' />
            <br />
             </ItemTemplate>
           </asp:TemplateField>


           <asp:TemplateField HeaderText=" Password:">
           <EditItemTemplate>

           <asp:TextBox ID="txt_pass" runat="server" Text='<%# Bind("Password") %>'></asp:TextBox>
           </EditItemTemplate>
            <ItemTemplate>

            <asp:Label ID="PasswordLabel" runat="server" Text='<%# Eval("Password") %>' />

             </ItemTemplate>
             <FooterTemplate>


             </FooterTemplate>
           </asp:TemplateField>


           <asp:TemplateField HeaderText="server_host:">
            <ItemTemplate>

            <asp:Label ID="server_hostLabel" runat="server" Text='<%# Eval("server_host") %>' />
            <br />
             </ItemTemplate>
           </asp:TemplateField>

           <asp:TemplateField HeaderText="Status:">
            <ItemTemplate>

            <asp:CheckBox runat="server" ID="CHB" Checked='<%# Eval("Status") %>' />

            <br />
             </ItemTemplate>
           </asp:TemplateField>

           </Columns>

        </asp:GridView>





这是代码背后的代码网格视图:





and this is code behind code for grid view:

protected void GridView1_onrowcommand(object sender, GridViewCommandEventArgs e)
       {

           if (e.CommandName.Equals("Activate"))
           {
               string user = Convert.ToString(e.CommandArgument.ToString());
               var objAct = from st in obj.sending_emails
                            where st.User_Name.Equals(user)
                            select st;
               foreach (var objActfor in objAct)
               {
                   objActfor.Status = true;
                   obj.SubmitChanges();

                   getdat();

               }
               var objdact = from st in obj.sending_emails
                             where st.Status.Equals(true) && st.User_Name != user

                             select st;
               foreach (var objdactf in objdact)
               {
                   objdactf.Status = false;
                   obj.SubmitChanges();
                   getdat();
               }



           }
           //if (e.CommandName.Equals("Deactivate"))
           //{
           //    int id = Convert.ToInt32(e.CommandArgument.ToString());
           //    var objAct = from st in objtps.Admin_smtptmails
           //                 where st.id.Equals(id)
           //                 select st;
           //    foreach (var objActfor in objAct)
           //    {
           //        objActfor.Active = false;
           //        objtps.SubmitChanges();
           //        FillGridview();

           //    }
           //}

       }

       protected void gv1_RowEditing(object sender, GridViewEditEventArgs e)
       {
           GridView1.EditIndex = e.NewEditIndex;
           getdat();


       }

       protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs e)
       {


           string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
           var getdata = from st in obj.sending_emails
                         where st.User_Name.Equals(user)
                         select st;
           foreach (var objT1 in getdata)
           {
               objT1.server_host = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_Server")).Text;
               objT1.Email_id = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_email")).Text;
               objT1.Password = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_pass")).Text;
               if (((CheckBox)GridView1.Rows[e.RowIndex].FindControl("cbgvact")).Checked.Equals(true))
               {
                   objT1.Status = true;
               }
               else
               {
                   objT1.Status = false;
               }
               obj.SubmitChanges();
           }
           GridView1.EditIndex = -1;
           getdat();
       }

       protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
       {

       }

       protected void gv1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
       {
           GridView1.EditIndex = -1;
           getdat();
       }
       protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e)
       {
           int ID = Convert.ToInt32( GridView1.DataKeys[e.RowIndex].Values[0].ToString());
           sending_email objadsmtp = obj.sending_emails.Where(a => a.User_Name.Equals(user)).SingleOrDefault();

           obj.sending_emails.DeleteOnSubmit(objadsmtp);
           obj.SubmitChanges();
           getdat();



       }





Its not working. Is there any solution to perform these operation on grid view..



Its not working. Is there any solution to perform these operation on grid view..

推荐答案

1. If you are getting specific errors, put that details

2. As general practice, do not do SubmitChanges for each row. Rather do it at end of all change batch at end.

3. Place a breakpoint at linq query to see if it actually returns elements to be modified. If it doesn’t, then the parameter being compared doesn’t exists. If it does, your update is not getting persisted.



Also by refactoring your code, you will be able to identify issues a lot faster and helps in better regression test. Too much code to look means the issue might be small but can get overlooked easily
1. If you are getting specific errors, put that details
2. As general practice, do not do SubmitChanges for each row. Rather do it at end of all change batch at end.
3. Place a breakpoint at linq query to see if it actually returns elements to be modified. If it doesn't, then the parameter being compared doesn't exists. If it does, your update is not getting persisted.

Also by refactoring your code, you will be able to identify issues a lot faster and helps in better regression test. Too much code to look means the issue might be small but can get overlooked easily


Google has atleast 1230000 results for this. Should we assume that none of them works for you?



https://www.google.co.in/search?q=gridview+update+asp.net&oq=gridview+update+asp.net&aqs=chrome..69i57j69i65l3j0l2.7091j0&sourceid=chrome&ie=UTF-8[^]
Google has atleast 1230000 results for this. Should we assume that none of them works for you?

https://www.google.co.in/search?q=gridview+update+asp.net&oq=gridview+update+asp.net&aqs=chrome..69i57j69i65l3j0l2.7091j0&sourceid=chrome&ie=UTF-8[^]


这篇关于网格视图由linq编辑和更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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