Click here to Skip to main content
14,362,507 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am having following code how can preserve check box state oon paging in grid view and how can i find the check box checked gridRow to save on save button click please review my code as well and let me know my code quality as well. Thanks in advance.
This is the aspx page gridview code

asp:GridView id="GridView1" AllowSorting="true" AutoGenerateColumns="false" runat="server" AllowPaging="True" DataKeyNames="branch_id"   OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="2">
<Columns>
<asp:TemplateField HeaderText="Select Student">
<ItemTemplate>
<asp:CheckBox id="Chek"  runat="server" Text="select" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Enrollment No." DataField="enrollment_no" />
<asp:BoundField HeaderText="Course Name" DataField="course_name"/>
<asp:BoundField  HeaderText="Branch Name"  DataField="branch_name"/>
<<asp:BoundField  HeaderText="Mobile" DataField="mobile"/>
<asp:BoundField HeaderText="Name"  DataField="first_name"/>
<asp:BoundField  HeaderText="Surname" DataField="last_name" />
</Columns>

</asp:GridView>




this is the ok button to fill the datagrid
protected void Btnok_Click(object sender, EventArgs e)
   {

       try
       {
           if (Page.IsValid)
           {
               int passoutYear = Convert.ToInt32(passout.SelectedValue);
               int courseList = Convert.ToInt32(course.SelectedValue);
               StringBuilder sb = new StringBuilder();
               string sep = "";




               foreach (ListItem li in Branch.Items)
               {
                   if (li.Selected)
                   {
                       sb.Append(sep + li.Value);
                       sep = ",";
                   }
               }

               DataTable dt = placedStudentManager.GetPlacedStudentList(sb, passoutYear, courseList);
               if (dt != null && dt.Rows.Count != 0)
               {
                   GridView1.DataSource = dt;
                   GridView1.DataBind();
                   GridView1.Visible = true;
                   Btnsave.Visible = true;
                   ViewState["dt"] = dt;
               }
               else
               {
                   noRecordExistOnOkButton.Text = "No Record Exist To Be Inserted For Recruited Students";
                   Btnsave.Visible = false;
                   GridView1.Visible = false;
               }
           }
       }
       catch (Exception ex)
       {
           COMMON.logger.Error("Error On Button Btnok_Click:CompanySelected.aspx.cs ", ex);
       }
   }

it's the save button
protected void Btnsave_Click(object sender, EventArgs e)
    {

        try
        {
            if (Page.IsValid)
            {
                int passoutYear = Convert.ToInt32(passout.SelectedValue);
                int companyId = Convert.ToInt32(company.SelectedValue);
                int courseId = Convert.ToInt32(course.SelectedValue);
                string package = txtpackage.Text.Trim();
                string venu = txtvenue.Text.Trim();

                DateTimeFormatInfo dateInfo = new DateTimeFormatInfo();
                dateInfo.ShortDatePattern = "dd/MM/yyyy";
                DateTime date = Convert.ToDateTime(vistDate.Text.Trim(), dateInfo);

                DataTable gridviewTbl = new DataTable();
                DataColumn dc1 = new DataColumn("company_id");
                DataColumn dc2 = new DataColumn("course_id");
                DataColumn dc3 = new DataColumn("branch_id");
                DataColumn dc4 = new DataColumn("student_enrollment");
                DataColumn dc5 = new DataColumn("company_visit_date");
                DataColumn dc6 = new DataColumn("venu");
                DataColumn dc7 = new DataColumn("package");
                DataColumn dc8 = new DataColumn("passout_year");
                gridviewTbl.Columns.Add(dc1);
                gridviewTbl.Columns.Add(dc2);
                gridviewTbl.Columns.Add(dc3);
                gridviewTbl.Columns.Add(dc4);
                gridviewTbl.Columns.Add(dc5);
                gridviewTbl.Columns.Add(dc6);
                gridviewTbl.Columns.Add(dc7);
                gridviewTbl.Columns.Add(dc8);

                if (GridView1 != null && GridView1.Rows.Count != 0)
                {
                    int rowCount = GridView1.Rows.Count;
                    int i = 0;
                    foreach (GridViewRow row in GridView1.Rows)
                    {

                        CheckBox cb = (CheckBox)row.FindControl("Chek");

                        if (cb != null && cb.Checked)
                        {
                            string enrollmetNo = GridView1.Rows[i].Cells[1].Text.ToString();
                            int branchId = Convert.ToInt32(GridView1.DataKeys[0].Value);
                            gridviewTbl.Rows.Add(companyId, courseId, branchId, enrollmetNo, date, venu, package, passoutYear);
                        }
                        i++;
                    }

                    if (gridviewTbl.Rows.Count != 0)
                    {
                        Boolean b = placedStudentManager.SaveSelectdStudent(gridviewTbl);
                        gridNotExist.Text = "Records Successfully inserted";
                    }

                    else
                    {
                        gridNotExist.Text = "Please check the record to be inserted";
                        Btnsave.Visible = true;
                        GridView1.Visible = true;
                    }
                }
                else
                {
                    gridNotExist.Text = "Please Select The Recruited Student, so click the ok button first ";
                }

            }
            else 
            {
                GridView1.Visible = true;
                Btnsave.Visible = true;
            }
        }
        catch (Exception ex)
        {
            COMMON.logger.Error("Error On Button Btnsave_Click:CompanySelected.aspx.cs ", ex);
        }

    }

now pagging event

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       GridView1.PageIndex = e.NewPageIndex;
       GridView1.DataSource = (DataTable)ViewState["dt"];
       GridView1.DataBind();
       GridView1.Visible = true;
       Btnsave.Visible = true;
       // StringBuilder str=(StringBuilder)ViewState["chk"];
       //foreach (GridViewRow row in GridView1.Rows)
       //{
       //}


}
Posted
Updated 30-Aug-10 7:38am
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Perhaps you need to review this http://msdn.microsoft.com/en-us/library/aa479347.aspx[^]

Also, in your save method why are you creating a new DataTable and populating it from the grid? You have the grid bound to a DataTable already.
   
Comments
rupesh007 30-Aug-10 14:07pm
   
i am to save that new populated data table to data base that's why.
Ia my code is poor do u suggest some other solutions.
[no name] 30-Aug-10 14:57pm
   
You already have a datatable, update it rather than create a new one.
Have you even looked at the resource that was given?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100