Update Checked (Selected) GridView Rows based on Id using C# in ASP.Net

Last Reply one month ago By pandeyism

Posted one month ago

Here I want update record with Id in GridView

<asp:GridView ID="gvData" runat="server" AutoGenerateColumns="False" ShowFooter="True"
    Width="100%" Font-Size="25px" HorizontalAlign="Center" CellPadding="4" GridLines="None">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:TemplateField HeaderText="Select Farmer">
            <ItemTemplate>
                <asp:CheckBox ID="chk" runat="server" />
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer Id">
            <ItemTemplate>
                <asp:Label ID="Id" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer Name">
            <ItemTemplate>
                <asp:Label ID="Name" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer Email">
            <ItemTemplate>
                <asp:Label ID="Email" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer State">
            <ItemTemplate>
                <asp:Label ID="State" runat="server" Text='<%# Eval("State") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer District">
            <ItemTemplate>
                <asp:Label ID="District" runat="server" Text='<%# Eval("District") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer Address">
            <ItemTemplate>
                <asp:Label ID="Address" runat="server" Text='<%# Eval("Address") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer Contact">
            <ItemTemplate>
                <asp:Label ID="Contact" runat="server" Text='<%# Eval("Contact") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Company Potato Quantity" Visible="false">
            <ItemTemplate>
                <asp:Label ID="PQuantity" runat="server" Text='<%# Eval("PQuantity") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer Quantity">
            <ItemTemplate>
                <asp:Label ID="Quantity" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer Total">
            <ItemTemplate>
                <asp:Label ID="Total" runat="server" Text='<%# Eval("Total") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<center>
    <asp:Button ID="Button1" runat="server" Text="Add" ForeColor="Red" OnClick="Button1_Click"
        Font-Size="20px" CssClass="bg-primary" Width="100px" Height="50px" />
</center>

 

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in gvData.Rows)
    {
        CheckBox chkRow = (row.Cells[0].FindControl("chk") as CheckBox);
        if (chkRow.Checked)
        {
            SqlCommand cmd2 = new SqlCommand();
            cmd2.Connection = con;
            con.Open();
            try
            {
                SqlCommand cmd = new SqlCommand("Update AddRaw_199 set status = 1 where Id= + '" + (row.FindControl("Id") as Label).Text + "'");
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                int suc = cmd.ExecuteNonQuery();
            }
            catch
            {
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
        }
    }
    lbl_app.CssClass = "text-center text-success";
    lbl_app.Text = "Status Updated Successfully";    
}

 

Posted one month ago

Hi VinitaAkha,

Refer below code.

HTML

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $('[id*=chk]').on('change', function () {
            var value = 0;
            $('[id*=chk]:checked').each(function () {
                var row = $(this).closest('tr');
                value = value + parseInt(row.find('[id*=Total]').html());
            });
            $("[id*=gvData] [id*=lblGrandTotal]").html(value);
            $("[id*=gvSelected] [id*=lblGrandTotal]").html(value);

        });
    });    
</script>
<asp:Label ID="lbl_app" runat="server"></asp:Label>
<asp:GridView ID="gvData" runat="server" AutoGenerateColumns="False" ShowFooter="True"
    GridLines="None">
    <Columns>
        <asp:TemplateField HeaderText="Select Farmer">
            <ItemTemplate>
                <asp:CheckBox ID="chk" runat="server" AutoPostBack="true" OnCheckedChanged="Transfer" />
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer Id">
            <ItemTemplate>
                <asp:Label ID="Id" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer Name">
            <ItemTemplate>
                <asp:Label ID="Name" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer Email">
            <ItemTemplate>
                <asp:Label ID="Email" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer State">
            <ItemTemplate>
                <asp:Label ID="State" runat="server" Text='<%# Eval("State") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer District">
            <ItemTemplate>
                <asp:Label ID="District" runat="server" Text='<%# Eval("District") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer Address">
            <ItemTemplate>
                <asp:Label ID="Address" runat="server" Text='<%# Eval("Address") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer Contact">
            <ItemTemplate>
                <asp:Label ID="Contact" runat="server" Text='<%# Eval("Contact") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Company Potato Quantity" Visible="false">
            <ItemTemplate>
                <asp:Label ID="PQuantity" runat="server" Text='<%# Eval("PQuantity") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer Quantity">
            <ItemTemplate>
                <asp:Label ID="Quantity" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Farmer Total">
            <ItemTemplate>
                <asp:Label ID="Total" runat="server" Text='<%# Eval("Total") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<center>
    <asp:Button ID="Button1" runat="server" Text="Add" ForeColor="Red" OnClick="Button1_Click"
        Font-Size="20px" CssClass="bg-primary" Width="100px" Height="50px" />
