locked
only gridview first row updated not apply update operation in other rows? RRS feed

  • Question

  • User-1026236167 posted

    Hello sir i want to simple update the data using the button click  here i used button to update data Button2_Click and i want to use data key named tbl_id which i get the data using int tableId = Convert.ToInt32(Gv2.DataKeys[row.RowIndex].Values[0].ToString()); i know row index  does not contain definition of event args so i want to get the data using this code now the code is run but the updation operation performs on 1st row of gridview not all records of gridview so where is my problem in code am using entity framework 6.0  

    so please give the answer on my code base?

    here is my code

    cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.Entity.Infrastructure;

    namespace WebApplication14
    {
    public partial class WebForm67 : System.Web.UI.Page
    {


    public static int RowIndex;
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!this.IsPostBack)
    {
    this.BindGrid();
    this.BindGrid1();
    //fillgridview2();
    }
    }

    private void BindGrid()
    {
    using (hospitalEntities ctx = new hospitalEntities())
    {
    Gv2.DataSource = (from hospitalEntities in ctx.manns
    select hospitalEntities).ToList();
    Gv2.DataBind();
    }
    }

    private void BindGrid1()
    {
    using (hospitalEntities ctx = new hospitalEntities())
    {
    dd3.DataSource = (from hospitalEntities in ctx.manns
    select hospitalEntities).ToList();
    dd3.DataBind();
    }
    }

    public void fillgridview2()
    {
    using (hospitalEntities ctx = new hospitalEntities())
    {

    var x = (from hospitalEntities in ctx.manns
    select hospitalEntities).ToList();

    for (int i = 0; i < Gv2.Rows.Count; i++)
    {
    try
    {
    ((DropDownList)Gv2.Rows[i].FindControl("dd31")).DataSource = x;
    ((DropDownList)Gv2.Rows[i].FindControl("dd31")).DataBind();

    }
    catch { }

    }
    }


    }

    void Clear()
    {
    //tb_benefited.Text = tb_allowance.Text = "";
    //Button1.Text = "save";
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    using (hospitalEntities ctx = new hospitalEntities())
    {

    ctx.manns.Add(new mann()
    {
    benefited_leaves1 = dd3.SelectedItem.ToString(),
    basic_allowance1 = tb_allowance.Text


    });

    ctx.SaveChanges();
    BindGrid1();
    //showdata();
    //Clear();


    }

    }

    //public void studentsGrid_UpdateItem(int tbl_id, int benefit_leaves1)
    //{
    // using (hospitalEntities ctx = new hospitalEntities())
    // {
    // mann item = null;
    // item = ctx.manns.Find(tbl_id);
    // item = ctx.manns.Find(benefit_leaves1);
    // if (item == null)
    // {
    // ModelState.AddModelError("",
    // String.Format("Item with id {0} was not found", tbl_id),
    // String.Format("Item with id {0} was not found", benefit_leaves1)
    // );

    // return;
    // }

    // TryUpdateModel(item);
    // if (ModelState.IsValid)
    // {
    // ctx.SaveChanges();
    // }
    // }
    //}


    protected void Button2_Click(object sender, EventArgs e)
    {
    GridViewRow row = Gv2.Rows[RowIndex];
    int tableId = Convert.ToInt32(Gv2.DataKeys[row.RowIndex].Values[0].ToString());


    using (hospitalEntities ctx1 = new hospitalEntities())
    {


    mann customer = (from c in ctx1.manns
    where c.tbl_id == tableId
    select c).FirstOrDefault();
    customer.benefited_leaves1 = dd3.SelectedItem.ToString();
    customer.basic_allowance1 = tb_allowance.Text;

    ctx1.SaveChanges();


    }

    }


    protected void Gv2_RowEditing(object sender, GridViewEditEventArgs e)
    {
    Gv2.EditIndex = e.NewEditIndex;
    RowIndex = e.NewEditIndex;
    this.BindGrid();
    fillgridview2();

    }

    protected void Gv2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
    Gv2.EditIndex = -1;
    this.BindGrid();
    fillgridview2();
    }

    protected void Gv2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    GridViewRow row = Gv2.Rows[e.RowIndex];
    int tableId = Convert.ToInt32(Gv2.DataKeys[e.RowIndex].Values[0]);
    string benefited_leaves = (row.FindControl("dd31") as DropDownList).SelectedItem.ToString();
    string basicallowance = (row.FindControl("txtCountry") as TextBox).Text;
    using (hospitalEntities ctx = new hospitalEntities())
    {
    mann customer = (from c in ctx.manns
    where c.tbl_id == tableId
    select c).FirstOrDefault();
    customer.benefited_leaves1 = benefited_leaves;
    customer.basic_allowance1 = basicallowance;
    ctx.SaveChanges();
    }
    Gv2.EditIndex = -1;
    fillgridview2();
    this.BindGrid();
    }

    protected void Gv2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    int tableId = Convert.ToInt32(Gv2.DataKeys[e.RowIndex].Values[0]);
    using (hospitalEntities ctx = new hospitalEntities())
    {
    mann customer = (from c in ctx.manns
    where c.tbl_id == tableId
    select c).FirstOrDefault();
    ctx.Entry(customer).State = System.Data.Entity.EntityState.Deleted;
    ctx.SaveChanges();
    }
    this.BindGrid();
    }

    }
    }

    aspx

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm67.aspx.cs" Inherits="WebApplication14.WebForm67" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="contentbody" runat="server">



    <asp:DropDownList DataTextField="benefited_leaves1" DataValueField="benefited_leaves1" ID="dd3" runat="server" ></asp:DropDownList>
    <asp:TextBox ID="tb_allowance" runat="server" ></asp:TextBox>

    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />

    <asp:GridView runat="server" ID="Gv2" DataKeyNames="tbl_id"

    CellPadding="4" ForeColor="#333333" Width="90%" AutoGenerateColumns="false" >
    <AlternatingRowStyle BackColor="White" />
    <Columns>


    <asp:TemplateField HeaderText="benefited leaves" ItemStyle-Width="150">
    <ItemTemplate>
    <asp:Label ID="lblName" runat="server" Text='<%# Eval("benefited_leaves1") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <%-- <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("benefited_leaves1") %>'></asp:TextBox>--%>

    <asp:DropDownList runat="server" ID="dd31" DataTextField="benefited_leaves1" DataValueField="benefited_leaves1"></asp:DropDownList>

    </EditItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Basic Allowance" ItemStyle-Width="150">
    <ItemTemplate>
    <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("basic_allowance1") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("basic_allowance1") %>'></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField Visible="false">
    <ItemTemplate>
    <asp:Label runat="server" ID="txt_id" Text='<%#Eval("tbl_id")%>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>

    <asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150"/>



    </Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>

    </asp:Content>

    Monday, June 22, 2020 7:44 AM

All replies

  • User-939850651 posted

    Hi prabhjot1313,

    What do you mean that the other rows do not apply update operations?

    Was there an error when clicked the update button? Or did nothing change when the update button was clicked?

    Regarding your issues, I found a similar case, I am not sure if this is your current situation, please read Only first row of datagridview updating c#.

    On the other hand, I think it will be helpful for you to learn to use debugging tools to view the running process of the program.

    And a guess, try to bind the data source to gridview again in button click event.

    Best regards,

    Xudong Peng

    Tuesday, June 23, 2020 7:44 AM