locked
gridview copy to another gridview RRS feed

  • Question

  • User197255166 posted
    Hi everyone. I have two gridview. When I selected checkbox I want to copy to gridview another gridview. How can I do this.
    <asp:GridView ID="example" CssClass="table table-striped table-bordered table-hover" AutoGenerateColumns="false" DataKeyNames="ID" runat="server" ClientIDMode="Static" OnPreRender="example_PreRender" OnRowEditing="example_RowEditing" OnRowCancelingEdit="example_RowCancelingEdit" OnRowDeleting="example_RowDeleting" OnRowDataBound="example_RowDataBound">
     
                                                <columns>
     
                                                    <asp:TemplateField HeaderText="Ürün Seç" ItemStyle-Width="50">
     
                                                        <itemtemplate>
                                                           
                                                                <asp:CheckBox ID="chcsec" CssClass="form-control" runat="server" />
     
    
                                                          
                                                        
                                                    
     
                                                    <asp:TemplateField HeaderText="Ürün Ad" ItemStyle-Width="100">
                                                        <itemtemplate>
                                                            <asp:Label ID="lblUrun" runat="server" Text='<%# Eval("UrunAd") %>'>
                                                        
                                                        <edititemtemplate>
                                                            <asp:TextBox ID="txturunad" runat="server" Text='<%# Eval("UrunAd") %>'>
                                                        
                                                    
     
    
                                                    <asp:TemplateField HeaderText="Stok" ItemStyle-Width="50">
                                                        <itemtemplate>
                                                            <asp:Label ID="lblStok" Style="padding: 3px 5px;" runat="server" Text='<%# Eval("Stok") %>'>
                                                        
                                                        <edititemtemplate>
                                                            <asp:TextBox ID="txtstok" Style="padding: 3px 5px;" runat="server" Text='<%# Eval("Stok") %>'>
                                                        
                                                    
     
                                                    <asp:TemplateField HeaderText="Fiyat" ItemStyle-Width="50">
                                                        <itemtemplate>
                                                            <asp:Label ID="txtfiyat" Style="padding: 3px 5px;" CssClass="form-control" runat="server">500
                                                        
                                                        <edititemtemplate>
                                                            <asp:TextBox ID="txtfiyat" Style="padding: 3px 5px;" runat="server" CssClass="form-control">
                                                        
                                                    
     
                                                    <asp:TemplateField HeaderText="Adet" ItemStyle-Width="50">
                                                       <itemtemplate>
                                                            <asp:Label ID="txtAdet" Style="padding: 3px 5px;" CssClass="form-control" runat="server">
                                                        
                                                        <edititemtemplate>
                                                            <asp:TextBox ID="txtAdet" runat="server" Style="padding: 3px 5px;" CssClass="form-control">
                                                        
                                                    
     
    
     
                                                    <asp:TemplateField HeaderText="Eleman Seç" ItemStyle-Width="100">
     
                                                        <itemtemplate>
                                                            <asp:DropDownList ID="drpEleman" CssClass="form-control" runat="server">
                                                        
     
    
                                                    
     
                                                    <asp:TemplateField HeaderText="Eleman Adet" ItemStyle-Width="50">
                                                        <itemtemplate>
                                                            <asp:Label ID="txtElemanAdet" Style="padding: 3px 5px;" CssClass="form-control" runat="server">
                                                        
                                                        <edititemtemplate>
                                                            <asp:TextBox ID="txtElemanAdet" runat="server" Style="padding: 3px 5px;" CssClass="form-control">
                                                        
                                                    
     
                                                    <asp:TemplateField HeaderText="Eleman Fiyat" ItemStyle-Width="50">
                                                        <itemtemplate>
                                                            <asp:Label ID="txtElemanFiyat" Style="padding: 3px 5px;" CssClass="form-control" runat="server">
                                                        
                                                        <edititemtemplate>
                                                            <asp:TextBox ID="txtElemanFiyat" runat="server" Style="padding: 3px 5px;" CssClass="form-control">
                                                        
                                                    
     
                                             
     
                                                    <asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150" />

    Monday, November 21, 2016 9:45 PM

All replies

  • User-707554951 posted

    Hi Aliyilmaz,

    Based on you need, I suggest you could use OnCheckedChanged event of CheckBox. You need to iterate row in gridview.

    I make an simple, it work well, you could refer to it:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="true"/>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="CategoryID">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%#Eval("CategoryID")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField HeaderText="CategoryName" DataField="CategoryName"/>
                </Columns>
            </asp:GridView>
            <hr />
            <asp:GridView ID="GridView2" runat="server"></asp:GridView>
    
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    
      protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    DataTable dt = GetProfile();
                    GridView1.DataSource = dt;
                    ViewState["DataTable"] = dt;
                    GridView1.DataBind();
                }
            }
            public DataTable GetProfile()
            {
                string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("select * from Categories"))
                    {
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            using (DataTable dt = new DataTable())
                            {
                                sda.Fill(dt);
                                return dt;
    
                            }
                        }
                    }
                }
            }
            protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
            {
    
                DataTable dt = new DataTable();
                dt.Columns.Add("CategoryID", typeof(int));
                dt.Columns.Add("CategoryName", typeof(string));
    
                foreach (GridViewRow rw in GridView1.Rows)
                {
                    if (rw.RowType == DataControlRowType.DataRow)
                    {
                        CheckBox chk = (CheckBox)rw.Cells[0].FindControl("CheckBox1");
                        if (chk.Checked) { 
                            Label lb = (Label)rw.Cells[1].FindControl("Label1");
                        int id = Convert.ToInt32(lb.Text.ToString());
                        string name = rw.Cells[2].Text.ToString();
                        dt.Rows.Add(id, name);
                        }
                    }         
                }
                GridView2.DataSource = dt;
                GridView2.DataBind();        
            }

    Output screenshot as below:

    Best regards

    Cathy

    Tuesday, November 22, 2016 8:17 AM
  • User197255166 posted

    Thank you but I have problem. I selected dropdown category and fill gridview. Then before filled gridview data lost.  How can I do this. My html code shared.

    Tuesday, November 22, 2016 9:33 AM
  • User-707554951 posted

    Hi Aliyilmaz,

    From your html code, I couldn't find  correct reason for your problem. However, from your description, I supposed that you forgot to rebind gridview in selectedchange event of dropdownlist. So, check this part of code in dropdownlist's selectedchange event.

    After that if you still have problem about this, would you please provide us with code about how you do databind to gridview and the code in dropdownlist's selectedchange event.?

    Best regards

    Cathy

    Wednesday, November 23, 2016 1:44 AM