</center>
<hr />
<center>
    <u style="font-size: 30px">Selected Rows</u></center>
<br />
<asp:GridView ID="gvSelected" runat="server" ShowFooter="true" GridLines="None" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="State" HeaderText="State" />
        <asp:BoundField DataField="District" HeaderText="District" />
        <asp:BoundField DataField="Address" HeaderText="Address" />
        <asp:BoundField DataField="Email" HeaderText="Email" />
        <asp:BoundField DataField="Contact" HeaderText="Contact" />
        <asp:BoundField DataField="Quantity" HeaderText="Quantity" />
        <asp:BoundField DataField="PQuantity" HeaderText="Potato  Quantity" />
        <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label ID="Total" runat="server" Text='<%# Eval("Total") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                Total :
                <asp:Label ID="lblGrandTotal" runat="server" />
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Namespaces

using System.Data;
using System.Data.SqlClient;

Code

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] {
                        new DataColumn("Id", typeof(string)),
                        new DataColumn("Role", typeof(string)),
                        new DataColumn("Email", typeof(string)),
                        new DataColumn("Contact",typeof(string)),
                        new DataColumn("State", typeof(string)),
                        new DataColumn("District", typeof(string)),
                        new DataColumn("Address",typeof(string)),
                        new DataColumn("Name", typeof(string)),
                        new DataColumn("Quantity", typeof(int)),
                        new DataColumn("PQuantity",typeof(int)),
                        new DataColumn("Total",typeof(int))});
        dt.Rows.Add(1, "Role 1", "Email 1", "Contact 1", "State 1", "District 1", "Address 1", "Name 1", 10, 5, 50);
        dt.Rows.Add(2, "Role 2", "Email 2", "Contact 2", "State 2", "District 2", "Address 2", "Name 2", 20, 5, 100);
        dt.Rows.Add(3, "Role 3", "Email 3", "Contact 3", "State 3", "District 3", "Address 3", "Name 3", 15, 5, 75);
        dt.Rows.Add(4, "Role 4", "Email 4", "Contact 4", "State 4", "District 4", "Address 4", "Name 4", 12, 5, 60);
        gvData.DataSource = dt;
        gvData.DataBind();
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection();
    SqlCommand cmd;
    foreach (GridViewRow row in gvData.Rows)
    {
        CheckBox chkRow = (row.Cells[0].FindControl("chk") as CheckBox);
        if (chkRow.Checked)
        {
            con.Open();
            try
            {
                int id = Convert.ToInt16((row.FindControl("Id") as Label).Text);
                cmd = new SqlCommand("UPDATE AddRaw_199 SET status = 1 WHERE Id= @id", con);
                cmd.Parameters.AddWithValue("@id", id);
                int suc = cmd.ExecuteNonQuery();
            }
            catch
            {
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
        }
    }
    lbl_app.CssClass = "text-center text-success";
    lbl_app.Text = "Status Updated Successfully";
}

protected void Transfer(object sender, EventArgs e)
{
    decimal total = 0;
    try
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[9] {
        new DataColumn("Name"),
        new DataColumn("Quantity"),
        new DataColumn("Contact"),
        new DataColumn("Email"),
        new DataColumn("State"),
        new DataColumn("District"),
        new DataColumn("Address"),
        new DataColumn("PQuantity"),
        new DataColumn("Total") });
        foreach (GridViewRow row in gvData.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRow = (row.Cells[0].FindControl("chk") as CheckBox);
                if (chkRow.Checked)
                {
                    string Name = (row.Cells[1].FindControl("Name") as Label).Text;
                    string Quantity = (row.Cells[2].FindControl("Quantity") as Label).Text;
                    string Contact = (row.Cells[3].FindControl("Contact") as Label).Text;
                    string Email = (row.Cells[4].FindControl("Email") as Label).Text;
                    string State = (row.Cells[5].FindControl("State") as Label).Text;
                    string District = (row.Cells[6].FindControl("District") as Label).Text;
                    string Address = (row.Cells[7].FindControl("Address") as Label).Text;
                    string PQuantity = (row.Cells[8].FindControl("PQuantity") as Label).Text;
                    string Total = (row.Cells[9].FindControl("Total") as Label).Text;
                    total += Convert.ToDecimal(Total);
                    dt.Rows.Add(Name, Quantity, Contact, Email, State, District, Address, PQuantity, Total);
                }
            }
        }
        gvSelected.DataSource = dt;
        gvSelected.DataBind();
    }
    catch (Exception ex)
    {
        Response.Write(ex);
    }
}