Click here to Skip to main content
15,877,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am taking a grid-view with two text-box control and one checkboxlist control. While I am adding a new row in grid-view dynamically, then the two text-box contain the value in the previous row but the items in checkboxlist control not displayed in previous row.I want to maintain the state of checkboxlist in the previous row. How to do this? Can anyone give any suggestion?

i am writing FirstGridViewRow() in page_load..

 private void FirstGridViewRow()
     DataTable dt = new DataTable();
     DataRow dr = null;
     dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
     dt.Columns.Add(new DataColumn("Col1", typeof(string)));
     dt.Columns.Add(new DataColumn("Col2", typeof(string)));
     dt.Columns.Add(new DataColumn("Col3", typeof(string)));
     dt.Columns.Add(new DataColumn("Col4", typeof(string)));
     dt.Columns.Add(new DataColumn("Col5", typeof(string)));
     dr = dt.NewRow();
     dr["RowNumber"] = 1;
     dr["Col1"] = string.Empty;
     dr["Col2"] = string.Empty;
     dr["Col3"] = string.Empty;
     dr["Col4"] = string.Empty;
     dr["Col5"] = string.Empty;

     ViewState["CurrentTable"] = dt;

     grvStudentDetails.DataSource = dt;

private void ButtonAdd_Click()
         int rowIndex = 0;

         if (ViewState["CurrentTable"] != null)
             DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
             DataRow drCurrentRow = null;
             if (dtCurrentTable.Rows.Count > 0)
                 for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                     TextBox actionitemname =
                     TextBox actionduedate =
                     CheckBoxList assignee = (CheckBoxList)grvStudentDetails.Rows[rowIndex].Cells[5].FindControl("chkAssignees");
                     drCurrentRow = dtCurrentTable.NewRow();
                     drCurrentRow["RowNumber"] = i + 1;

                     dtCurrentTable.Rows[i - 1]["Col1"] = actionitemname.Text;
                     dtCurrentTable.Rows[i - 1]["Col2"] = actionduedate.Text;
                     //dtCurrentTable.Rows[i - 1]["Col5"] = assignee.SelectedItem.Text;
                 ViewState["CurrentTable"] = dtCurrentTable;
                 grvStudentDetails.DataSource = dtCurrentTable;
             Response.Write("ViewState is null");

 private void SetPreviousData()
     int rowIndex = 0;
     if (ViewState["CurrentTable"] != null)
         DataTable dt = (DataTable)ViewState["CurrentTable"];
         if (dt.Rows.Count > 0)
             for (int i = 0; i < dt.Rows.Count; i++)
                 TextBox TextBoxName =(TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("txtName");
                 TextBox TextBoxAge = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[2].FindControl("txtAge");
                 TextBoxName.Text = dt.Rows[i]["Col1"].ToString();
                 TextBoxAge.Text = dt.Rows[i]["Col2"].ToString();

Html gridview code
<asp:GridView ID="grvStudentDetails" runat="server" ShowFooter="True" AutoGenerateColumns="False"
                  CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="grvStudentDetails_RowDeleting"
                  OnRowDataBound="grvStudentDetails_RowDataBound" OnSelectedIndexChanged="grvStudentDetails_SelectedIndexChanged">
                  <AlternatingRowStyle BackColor="White" />
                      <asp:BoundField DataField="RowNumber" HeaderText="SNo" />
                      <asp:TemplateField HeaderText="Action Item">
                              <asp:TextBox ID="txtName" TextMode="MultiLine" Width="250px" runat="server" Placeholder="Type Action Item" style="overflow:auto"></asp:TextBox>
                      <asp:TemplateField HeaderText="Due Date">
                              <asp:TextBox ID="txtAge" runat="server" Placeholder="Select Date" Width="130px"></asp:TextBox>
                              <cc1:CalendarExtender ID="DuedateCalender"  runat="server" Format="dd/MM/yyyy" PopupPosition="BottomRight"
                      <asp:TemplateField HeaderText="Network">
                              <asp:DropDownList ID="ddlseconnetwrklist" runat="server" Width="130px" Height="20px" OnDataBound="ddlseconnetwrklist_DataBound"
                                  OnSelectedIndexChanged="ddlseconnetwrklist_SelectedIndexChanged" AutoPostBack="true">
                      <asp:TemplateField >
                              <asp:ImageButton ID="gvntwemps" runat="server" Height="20px" ImageUrl="~/Images/select user.png"
                                  Style="margin-top: 2px;" ValidationGroup="null" OnClick="gvntwemps_Click" ToolTip="Employee in the network" />
                           <asp:TemplateField HeaderText="Assignees">
                           <div style=" overflow:auto; height:50px; width:266px">
                                  <asp:CheckBoxList ID="chkAssignees" Width="249px" runat="server" Height="27px">
                          <FooterStyle HorizontalAlign="Right" />
                              <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" ValidationGroup="null"
                                  OnClick="ButtonAdd_Click" />
                                  <asp:Button runat="server" ID="assigntask" Text="Assign Task" OnClick="assigntask_Click" />
                  <EditRowStyle BackColor="#2461BF" />
                  <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                  <RowStyle BackColor="#EFF3FB" />
                  <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                  <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                  <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                  <SortedAscendingCellStyle BackColor="#F5F7FB" />
                  <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                  <SortedDescendingCellStyle BackColor="#E9EBEF" />
                  <SortedDescendingHeaderStyle BackColor="#4870BE" />
Updated 3-Dec-14 19:52pm
syed shanu 4-Dec-14 1:03am    
Your question is not clear can you give more details .And where do you call FirstGridViewRow().In this method you do create column and row.i think this is wrong .Hope you need to create separate method to create column for datatable and then create separate method to add rows to data table.
januskarthik 4-Dec-14 1:13am    
i have updated my question shanu.. please review it
I cannot see any code for a CheckBoxList.
januskarthik 4-Dec-14 1:19am    
i don't know how to code for checkbox list. that is my doubt
Inside which Event, you want to do? Also post the GridView Markup.

1 solution

Inside Button Click Event, you are getting the CheckBoxList as...
CheckBoxList assignee = (CheckBoxList)grvStudentDetails.Rows[rowIndex].Cells[5].FindControl("chkAssignees");

Now get all the checked ones and store their item index separated by comma or something in one column like...
foreach (ListItem item in assignee.Items)
  if (item.Selected)
    // Here store the item.Index or item.Value in a StringBuilder or array or something.

// Now store the whole <code>string </code>in a column like <code>col5 </code>or something.

Then while restoring the data, retrieve from that column and split the string to get the index or values and check the CheckBoxes matching with those.

Please get started on this. You will explore many things. :)
Share this answer

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900