locked
How to show gridview 1 data in gridview2 RRS feed

  • Question

  • User2033107836 posted

    Hello

    This is my Gridview1 data 

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource5" AllowPaging="True" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal" CellSpacing="10" OnRowDataBound="GridView2_RowDataBound">
                        <Columns>
                        <asp:TemplateField>
                        <ItemTemplate>
                        <asp:CheckBox ID="chk1" runat="server" />
                        </ItemTemplate>
                        </asp:TemplateField>
                            <asp:TemplateField HeaderText="Arrival Date" SortExpression="Arrival_Date">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind(Record_Details") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Record_Details") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>                       
                        </Columns>
                    </asp:GridView>

    Output look like this

    Now i will select second and third row in gridview1

    i want show in Textbox1 

    11-Sep-2017,15-Sep-2017 

    In Textbox2 

    880,1035

    how to do so using asp.net 2.0 C#

    Tuesday, October 31, 2017 6:19 PM

Answers

  • User-1838255255 posted

    Hi asp.ambur,

    According to your description and needs, please check the following sample code:

    Sample Code:

    <head runat="server">
        <title></title>
        <style type="text/css">
            body {
                font-family: Arial;
                font-size: 10pt;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
                AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkRow" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Arrival Date" ItemStyle-Width="250">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Record_Details") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <br />
            <asp:Button ID="btnGetSelected" runat="server" Text="Get selected records" OnClick="GetSelectedRecords" />
            <hr />
            <u>Selected Rows</u>
            <br />
            <asp:GridView ID="gvSelected" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
                AutoGenerateColumns="false">
                <Columns>
                    <asp:BoundField DataField="date" HeaderText="date" ItemStyle-Width="150" />
                    <asp:BoundField DataField="price" HeaderText="price" ItemStyle-Width="150" />
                </Columns>
            </asp:GridView>
        </form>
    </body>
    
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Record_Details", typeof(string));
                dt.Rows.Add(DateTime.Now.ToString() + ",850");
                dt.Rows.Add(DateTime.Now.ToString() + ",880");
                dt.Rows.Add(DateTime.Now.ToString() + ",1035");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
        protected void GetSelectedRecords(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[2] { new DataColumn("date"), new DataColumn("price") });
            var date = "";
            var price = "";
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
    
                    CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
                    if (chkRow.Checked)
                    {
                        string Label1 = (row.Cells[1].FindControl("Label1") as Label).Text;
                        var array = Label1.Split(',');
                        date += array[0] + ",";
                        price += array[1]+",";
                    }
                }
            }
            dt.Rows.Add(date, price);
            gvSelected.DataSource = dt;
            gvSelected.DataBind();
        }

    Result:

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 1, 2017 7:55 AM
  • User-1838255255 posted

    Hi asp.ambur,

    According to your description and needs, I make a modify based on my sample, please check:

    Sample Code:

    <head runat="server">
        <title></title>
        <style type="text/css">
            body {
                font-family: Arial;
                font-size: 10pt;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
                AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkRow" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Arrival Date" ItemStyle-Width="250">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Record_Details") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <br />
            <asp:Button ID="btnGetSelected" runat="server" Text="Get selected records" OnClick="GetSelectedRecords" />
            <hr />
            <u>Selected Rows</u>
            <br />
            <asp:TextBox ID="Date" runat="server"></asp:TextBox>
            <asp:TextBox ID="Price" runat="server"></asp:TextBox><br />
            <br />
            <asp:GridView ID="gvSelected" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
                AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField HeaderText="date" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("date") %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="price" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("price") %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </form>
    </body>
    
     protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Record_Details", typeof(string));
                dt.Rows.Add(DateTime.Now.ToString() + ",850");
                dt.Rows.Add(DateTime.Now.ToString() + ",880");
                dt.Rows.Add(DateTime.Now.ToString() + ",1035");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
        protected void GetSelectedRecords(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[2] { new DataColumn("date"), new DataColumn("price") });
            var date = "";
            var price = "";
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
    
                    CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
                    if (chkRow.Checked)
                    {
                        string Label1 = (row.Cells[1].FindControl("Label1") as Label).Text;
                        var array = Label1.Split(',');
                        date += array[0] + ",";
                        price += array[1] + ",";
                    }
                }
            }
    
            string newdate = date.Remove(date.Length - 1);
            string newprice = price.Remove(price.Length - 1);
            dt.Rows.Add(newdate, newprice);
            gvSelected.DataSource = dt;
            gvSelected.DataBind();
    
            Date.Text=newdate;
            Price.Text = newprice;
        }

    Result:

    Best Regards,

    Eric Du 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 2, 2017 10:56 AM

