Friday, April 3, 2015

Bulk edit and update in asp.net Gridview

Bulk edit and update in asp.net gridview
Here I am going to explain how to bulk edit update multiple rows or records in asp.net gridview using checkboxes on single click and c# sharp


ASP.NET 


<asp:GridView ID="gvcallingstatus" runat="server" AutoGenerateColumns="False" CssClass="GridViewStyle"
                                  DataKeyNames="ID" AllowPaging="True" PageSize="12"
                                  onrowdatabound="gvcallingstatus_RowDataBound"
                                  onpageindexchanging="gvcallingstatus_PageIndexChanging"
                                  DataSourceID="sqlgvdatabind">
                        <pagerstyle backcolor="#336666" forecolor="White" horizontalalign="Center" />
                        <selectedrowstyle backcolor="#339966" font-bold="True" forecolor="White" />
                        <headerstyle backcolor="#336666" font-bold="True" forecolor="White" />
                        <alternatingrowstyle cssclass="AltRowStyle" />
                        <headerstyle wrap="false" />
                        <columns>

                            <asp:templatefield>
                                <itemtemplate>
                                    <asp:CheckBox ID="chkedit" runat="server" AutoPostBack="true" OnCheckedChanged="ChkEdit_Checked" EnableViewState="true" />
                                </itemtemplate>
                            </asp:templatefield>

                            <asp:templatefield headertext="ID">
                                <itemtemplate>
                                    <asp:TextBox ID="txtid" runat="server" ReadOnly="true" BackColor="Transparent" BorderStyle="None" Width="50px" Text='<%# Bind("ID") %>' CssClass="GVItemStyle"></asp:TextBox>
                                </itemtemplate>
                            </asp:templatefield>

                            <asp:templatefield headertext="Status">
                                <itemtemplate>
                                    <asp:TextBox ID="txtremarks" runat="server" ReadOnly="true" BackColor="Transparent" BorderStyle="None" Text='<%# Bind("Remarks") %>' CssClass="GVItemStyle"></asp:TextBox>
                                    <asp:DropDownList ID="ddlremarks" runat="server" AppendDataBoundItems="true" Visible="false" ToolTip='<%# Bind("Region") %>' AutoPostBack="true" OnSelectedIndexChanged="ddlremarks_SelectedIndexChanged">
                                        <asp:listitem selected="True"></asp:listitem>
                                        <asp:listitem>Busy / Engaged</asp:listitem>
                                        <asp:listitem>Call Later</asp:listitem>
                                    </asp:DropDownList>
                                </itemtemplate>
                                <itemstyle wrap="False" />
                            </asp:templatefield>


                            <asp:templatefield headertext="TC">
                                <itemtemplate>
                                    <asp:TextBox ID="txttc" runat="server" ReadOnly="true" BackColor="Transparent" BorderStyle="None" Text='<%# Bind("TC") %>' Width="100%" CssClass="GVItemStyle"></asp:TextBox>
                                    <asp:DropDownList ID="ddltc" runat="server" Visible="false" Width="150px" AppendDataBoundItems="true" EnableViewState="true">
                                    </asp:DropDownList>
                                </itemtemplate>
                                <itemstyle width="10%" />
                            </asp:templatefield>

                            <asp:templatefield headertext="Rejection Reason">
                                <itemtemplate>
                                    <asp:TextBox ID="txtrr" runat="server" Height="20px" ReadOnly="true" TextMode="MultiLine" BackColor="Transparent" BorderStyle="None" Text='<%# Bind("RReason") %>' Width="100%" CssClass="GVItemStyle"></asp:TextBox>
                                    <asp:DropDownList ID="ddlrr" runat="server" Visible="false" Width="150px" AppendDataBoundItems="true" EnableViewState="true">
                                        <asp:listitem selected="True"></asp:listitem>
                                        <asp:listitem>Call were not recorded</asp:listitem>
                                        <asp:listitem>Incomplete call</asp:listitem>
                                        <asp:listitem>Language Problem</asp:listitem>
                                        <asp:listitem>Permission not taken regarding recording of calls</asp:listitem>
                                        <asp:listitem>Ratings not explained properly</asp:listitem>
                                        <asp:listitem>Some questions skipped</asp:listitem>
                                    </asp:DropDownList>
                                </itemtemplate>
                                <itemstyle width="20%" />
                            </asp:templatefield>



                        </columns>
                        <editrowstyle cssclass="EditRowStyle" />
                        <headerstyle cssclass="HeaderStyle" />
                        <pagerstyle cssclass="PagerStyle" />
                        <rowstyle cssclass="RowStyle" />
                        <selectedrowstyle cssclass="SelectedRowStyle" />

                    </asp:GridView>

C# SHARP


