locked
How to add a new row in gridview after selecting controls outside of the gridview? RRS feed

  • Question

  • User370858843 posted

    Hi, I would like to get radiobuttonlist selectedvalue,  checkboxlist checked value, etc. (those controls are outside of the gridview) and display as a new gridview row. At the footer, I have a "Add New" button to add new row. How do I do this? It is very easy to do with Listbox control, all I need to do is use ListBox.Items.Add() method. Please advice!

    Tuesday, November 14, 2017 4:28 PM

Answers

  • User-1838255255 posted

    Hi jzhou819,

    According to your description and needs, i make a sample through your needs, here is the complete sample code:

    Sample Code:

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="tb" runat="server"></asp:TextBox>
                <asp:RadioButtonList ID="rbl" runat="server">
                    <asp:ListItem>Red</asp:ListItem>
                    <asp:ListItem>Yellow</asp:ListItem>
                    <asp:ListItem>Blue</asp:ListItem>
                </asp:RadioButtonList>
                <asp:CheckBoxList ID="cbl" runat="server">
                    <asp:ListItem>shoes</asp:ListItem>
                    <asp:ListItem>trousers</asp:ListItem>
                    <asp:ListItem>jackets</asp:ListItem>
                </asp:CheckBoxList>
                <asp:GridView ID="GridView1" runat="server" CssClass="Grid" AutoGenerateColumns="false"
                    EmptyDataText="No records has been added.">
                    <Columns>
                        <asp:BoundField DataField="tbvalue" HeaderText="tbvalue" ItemStyle-Width="120" />
                        <asp:BoundField DataField="Color" HeaderText="Color" ItemStyle-Width="120" />
                        <asp:BoundField DataField="Type" HeaderText="Type" ItemStyle-Width="120" />
                    </Columns>
                </asp:GridView>
                <asp:Button ID="insert" runat="server" Text="Add New Row" OnClick="insert_Click" />
            </div>
        </form>
    </body>
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("tbvalue"), new DataColumn("Color"), new DataColumn("Type") });
                    ViewState["Customers"] = dt;
                    this.BindGrid();
                }
            }
    
            protected void BindGrid()
            {
                GridView1.DataSource = (DataTable)ViewState["Customers"];
                GridView1.DataBind();
            }
    
            protected void insert_Click(object sender, EventArgs e)
            {
                DataTable dt = (DataTable)ViewState["Customers"];
                var value1 = tb.Text.Trim();
                var value2 = rbl.SelectedValue;
                var value3 = cbl.SelectedValue;
                dt.Rows.Add(value1, value2, value3);
                ViewState["Customers"] = dt;
                this.BindGrid();
                tb.Text = string.Empty;
            }

    Result:

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 15, 2017 6:57 AM

All replies

  • User-1838255255 posted

    Hi jzhou819,

    According to your description and needs, i make a sample through your needs, here is the complete sample code:

    Sample Code:

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="tb" runat="server"></asp:TextBox>
                <asp:RadioButtonList ID="rbl" runat="server">
                    <asp:ListItem>Red</asp:ListItem>
                    <asp:ListItem>Yellow</asp:ListItem>
                    <asp:ListItem>Blue</asp:ListItem>
                </asp:RadioButtonList>
                <asp:CheckBoxList ID="cbl" runat="server">
                    <asp:ListItem>shoes</asp:ListItem>
                    <asp:ListItem>trousers</asp:ListItem>
                    <asp:ListItem>jackets</asp:ListItem>
                </asp:CheckBoxList>
                <asp:GridView ID="GridView1" runat="server" CssClass="Grid" AutoGenerateColumns="false"
                    EmptyDataText="No records has been added.">
                    <Columns>
                        <asp:BoundField DataField="tbvalue" HeaderText="tbvalue" ItemStyle-Width="120" />
                        <asp:BoundField DataField="Color" HeaderText="Color" ItemStyle-Width="120" />
                        <asp:BoundField DataField="Type" HeaderText="Type" ItemStyle-Width="120" />
                    </Columns>
                </asp:GridView>
                <asp:Button ID="insert" runat="server" Text="Add New Row" OnClick="insert_Click" />
            </div>
        </form>
    </body>
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("tbvalue"), new DataColumn("Color"), new DataColumn("Type") });
                    ViewState["Customers"] = dt;
                    this.BindGrid();
                }
            }
    
            protected void BindGrid()
            {
                GridView1.DataSource = (DataTable)ViewState["Customers"];
                GridView1.DataBind();
            }
    
            protected void insert_Click(object sender, EventArgs e)
            {
                DataTable dt = (DataTable)ViewState["Customers"];
                var value1 = tb.Text.Trim();
                var value2 = rbl.SelectedValue;
                var value3 = cbl.SelectedValue;
                dt.Rows.Add(value1, value2, value3);
                ViewState["Customers"] = dt;
                this.BindGrid();
                tb.Text = string.Empty;
            }

    Result:

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 15, 2017 6:57 AM
  • User370858843 posted

    Thanks for your help! It solves my problem. The checkboxlist selectedvalue code should be:

     var value3 = "";
                for (int i = 0; i < cbl.Items.Count; i++)
                {
                    if (cbl.Items[i].Selected)
                    {
                        value3 += cbl.Items[i].Value + ",";
                    }
                }

    Thursday, November 16, 2017 4:01 PM
  • User2103319870 posted

    var value3 = "";
                for (int i = 0; i < cbl.Items.Count; i++)
                {
                    if (cbl.Items[i].Selected)
                    {
                        value3 += cbl.Items[i].Value + ",";
                    }
                }

    You can also use the below code which make use of linq method to generate comma separated string of selected checkboxlist items

    //Getting multple checked items from checkboxlist as comma separated string
    string value3 = String.Join(",", cbl.Items.OfType<ListItem>().Where(r => r.Selected).Select(r => r.Text));

    Ensure that you added the below namespace in your page before using above method

    using System.Linq;

    Thursday, November 16, 2017 4:11 PM