locked
Show values in gridview ,Datalist in asp.net RRS feed

  • Question

  • User345697035 posted
    Hi
    I had two values inside foreach loop I.e
    for each(GridViewRow grow in GridViewdetails.Rows)
    {
    string dd=grow.cells[4].Text
    string cc=grow.cells[5].Text
    }
    What I want is on button click these two value should show in another gridview or datalist
    Wednesday, December 7, 2016 5:29 PM

Answers

  • User2103319870 posted

    What I want is on button click these two value should show in another gridview or datalist

    You can try with the below implemenation

    HTML

     <asp:GridView ID="GridView2" CellPadding="5" runat="server" ShowFooter="True" AutoGenerateColumns="false"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:TemplateField HeaderText="IsApproved">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Select</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="AddressID" HeaderText="AddressID" ItemStyle-Width="150px" />
                <asp:BoundField DataField="AddressLine1" HeaderText="AddressLine1" ItemStyle-Width="100px" />
                <asp:BoundField DataField="StateProvinceID" HeaderText="StateProvinceID" ItemStyle-Width="100px" />
                <asp:BoundField DataField="City" HeaderText="City" HtmlEncode="false"
                    ItemStyle-Width="100px" />
            </Columns>
            <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
            SelectCommand="SELECT top 5 [AddressID], [AddressLine1], [AddressLine2], [City], [StateProvinceID] FROM  Person.Address a"></asp:SqlDataSource>
        <%--Second gridview to show selected row--%>
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
     /// <summary>
            /// Handles the Click event of the LinkButton1 control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
            protected void LinkButton1_Click(object sender, EventArgs e)
            {
                //Get the selected GridView row
                GridViewRow row = (GridViewRow)((LinkButton)sender).NamingContainer;
                DataTable dt = new DataTable("sample");
                dt.Columns.Add("AddressID");
                dt.Columns.Add("AddressLine1");
                DataRow dataRow;
                dataRow = dt.NewRow();
                //Change the column index here
                dataRow[0] = row.Cells[1].Text;
                dataRow[1] = row.Cells[2].Text;
                dt.Rows.Add(dataRow);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 7, 2016 6:36 PM

All replies

  • User2103319870 posted

    What I want is on button click these two value should show in another gridview or datalist

    You can try with the below implemenation

    HTML

     <asp:GridView ID="GridView2" CellPadding="5" runat="server" ShowFooter="True" AutoGenerateColumns="false"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:TemplateField HeaderText="IsApproved">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Select</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="AddressID" HeaderText="AddressID" ItemStyle-Width="150px" />
                <asp:BoundField DataField="AddressLine1" HeaderText="AddressLine1" ItemStyle-Width="100px" />
                <asp:BoundField DataField="StateProvinceID" HeaderText="StateProvinceID" ItemStyle-Width="100px" />
                <asp:BoundField DataField="City" HeaderText="City" HtmlEncode="false"
                    ItemStyle-Width="100px" />
            </Columns>
            <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
            SelectCommand="SELECT top 5 [AddressID], [AddressLine1], [AddressLine2], [City], [StateProvinceID] FROM  Person.Address a"></asp:SqlDataSource>
        <%--Second gridview to show selected row--%>
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
     /// <summary>
            /// Handles the Click event of the LinkButton1 control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
            protected void LinkButton1_Click(object sender, EventArgs e)
            {
                //Get the selected GridView row
                GridViewRow row = (GridViewRow)((LinkButton)sender).NamingContainer;
                DataTable dt = new DataTable("sample");
                dt.Columns.Add("AddressID");
                dt.Columns.Add("AddressLine1");
                DataRow dataRow;
                dataRow = dt.NewRow();
                //Change the column index here
                dataRow[0] = row.Cells[1].Text;
                dataRow[1] = row.Cells[2].Text;
                dt.Rows.Add(dataRow);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 7, 2016 6:36 PM
  • User345697035 posted
    Satyam Khare:
    Hi,

    Am Getting Some Error, Here is my Code

    //------------------------------------------------------------------------FirstGridView------------------------------------------------//

    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" Width="995px" algin="center" CssClass="mydatagrid" PagerStyle-CssClass="pager" HeaderStyle-CssClass="header" RowStyle-CssClass="rows" OnRowDataBound=" GridView2_RowDataBound" OnPreRender="GridView2_PreRender">
    <Columns>

    <asp:BoundField DataField="Student_Reg_No" HeaderText="Roll No" ItemStyle-Width="8%"
    SortExpression="LastName">

    <ItemStyle Width="8%" />
    </asp:BoundField>

    <asp:TemplateField HeaderText="Present" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
    <asp:RadioButton ID="rbpresent" runat="server" GroupName="checked" Checked="true" onclick="setrowcolorpresent(this);" />
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Absent" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
    <asp:RadioButton ID="rbabsent" runat="server" GroupName="checked" onclick="setrowcolorabsent(this);" />
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Leave" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
    <asp:RadioButton ID="ableave" runat="server" GroupName="checked" onclick="setrowcolorLeave(this);" />
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center" />
    </asp:TemplateField>
    <asp:BoundField DataField="ADM_NO"
    HeaderText="Admission No"
    SortExpression="FirstName" ItemStyle-Width="11%">
    <ItemStyle Width="11%" />
    </asp:BoundField>
    <asp:BoundField DataField="Student_Name" HeaderText="Student Name"
    SortExpression="FirstName" ItemStyle-Width="27%">
    <ItemStyle Width="27%" />
    </asp:BoundField>
    <asp:BoundField DataField="Class" HeaderText="Class"
    SortExpression="FirstName" ItemStyle-Width="6%">
    <ItemStyle Width="6%" HorizontalAlign="Center" />
    </asp:BoundField>
    <asp:BoundField DataField="Section" HeaderText="Section"
    SortExpression="FirstName" ItemStyle-Width="6%">


    <ItemStyle Width="6%" HorizontalAlign="Center" />
    </asp:BoundField>


    <asp:BoundField DataField="Fathers_Name" HeaderText="Fathers Name."
    SortExpression="FirstName" ItemStyle-Width="15%">
    <ItemStyle Width="15%" />
    </asp:BoundField>
    <%--<asp:BoundField DataField="SmsDetailMobNo" HeaderText="Mob No."
    SortExpression="FirstName" ItemStyle-Width="15%" DataFormatString="######"/>--%>
    <asp:BoundField DataField="SmsDetailMobNo" HeaderText="Mob No." HtmlEncode="False"
    SortExpression="FirstName" ItemStyle-Width="9%">
    <ItemStyle Width="9%" HorizontalAlign="Center" />
    </asp:BoundField>
    </Columns>

    <HeaderStyle CssClass="header" />
    <PagerStyle CssClass="pager" />
    <RowStyle CssClass="rows" BackColor="#4CAF50" />

    </asp:GridView>

    //-----------------------------------Second Gridvew To Show Data Of Selected Radio Button On Button Click —------------------------------------------------—
    <asp:GridView ID="Gridshow" runat="server"></asp:GridView>











    //----------------------------------------------------C# Code To Get the Selected Radiobutton Value on Second Grid on Button Click--------------------------------------------------//

    [System.Web.Services.WebMethod]

    protected void btnsemdsms_Click(object sender, EventArgs e)
    {

    if(TextMain.Text == "" || TextPreview.Text == "")
    {
    }
    else
    {
    try
    {
    string strAdmissionNO = string.Empty;
    string StuName = string.Empty;
    string strname = string.Empty;
    foreach (GridViewRow gvrow in GridView2.Rows)
    {
    RadioButton chkabsent = (RadioButton)gvrow.FindControl("rbabsent");
    RadioButton chkleave = (RadioButton)gvrow.FindControl("ableave");
    RadioButton chkpresent= (RadioButton)gvrow.FindControl("rbpresent");
    if (chkabsent != null & chkabsent.Checked)
    {
    gvrow.BackColor = ColorTranslator.FromHtml("Red");
    strAdmissionNO = gvrow.Cells[4].Text + ",";
    string output = strAdmissionNO.Trim(",".ToCharArray());
    GridViewRow row = (GridViewRow)((RadioButton)sender).NamingContainer;
    DataTable dt = new DataTable("sample");
    dt.Columns.Add("ADM_NO");
    dt.Columns.Add("Student_Name");
    DataRow dataRow;
    dataRow = dt.NewRow();
    //Change the column index here
    dataRow[0] = row.Cells[1].Text;
    dataRow[1] = row.Cells[2].Text;
    dt.Rows.Add(dataRow);
    Gridshow.DataSource = dt;
    Gridshow.DataBind();
    }

    else if (chkleave != null & chkleave.Checked)
    {

    gvrow.BackColor = ColorTranslator.FromHtml("#f9a825");
    }
    else if (chkpresent != null & chkpresent.Checked)
    {

    gvrow.BackColor = ColorTranslator.FromHtml("#4caf50");
    }



    }
    }


    catch (Exception)
    {

    }
    }
    }
    Thursday, December 8, 2016 4:45 AM
  • User2103319870 posted

    Am Getting Some Error,

    Please provide more details on what is the actual error you are getting.

    catch (Exception)
    {

    }

    You have an empty catch block in your code. if you have like this then you wont be able to get any error details.

    Implement try catch like this : https://msdn.microsoft.com/en-us/library/xtd0s8kd(v=vs.110).aspx

    Thursday, December 8, 2016 3:08 PM
  • User345697035 posted
    Hi ,
    a2h
    Error is resolved but still having one problem that
    When I bind the dt to gridview it's showing only the last selected radio button coulmn value
    Because the previous values are not saving in datatable
    Can you tell me how to add my values in dt
    And after all values added to datatable then it assign to gridview
    Thursday, December 8, 2016 4:22 PM
  • User2103319870 posted

    When I bind the dt to gridview it's showing only the last selected radio button coulmn value
    Because the previous values are not saving in datatable
    Can you tell me how to add my values in dt
    And after all values added to datatable then it assign to gridview

    You might consider saving the values to database when user selects a row and then populate the gridview from database.

    Friday, December 9, 2016 2:41 PM