Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
there is one dropdown and one button and two grid view. when i select dropdown value and than first grid will fill than i select radio button and click on apply button than second grid will be fill. problem is start now again i select drop down it will display first grid and select radio button and click on button and display only new record and old will remove so i want both


C#
protected void btnapply_Click(object sender, EventArgs e)
   {
          SelectModel();

   }

   public void SelectModel()
   {
       if (IsPostBack)
       {
           DataTable dt = new DataTable();
           dt.Columns.AddRange(new DataColumn[6] { new DataColumn("Software"), new DataColumn("id"), new DataColumn("module"), new DataColumn("rdoread"), new DataColumn("rdoreadmodify"), new DataColumn("all") });
           foreach (GridViewRow row in Gridmodule.Rows)
           {
               if (row.RowType == DataControlRowType.DataRow)
               {
                   CheckBox chkRead = (row.Cells[0].FindControl("rdoread") as CheckBox);
                   CheckBox chkReadModify = (row.Cells[0].FindControl("rdoreadmodify") as CheckBox);
                   CheckBox chkAll = (row.Cells[0].FindControl("rdoall") as CheckBox);

                   if (chkRead.Checked)
                   {
                       string id = (row.Cells[1].FindControl("lblid") as Label).Text;
                       string module = (row.Cells[2].FindControl("lblmodule") as Label).Text;
                       bool read = true;
                       bool readmodify = false;
                       bool all = false;
                       string soft = (drpsoftware.SelectedItem.ToString());

                       dt.Rows.Add(soft, id, module, read, readmodify, all);
                   }

                   if (chkReadModify.Checked)
                   {
                       string id = (row.Cells[1].FindControl("lblid") as Label).Text;
                       string module = (row.Cells[2].FindControl("lblmodule") as Label).Text;
                       bool read = false;
                       bool readmodify = true;
                       bool all = false;
                       string soft = (drpsoftware.SelectedItem.ToString());

                       dt.Rows.Add(soft, id, module, read, readmodify, all);
                   }

                   if (chkAll.Checked)
                   {
                       string id = (row.Cells[1].FindControl("lblid") as Label).Text;
                       string module = (row.Cells[2].FindControl("lblmodule") as Label).Text;
                       bool read = false;
                       bool readmodify = false;
                       bool all = true;
                       string soft = (drpsoftware.SelectedItem.ToString());

                       dt.Rows.Add(soft, id, module, read, readmodify, all);
                   }

               }
           }
           gridselected.DataSource = dt;
           gridselected.DataBind();
       }

i want that when i press first time this button it fill grid and when i press again the same button it never erase previous data and fill new data with old .

What I have tried:

i try viewstat but can no solve the issue.
Posted
Updated 12-May-16 5:56am
v2
Comments
Karthik_Mahalingam 12-May-16 10:44am    
second time you are not checking any checkbox i guess.
Kumbhani Bhavesh 12-May-16 10:48am    
no . check it and press on button than it will replace new data place with old.
Karthik_Mahalingam 12-May-16 10:50am    
ya it will refresh the data, what do you need exactly??
you want new data to be displayed ?
Karthik_Mahalingam 12-May-16 10:51am    
Always use Reply button, to post Comments/query to the user, else the User wont get notified.
Kumbhani Bhavesh 12-May-16 11:10am    
i want both data new and old

1 solution

Refer this simple example and implement in your code..

ASPX:

ASP.NET
<asp:GridView ID="Gridmodule" runat="server">
            <Columns>
                 <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox  ID="chk" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox Text="" ID="txtSoftware" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

        <asp:GridView ID="gridselected" runat="server">
            <Columns>
                <asp:BoundField DataField="Software" HeaderText="Software" ItemStyle-Width="50" />
            </Columns> 
        </asp:GridView> 

        <asp:Button ID="btnapply" runat="server" Text="Apply" OnClick="btnapply_Click" />



CS

C#
protected void Page_Load(object sender, EventArgs e)
     {
         if (!IsPostBack)
         {
             DataTable dt = new DataTable();
             dt.Columns.Add("SomeColumn");
             dt.Rows.Add("one");
             dt.Rows.Add("two");
             Gridmodule.DataSource = dt;
             Gridmodule.DataBind();
         }
     }
     protected void btnapply_Click(object sender, EventArgs e)
     {
         DataTable dt = new DataTable();
         dt.Columns.Add("Software");
         foreach (GridViewRow row in Gridmodule.Rows)
         {
             if (row.RowType == DataControlRowType.DataRow)
             {
                 bool isChecked = (row.FindControl("chk") as CheckBox).Checked;
                 if (isChecked)
                 {
                     string a = (row.FindControl("txtSoftware") as TextBox).Text;
                     dt.Rows.Add(a);
                 }
             }
         }

         // Adding the Second grid data to datatable
         foreach (GridViewRow row in gridselected.Rows)
             if (row.RowType == DataControlRowType.DataRow)
             {
                 string software = row.Cells[0].Text;
                 // similarly for all columns
                 dt.Rows.Add(software);
             }


         gridselected.DataSource = dt;
         gridselected.DataBind(); ;
     }
 
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