I trying to load data to a dropdown column from the code behind. I was using GridDropDownListColumnEditor which was working fine in telerick version of 2009 and the same is not working in 2010 version. Where as i am getting data which is binded in the grid instead of getting
binded from the datatable. The below i have given my code
ASPX :
<telerik:GridDropDownColumn DataField="user_type" DataType="System.Int32" HeaderText="User Type"
SortExpression="user_type" UniqueName="user_type" ListTextField="user_type_name" ListDataMember ="user_profile"
ListValueField="user_type" >
</telerik:GridDropDownColumn>
C#
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if ((e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted) || (e.Item is GridEditableItem && e.Item.IsInEditMode))
{
GridEditableItem item = e.Item as GridEditableItem;
GridEditManager editMan = item.EditManager;
GridDropDownListColumnEditor usertype = editMan.GetColumnEditor("user_type") as GridDropDownListColumnEditor;
usertype.DataSource = datatable("user_type");
usertype.DataBind();
}
}
The Datatable will return 3 rows like superadmin,admin and users. It should dispaly 3 rows instead it is displaying 10 dupilcate rows where as i have 10 users in user_profile which is listed in the GRID.
Need ur help in this regards.
Thanks & Regards
Kannan
10 Answers, 1 is accepted
Please try setting the DataSource of the DropDownListControl of the editor:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e){ if ((e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted) || (e.Item is GridEditableItem && e.Item.IsInEditMode)) { GridEditableItem item = e.Item as GridEditableItem; GridEditManager editMan = item.EditManager; GridDropDownListColumnEditor usertype = editMan.GetColumnEditor("user_type") as GridDropDownListColumnEditor; usertype.DropDownListControl.DataSource = datatable("user_type"); usertype.DropDownListControl.DataBind(); }}You can examine the Customize/Configure GridDropDownColumn help topic for additional information.
I hope this helps.
Kind regards,
Mira
the Telerik team
Thanks for your reply and that code is still not working.
See when i remove ListDataMember ="user_profile" from the aspx page then my code is working. But the problem is User type is not displayed in the GRID. Seems to show empty column. And be sure my code is working fine in telerik version 2009.3, It is not working in 2010.3.
Please give some other solution.
Thanks & Regards
Kannan.
Try the following code snippet in ItemDataBound event to display the GridDropDownColumn value in normal mode of grid.
C#:
protected void gdParcelReq_ItemDataBound(object sender, GridItemEventArgs e) { if (e.Item is GridDataItem) { GridDataItem item = (GridDataItem)e.Item; DataRowView row = (DataRowView)e.Item.DataItem; item["user_type"].Text = row["user_type_name"].ToString(); } }Thanks,
Princy.
Thanks for your code and it isworking. But in the edit mode the dropdown shows the first data.
Example:
If a user is saved with user type as user then when in edit mode it shows as system admin. Since the data in the dropdown is super admin, admin and user. For all the users in the edit mode it displays the first data. Its not picking up the saved one.
Please give a solution
Thanks & Regards
Kannan
You need to set the RadComboBox Selectedvalue explicitly from code behind. Here is the sample code.
C#:
if (e.Item is GridEditFormItem && e.Item.IsInEditMode) { GridEditFormItem edititem = (GridEditFormItem)e.Item; RadComboBox combo= (RadComboBox)edititem["DropDownListColumn"].Controls[0]; combo.DataTextField = "FirstName"; combo.DataValueField = "FirstName"; combo.DataSourceID = "SqlDataSource1"; GridDataItem item = (GridDataItem)edititem.ParentItem; string strTxt = item["DropDownListColumn"].Text.ToString();//accessing the text combo.SelectedValue = strTxt; combo.DataBind(); }Thanks,
Shinu.
Hi Shinu,
Thanks for you code. And the code is not working.
string strTxt = item["user_type"].Text.ToString();
This line returns empty string. So the selected value is not assigned. And be sure I am not using asp:SqlDataSource1.
I am binding a datatable.
Here is the code which is used.
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e){
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
DataRowView row = (DataRowView)e.Item.DataItem;
item["user_type"].Text = row["user_type_name"].ToString();
}
if ((e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted) || (e.Item is GridEditableItem && e.Item.IsInEditMode)) { GridEditableItem item = e.Item as GridEditableItem; GridEditManager editMan = item.EditManager; GridDropDownListColumnEditor usertype = editMan.GetColumnEditor("user_type") as GridDropDownListColumnEditor;
usertype.DataSource = datatable("user_type");
GridEditFormItem edititem = (GridEditFormItem)e.Item;
GridDataItem item = (GridDataItem)edititem.ParentItem;
string strTxt = item["user_type"].Text.ToString();//This line Returns empty string
usertype.SelectedValue=strTxt;
usertype.DataBind(); }}
Please give me a solution
Thanks & Regards
Kannan<telerik:RadGrid ID="gvVendor" runat="server" GridLines="None"><MasterTableView DataKeyNames="VendorID" AutoGenerateColumns="false" AllowSorting="true" AllowPaging="true" CommandItemDisplay="Top" EditMode="InPlace" ><Columns> <telerik:GridBoundColumn UniqueName="VendorID" DataField="VendorID" HeaderText="VendorID" ReadOnly="true" /> <telerik:GridTemplateColumn UniqueName="VendorName" HeaderText="Vendor Name" HeaderStyle-Width="200px" > <ItemTemplate> <asp:Label ID="lblVendorName" Text='<%# Bind("VendorName") %>' runat="server" /> </ItemTemplate> <EditItemTemplate> <telerik:RadTextBox ID="rtbVendorName" Text='<%# Eval("VendorName") %>' runat="server" /> <asp:RequiredFieldValidator ID="rfvVendorName" ControlToValidate="rtbVendorName" ErrorMessage="*" runat="server" /> </EditItemTemplate> </telerik:GridTemplateColumn> <telerik:GridBoundColumn UniqueName="LNSPrefix" DataField="LNSPrefix" HeaderText="LNSPrefix" /> <telerik:GridBoundColumn UniqueName="KMSPrefix" DataField="KMSPrefix" HeaderText="KMSPrefix" /> <telerik:GridCheckBoxColumn UniqueName="RepGroupFlag" HeaderText="Rep Group?" /> <telerik:GridDropDownColumn UniqueName="RepGroup" DropDownControlType="RadComboBox" EnableEmptyListItem="True" EmptyListItemValue="0" EmptyListItemText="Select One" ListValueField="RepGroupID" ListTextField="RepGroups" HeaderText="Rep Group" /> <telerik:GridCheckBoxColumn UniqueName="RegistrationFlag" HeaderText="Registration?" /> <telerik:GridEditCommandColumn EditText="Edit" /> <telerik:GridButtonColumn UniqueName="DeleteColumn" CommandName="Delete" Text="Delete" /></Columns></MasterTableView></telerik:RadGrid>Protected Sub gvVendor_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles gvVendor.ItemDataBound Dim dgItem As GridDataItem = Nothing Dim gefItem As GridEditFormItem = Nothing Dim geItem As GridEditableItem = Nothing If TypeOf e.Item Is IGridInsertItem Then ElseIf TypeOf e.Item Is GridDataItem Then dgItem = CType(e.Item, GridDataItem) FillInCheckBoxes(dgItem) End If If TypeOf e.Item Is GridEditableItem Then geItem = CType(e.Item, GridEditableItem) FillInRepGroups(geItem) End IfEnd SubPrivate Sub FillInCheckBoxes(ByVal gdItem As GridDataItem) Dim chkRepGroup, chkRegistration As CheckBox Dim litRepGroup As Literal = Nothing Dim drView As DataRowView drView = gdItem.DataItem chkRepGroup = CType(gdItem("RepGroupFlag").Controls(0), CheckBox) If drView("RepGroupFlag") = 1 Then chkRepGroup.Checked = True Else chkRepGroup.Checked = False End If chkRegistration = CType(gdItem("RegistrationFlag").Controls(0), CheckBox) If drView("RegistrationFlag") = 1 Then chkRegistration.Checked = True Else chkRegistration.Checked = False End If If TypeOf gdItem("RepGroup").Controls(0) Is Literal Then litRepGroup = CType(gdItem("RepGroup").Controls(0), Literal) litRepGroup.Text = drView("RepGroup").ToString End IfEnd SubPrivate Sub FillInRepGroups(ByVal geItem As GridEditableItem) Dim geManager As GridEditManager = Nothing Dim gddlEditor As GridDropDownListColumnEditor = Nothing Dim rcbRepGroup As RadComboBox Dim rcbItem As RadComboBoxItem = Nothing Dim ds As DataSet = Nothing Dim ws As CommonFunctions If geItem.IsInEditMode Then Else Exit Sub End If geManager = geItem.EditManager gddlEditor = CType(geManager.GetColumnEditor("RepGroup"), GridDropDownColumnEditor) rcbRepGroup = gddlEditor.ComboBoxControl ws = New CommonFunctions ds = ws.GetRepGroups rcbRepGroup.DataSource = ds.Tables(0) rcbRepGroup.DataValueField = "RepGroupsID" rcbRepGroup.DataTextField = "RepGroups" rcbRepGroup.DataBind() rcbItem = New RadComboBoxItem("Select One", 0) rcbRepGroup.Items.Insert(0, rcbItem)End SubNo your code too is not working in edit mode. However in add mode its working fine. I want the values which is saved should be selected in the dropdown list.
I Need a solution for edit mode
Thanks & Regards
Kannan
Sorry I misread your post. And your code is working fine.
Thanks
Kannan