Tuesday, November 30, 2021

Grid edit and update facility in asp.net C#

 https://www.c-sharpcorner.com/UploadFile/1e050f/edit-and-update-record-in-gridview-in-Asp-Net/


or


<asp:GridView ID="grd_Batch" AutoGenerateColumns="false" runat="server" DataKeyNames="Batch_id"

            AllowPaging="true" PageSize="20" PagerSettings-Position="Bottom" PagerSettings-PageButtonCount="10"

            Width="100%" CssClass="table table-bordered table-hover table-stripped table-condensed table-responsive"

            OnRowEditing="grd_Batch_RowEditing" OnRowCancelingEdit="grd_Batch_RowCancelingEdit" OnRowDataBound="grd_Batch_RowDataBound"

            OnRowUpdating="grd_Batch_RowUpdating" OnRowDeleting="grd_Batch_RowDeleting" OnRowCommand="grd_Batch_RowCommand">

<Columns>

<asp:TemplateField HeaderText="EDIT">

                    <ItemTemplate>

                        <asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" CssClass="btn btn-primary" />

                        &nbsp;

                        <%--  <asp:Button ID="btn_Delete" runat="server" OnClientClick="return confirm('Are you sure to delete this record ?')" Text="Delete" CommandName="Delete" CssClass="btn btn-danger" />

           <asp:Button ID="btn_sucess" runat="server" OnClientClick="return confirm('Are you sure to Extend this batch ?')" Text="Extend" CommandName="Extend" CssClass="btn btn-success" />--%>

                    </ItemTemplate>

                    <EditItemTemplate>

                        <asp:Button ID="btn_update" ValidationGroup="a" runat="server" Text="Update" OnClientClick="return confirm('Are you sure to update this record ?');"

                            CommandName="Update" CssClass="btn btn-primary" />

                        <asp:Button ID="bhtn_cancel" runat="server" Text="Cancel" CommandName="Cancel" CssClass="btn btn-danger" />

                    </EditItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Extend">

                    <ItemTemplate>

                        <asp:LinkButton ID="lnkButton" runat="server" Text="Extend" CommandArgument='<%#Eval("Training_Partner_id") + "," + Eval("Certifying_Agency_name") + "," + Eval("Course_name")  + "," + Eval("Batch_Start_date") + "," + Eval("Batch_End_Date")+ "," + Eval("batchLocation") %>'

                            CommandName="modal" CssClass="btn btn-success btn-sm"></asp:LinkButton>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Delete">

                    <ItemTemplate>

                        <asp:Button ID="btn_Delete" runat="server" OnClientClick="return confirm('Are you sure to delete this record ?')"

                            Text="Delete" CommandName="Delete" CssClass="btn btn-danger" />

                    </ItemTemplate>

                </asp:TemplateField>

</Columns>

</asp:gridview>


--------------------------

C# code

protected void grd_Batch_RowEditing(object sender, GridViewEditEventArgs e)

    {

        grd_Batch.EditIndex = -1;

        grd_Batch.EditIndex = e.NewEditIndex;

        drp_batch_SelectedIndexChanged(null, null);

//bind();

    }

    protected void grd_Batch_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

    {

        grd_Batch.EditIndex = -1;


        drp_batch_SelectedIndexChanged(null, null);

        lbl_error.Text = string.Empty;

        lbl_error.Visible = false;

//bind()  use grid databind

    }


protected void grd_Batch_RowUpdating(object sender, GridViewUpdateEventArgs e)

    {

        if (ViewState["CSRF"] != null && Convert.ToString(ViewState["CSRF"]) == Convert.ToString(Session["randomno"]))

        {

            int? result = null;

            int batchid = 0;

            try

            {

                batchid = (int)(grd_Batch.DataKeys[e.RowIndex].Value);


                var batchSdate = grd_Batch.Rows[e.RowIndex].FindControl("txt_batchStartDate") as TextBox;

                var BatchEndDate = grd_Batch.Rows[e.RowIndex].FindControl("txt_batchendtDate") as TextBox;

                //New Code Add District on 24082021 

                var Districtid = grd_Batch.Rows[e.RowIndex].FindControl("ddl_district") as DropDownList;

                var purposedExamDate = grd_Batch.Rows[e.RowIndex].FindControl("txt_PurposeExamtDate") as TextBox;

                var ScheduleExamDate = grd_Batch.Rows[e.RowIndex].FindControl("txt_ScheduleDate") as TextBox;

                //New Code Add on 14052020

                var SecondTimeAppearScheduledDate = grd_Batch.Rows[e.RowIndex].FindControl("txt_ScheduleDate2") as TextBox;

                var ThirdTimeAppearScheduledDate = grd_Batch.Rows[e.RowIndex].FindControl("txt_ScheduleDate3") as TextBox;

                var batchCreationDate = grd_Batch.Rows[e.RowIndex].FindControl("txt_BatchCreation") as TextBox;

                var MaxBatchSize = grd_Batch.Rows[e.RowIndex].FindControl("txt_batchsize") as TextBox;


                DateTime? purxamdate = null;

                DateTime? schedulexamdate = null;

                //New Code Add on 14052020

                DateTime? secondtimeappearscheduleddate = null;

                DateTime? thirdtimeappearscheduleddate = null;

                DateTime? bSdate = null;

                DateTime? bEdate = null;

                DateTime? BcreateDate = null;


//USP_CA_SCHEDULE_EXAM_UPDATE_BATCH

int index = e.RowIndex;

                GridViewRow row = (GridViewRow)grd_Batch.Rows[index];

                FileUpload fld_AttendanceUpload = (FileUpload)row.FindControl("fld_AttendanceUpload");

                string Upload_Attendance_Sheet_Document;

                string ustring = Guid.NewGuid().ToString().Replace("-", "") + DateTime.Now.Ticks.ToString();


                //Attendance Sheet Upload

                if (fld_AttendanceUpload.HasFile)

                {

                    string filename1 = Path.GetFileNameWithoutExtension(fld_AttendanceUpload.FileName);

                    string extension1 = Path.GetExtension(fld_AttendanceUpload.FileName);

                    Upload_Attendance_Sheet_Document = filename1 + "_" + ustring + extension1;

                    fld_AttendanceUpload.SaveAs(Server.MapPath("~\\View\\TrainingPartner\\StudentData\\Attendance\\" + Upload_Attendance_Sheet_Document));

                    

                    UpdateAttendance(batchid, Upload_Attendance_Sheet_Document);

                    lbl_error.Visible = true;

                    lbl_error.ForeColor = System.Drawing.Color.Green;

                    lbl_error.Text = "Attendance Uploaded successfully";

                }

                else

                {

                    return;

                }

}


reference:- https://www.c-sharpcorner.com/UploadFile/9f0ae2/gridview-edit-delete-and-update-in-Asp-Net/

No comments:

Post a Comment