All replies

  • User-1838255255 posted

    Hi asp.ambur,

    According to your description and needs, please check the following sample code:

    Sample Code:

    <head runat="server">
        <title></title>
        <style type="text/css">
            body {
                font-family: Arial;
                font-size: 10pt;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
                AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkRow" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Arrival Date" ItemStyle-Width="250">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Record_Details") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <br />
            <asp:Button ID="btnGetSelected" runat="server" Text="Get selected records" OnClick="GetSelectedRecords" />
            <hr />
            <u>Selected Rows</u>
            <br />
            <asp:GridView ID="gvSelected" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
                AutoGenerateColumns="false">
                <Columns>
                    <asp:BoundField DataField="date" HeaderText="date" ItemStyle-Width="150" />
                    <asp:BoundField DataField="price" HeaderText="price" ItemStyle-Width="150" />
                </Columns>
            </asp:GridView>
        </form>
    </body>
    
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Record_Details", typeof(string));
                dt.Rows.Add(DateTime.Now.ToString() + ",850");
                dt.Rows.Add(DateTime.Now.ToString() + ",880");
                dt.Rows.Add(DateTime.Now.ToString() + ",1035");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
        protected void GetSelectedRecords(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[2] { new DataColumn("date"), new DataColumn("price") });
            var date = "";
            var price = "";
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
    
                    CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
                    if (chkRow.Checked)
                    {
                        string Label1 = (row.Cells[1].FindControl("Label1") as Label).Text;
                        var array = Label1.Split(',');
                        date += array[0] + ",";
                        price += array[1]+",";
                    }
                }
            }
            dt.Rows.Add(date, price);
            gvSelected.DataSource = dt;
            gvSelected.DataBind();
        }

    Result:

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 1, 2017 7:55 AM
  • User2033107836 posted

    Hello Eric

    Thanks for your code

    1) Can you bind that same in textbox

    2) why in last it showing , it has to show like this 880,1035

    Thank You

    Wednesday, November 1, 2017 8:10 AM
  • User-1838255255 posted

    Hi asp.ambur,

    According to your description and needs, I make a modify based on my sample, please check:

    Sample Code:

    <head runat="server">
        <title></title>
        <style type="text/css">
            body {
                font-family: Arial;
                font-size: 10pt;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
                AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkRow" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Arrival Date" ItemStyle-Width="250">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Record_Details") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <br />
            <asp:Button ID="btnGetSelected" runat="server" Text="Get selected records" OnClick="GetSelectedRecords" />
            <hr />
            <u>Selected Rows</u>
            <br />
            <asp:TextBox ID="Date" runat="server"></asp:TextBox>
            <asp:TextBox ID="Price" runat="server"></asp:TextBox><br />
            <br />
            <asp:GridView ID="gvSelected" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
                AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField HeaderText="date" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("date") %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="price" ItemStyle-Width="150">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("price") %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </form>
    </body>
    
     protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Record_Details", typeof(string));
                dt.Rows.Add(DateTime.Now.ToString() + ",850");
                dt.Rows.Add(DateTime.Now.ToString() + ",880");
                dt.Rows.Add(DateTime.Now.ToString() + ",1035");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
        protected void GetSelectedRecords(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[2] { new DataColumn("date"), new DataColumn("price") });
            var date = "";
            var price = "";
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
    
                    CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
                    if (chkRow.Checked)
                    {
                        string Label1 = (row.Cells[1].FindControl("Label1") as Label).Text;
                        var array = Label1.Split(',');
                        date += array[0] + ",";
                        price += array[1] + ",";
                    }
                }
            }
    
            string newdate = date.Remove(date.Length - 1);
            string newprice = price.Remove(price.Length - 1);
            dt.Rows.Add(newdate, newprice);
            gvSelected.DataSource = dt;
            gvSelected.DataBind();
    
            Date.Text=newdate;
            Price.Text = newprice;
        }

    Result:

    Best Regards,

    Eric Du 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 2, 2017 10:56 AM