RadComboBox in a RadGrid and getting the unique row
I have a radgrid with a radcombobox in each row. I want to get the row's ID after the combo box has been chosen (someone selecting a value in the drop down). I'm using the onitemcreated property of the radgrid to get my method called in code behind. However, I'm not able to read the value of the ID that belongs to the row in which the chosen combo box belongs to. Can anyone provide any suggestions. I have attempted all of Telerik's samples but I'm not getting good results.
As you see in my code below I have a radbutton in the gridtemplatecolumn and this works as expected. When I'm in debug I get   for the value to intID which is not a result I would expect after I select a value from the combo box.
Here is all of my code:
HTML markup:
<telerik:RadGrid ID="rdg1"
runat="server"
ItemStyle-Wrap="false"
TabIndex="1000"
GridLines="Horizontal"
BorderColor="#738BA4"
BorderWidth="1px"
OnNeedDataSource="ds1"
OnItemDataBound="oidb1"
AutoGenerateColumns="False"
OnItemCommand = "oicommand1"
EnableLinqExpressions="true"
OnItemCreated="oicreated1">
<MasterTableView DataKeyNames="ID"
HorizontalAlign="Center"
HeaderStyle-BorderWidth="0"
ItemStyle-BorderWidth="2"
ItemStyle-BorderColor="#738BA4"
FooterStyle-BorderWidth="0"
BorderWidth="0">
<CommandItemSettings ExportToPdfText="Export to Pdf" />
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
</RowIndicatorColumn>
开发者_运维知识库 <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
</ExpandCollapseColumn>
<Columns>
<telerik:GridBoundColumn DataField="ID"
ItemStyle-Font-Size="8"
UniqueName="ID"
Visible="false">
<HeaderStyle Wrap="False" />
<ItemStyle Font-Size="8pt" Wrap="False" />
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn ItemStyle-Font-Size="8" HeaderText="Level" UniqueName="Level">
<ItemTemplate>
<telerik:RadComboBox ID="rdcb1" runat="server" AutoPostBack="true"></telerik:RadComboBox>
</ItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Font-Size="8pt" Wrap="False" />
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn ItemStyle-Font-Size="8"
DataField="Name"
UniqueName="Name"
Visible="true"
HeaderText="Name">
<HeaderStyle Wrap="False" />
<ItemStyle Font-Size="8pt" Wrap="true" />
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:ImageButton ID="imgbtn" runat="server" ImageUrl="/Images/Delete-Small.PNG" />
</ItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</telerik:GridTemplateColumn>
</Columns>
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column">
</EditColumn>
</EditFormSettings>
<ItemStyle BorderColor="#738BA4" BorderWidth="2px" />
<AlternatingItemStyle />
<HeaderStyle BorderWidth="0px" />
<FooterStyle BorderWidth="0px" />
</MasterTableView>
<ItemStyle Wrap="False" />
<FilterMenu EnableImageSprites="False">
</FilterMenu>
<HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
</HeaderContextMenu>
</telerik:RadGrid>
</asp:Panel>
Code behind:
protected void ds1(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e) {
var Role = (from r in db.Role
select new { r.ID, r.Name });
rdg1.DataSource = Role;
Level = Role.Count();
_dataTableLevel.Clear();
_dataTableLevel.Columns.Add("Level");
for (int i = 1; i <= Level; i++) {
DataRow drLevel = _dataTableLevel.NewRow();
drLevel["Level"] = i;
_dataTableLevel.Rows.Add(drOrderLevel);
}
protected void oidb1(object sender, GridItemEventArgs e) {
if (e.Item is GridDataItem) {
GridDataItem Item1 = (GridDataItem)e.Item;
(Item1.FindControl("rdcb1") as RadComboBox).DataValueField = "Level";
(Item1.FindControl("rdcb1") as RadComboBox).DataTextField = "Level";
(Item1.FindControl("rdcb1") as RadComboBox).DataSource = _dataTableLevel.DefaultView;
(Item1.FindControl("rdcb1") as RadComboBox).DataBind();
}
}
protected void oicommand1(object sender, GridCommandEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem Item2 = (GridDataItem)e.Item;
TableCell ID = Item2["ID"] as TableCell;
int intID = Convert.ToInt32(ID.Text);
var deleteRole = (from r in db.Role
where r.ID == intID).First();
db.Role.DeleteObject(deleteRole);
db.SaveChanges();
rdg1.Rebind();
}
}
protected void oicreated1(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem Item3 = (GridDataItem)e.Item;
TableCell ID = Item3["ID"] as TableCell;
string ID = ID.Text;
}
}
The guys at Telerik provided the solution below. The solution by Telerik works.
Thanks.
Thank you for contacting us.
If you want go get the row's ID when you change the selected index of particular combobox, my suggestion is to subscribe on the service-side OnSelectedIndexChanged
event and use the following implementation of the event handling function:
protected void OnSelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
var combobox = sender as RadComboBox;
GridDataItem dataItem = combobox.Parent.Parent as GridDataItem;
var text = dataItem["ID"].Text;
}
I hope this would help you out.
Kind regards, Dimitar Terziev the Telerik team
your solution works but there is another approach with DataKeyName of RadGrid, if somebody does not ID in columns
protected void RadComboBox1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
var combobox = sender as RadComboBox;
GridDataItem dataItem = combobox.Parent.Parent as GridDataItem;
string id = dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["Id"].ToString();
}
精彩评论