This is a migrated thread and some comments may be shown as answers.

add dynamic radiobutton on rad grid

1 Answer 302 Views
Grid
This is a migrated thread and some comments may be shown as answers.
shinra
Top achievements
Rank 1
shinra asked on 08 Apr 2015, 07:15 PM

I need to add a custom radio button control that I created based on an if condition to my GridView. My radiobutton will be enabled or disabled based on this condition and will have the text changed as well.I'm trying to figure out how to add a radiobutton object into my data row instead of a stringdt.Columns.Add("FirstName").

 

<telerik:RadGrid runat="server" ID="grd1" OnNeedDataSource="grd1_NeedDataSource">
<MasterTableView AutoGenerateColumns="False">
<Columns>
<telerik:GridTemplateColumn HeaderText="Radiobutton header" UniqueName="col1">
<ItemTemplate>
<asp:RadioButton ID="rbType" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "rbEnableorDisable")%>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
 
<telerik:GridTemplateColumn HeaderText="FirstName header" UniqueName="col2">
<ItemTemplate>
<asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "Name")%>' runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>

 Codebehind


dt= New DataTable
dt.Columns.Add("rbEnableorDisable")
dt.Columns.Add("FirstName")
 
Dim rb As RadioButton
rb = New RadioButton
 
For each item in itemlist //some data iteration declared elsewhere
dr = dt.NewRow()
If (Condition)
rb.Text = "Should be Disabled"
rb.Enabled = False
Else
rb.Text = "Should be Enabled"
rb.Enabled = True
End if
 
dr.Item("FirstName") = item.FirstName
dr.Item("rbEnableOrDisable") = rb//?Code for inserting a radio button object
dt.Rows.Add(dr)
Next
 
With grd1
.DataSource = dt
.DataBind()
End With

 So far with this code I am only able to display the radiobutton text if i havedr.Item("rbEnableOrDisable") = rb.Text.I need to display the whole radiobutton object(show the text and if it's enabled or disabled among others)I triedLocationData.Columns.Add(New DataColumn("rbType", GetType(RadioButton)))but it seems I need to append to the ItemTemplateAlso tried adding the whole column dynamic with:grd1.Controls.Add(rb)

 

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 13 Apr 2015, 08:54 AM
Hello Shinra,

Please note that using DataBind() to bind the grid is not recommended. Performing complex grid operations such as Inserting, Deleting, Updating, Hierarchy relations, Grouping, Exporting, Paging, Sorting, Filtering, etc. require accommodating appropriate database operations.  Therefore, we suggest you to avoid Simple Databinding and strongly recommend the use of more advanced databinding methods, which automatically handle the aforementioned functions:
Declarative DataSource
Advanced Data Binding


You can use ItemCreated and ItemDataBound event handlers provided by RadGrid to add the RadioButton programmatically, similar to the approach demonstrated in the attached application.

Hope this helps.

Regards,
Eyup
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
Grid
Asked by
shinra
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or