How to insert radio button column in gridview asp.net

10,816

You can add in a RadioButton tag in the TemplateField

<Columns>
  <asp:TemplateField>
    <ItemTemplate>
      <asp:RadioButton ID="RowSelector" runat="server" GroupName="SelectGroup" />
    </ItemTemplate>
  </asp:TemplateField>
</Columns>

RadioButton columns in a gridview

Share:
10,816
Bilal
Author by

Bilal

Just &lt;3 Coding.

Updated on June 14, 2022

Comments

  • Bilal
    Bilal almost 2 years

    I am having trouble inserting a radio button in gridview. I don't understand how this get's done, also i don't see item template anywhere in gridview?

  • Bilal
    Bilal almost 10 years
    Thank you, this works for me, how do i manually add another column? say a column that displays today's date and another that displays some offer
  • andyno
    andyno almost 10 years
    You can do this programatically by having ASP controls in the <ItemTemplate> such as <asp:Label id="lbDate" Text='<%= System.DateTime.Now.ToString() %>' runat="server" />, for more complicated custom data that doesn't directly match up to a column in your datasource I'd recommend taking a look at the GridView.RowDataBound Event, you can access GridViewRowEventArgs data items from the datasource you are binding the grid view to and use gridview.FindControl to set controls from the code behind. See RowDataBound and GridViewRowEventArgs
  • Bilal
    Bilal almost 10 years
    why fields are going to be static i.e only the time will be changed.. 3 columns -- radio button column || date time column of now || some text column
  • andyno
    andyno almost 10 years
    It totally depends on your requirements, you could have 3 TemplateFields, with the relevant controls, and use e.g Text='<%# Container.DataItem("TextContent") %>' to set the value, and avoid having to use RowDataBound, RowDataBound is used if the data is more complicated/different to a directly matched database column.
  • Bilal
    Bilal almost 10 years
    DataTable dt = new DataTable(); dt.Columns.Add("sr_no"); dt.Columns.Add("item_name"); dt.Columns.Add("item_id"); dt.Columns.Add("qty"); dt.Columns.Add("rate"); dt.Columns.Add("total"); var dr = dt.NewRow(); dr["sr_no"] = txtSr.Text; dr["item_name"] = ddlItem.SelectedItem.Text; dr["item_id"] = ddlItem.SelectedValue; dr["qty"] = txtQty.Text; dr["rate"] = txtRate.Text; dr["total"] = int.Parse(txtQty.Text) * int.Parse(txtRate.Text); dt.Rows.Add(dr); i ll be using this. fyi