protected void Btnchkall_Click(object sender, EventArgs e)
    {
        if (btnchkall.Text.ToString() == "Check All")
        {
            for (int i = 0; i < gvcallingstatus.Rows.Count; i++)
            {
                CheckBox chkedit = (CheckBox)gvcallingstatus.Rows[i].Cells[0].FindControl("chkedit");
                chkedit.Checked = true;
                TextBox txtremarks = (TextBox)gvcallingstatus.Rows[i].FindControl("txtremarks");
                TextBox txttc = (TextBox)gvcallingstatus.Rows[i].FindControl("txttc");
                DropDownList ddlremarks = (DropDownList)gvcallingstatus.Rows[i].FindControl("ddlremarks");
                DropDownList ddltc = (DropDownList)gvcallingstatus.Rows[i].FindControl("ddltc");

                txtremarks.Visible = false;
                ddlremarks.Visible = true;
                txttc.Visible = false;
                ddltc.Visible = true;

                //Telecalelr Name
                using (SqlConnection TCCon = new SqlConnection(QualitySQL))
                {
                    ViewState["DDLTC"] = ddltc.SelectedValue.ToString();

                    TCCon.Close();
                    TCCon.Open();

                    //Telecaller name data bindings
                    string SQLTC = "Select * from TC where Branch = '" + Session["Zones"].ToString() + "'";
                    SqlCommand CMDTC = new SqlCommand(SQLTC, TCCon);
                    ddltc.Items.Clear();
                    ddltc.DataSource = CMDTC.ExecuteReader();
                    ddltc.DataTextField = "Telecaller";
                    ddltc.DataValueField = "Telecaller";
                    ddltc.DataBind();
                    ddltc.Items.Insert(0, new ListItem("", ""));
                    //End of telecaller name data bindings

                    TCCon.Close();
                }

                btnchkall.Text = "Clear All";
            }
        }
        else if (btnchkall.Text.ToString() == "Clear All")
        {
            for (int i = 0; i < gvcallingstatus.Rows.Count; i++)
            {
                CheckBox chkedit = (CheckBox)gvcallingstatus.Rows[i].Cells[0].FindControl("chkedit");
                chkedit.Checked = false;
                TextBox txtremarks = (TextBox)gvcallingstatus.Rows[i].FindControl("txtremarks");
                TextBox txttc = (TextBox)gvcallingstatus.Rows[i].FindControl("txttc");
                DropDownList ddlremarks = (DropDownList)gvcallingstatus.Rows[i].FindControl("ddlremarks");
                DropDownList ddltc = (DropDownList)gvcallingstatus.Rows[i].FindControl("ddltc");

                txtremarks.Visible = true;
                ddlremarks.Visible = false;

                txttc.Visible = true;
                ddltc.Visible = false;

                btnchkall.Text = "Check All";
            }
        }
    }



protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        foreach(GridViewRow row in gvcallingstatus.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                bool chkedit = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
                if (chkedit != null)
                {
                    if (chkedit)
                    {
                        DropDownList ddlremarks = (DropDownList)row.FindControl("ddlremarks");
                        DropDownList ddltc = (DropDownList)row.FindControl("ddltc");

                        DateTime dateval = DateTime.Now;

                        SqlConnection DBCon = new SqlConnection(QualitySQL);
                        SqlCommand DBCmd = new SqlCommand();

                        DBCon.Close();
                        DBCon.Open();

                        //Remarks and Telecaller name updating
                        if (ddlremarks.SelectedValue.ToString() != "" & ddlremarks.SelectedValue.ToString() != "Rejected" & ddlremarks.SelectedValue.ToString() != "Call Later")
                        {
                            if (ddltc.SelectedValue.ToString() != "")
                            {
                                double id = Convert.ToDouble(((TextBox)row.FindControl("txtid")).Text.ToString());
                               
                                DBCmd.Connection = DBCon;
                                DBCmd.CommandText = "Update JDPDB SET Remarks=@Remarks, TC=@TC, RReason=@RReason, RemarksDate=@RemarksDate where ID = " + id;
                                DBCmd.CommandType = CommandType.Text;

                                DBCmd.Parameters.Add(new SqlParameter("@Remarks", row.Cells[11].Controls.OfType<DropDownList>().FirstOrDefault().SelectedValue.ToString()));
                                DBCmd.Parameters.Add(new SqlParameter("@TC", row.Cells[13].Controls.OfType<DropDownList>().FirstOrDefault().SelectedValue.ToString()));
                                DBCmd.Parameters.Add(new SqlParameter("@RReason", ""));
                                DBCmd.Parameters.Add(new SqlParameter("@RemarksDate", dateval));

                                DBCmd.ExecuteNonQuery();
                                gvcallingstatus.DataBind();
                                btnchkall.Text = "Check All";
                            }
                            else { ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('Telecaller Name can not be blank');", true); ddltc.Focus(); }
                        }
                       
                        DBCon.Close();
                    }
                }
            }
        }
    }



No comments:

Post a Comment