I am using Telerik.Web.UI version 2017.1.118.40 with Visual studio 2010.
I have an issue using GridDropDownColumn inside RadGrid. In Edit mode when data is displayed in dropdownlist, its data is reset (i.e. it is not displaying the text which was in Item template or on the form for that row). I cannot use SQLDataSource, so please do not give me sample code using that.
Here is the ASPX code :
<telerik:RadGrid ID="RadGrid1"runat="server"AutoGenerateColumns="False"GridLines="None"
AllowMultiRowEdit="True"OnItemDataBound="RadGrid1_ItemDataBound"OnNeedDataSource="RadGrid1_NeedDataSource"
OnUpdateCommand="RadGrid1_UpdateCommand">
<MasterTableViewAutoGenerateColumns="false"DataKeyNames="AccessID"Width="100%"
CommandItemDisplay="Top">
<Columns>
<telerik:GridButtonColumn CommandName="Delete"Text="Delete"UniqueName="Delete">
</telerik:GridButtonColumn>
<telerik:GridBoundColumn DataField="AccessID" DataType="System.Int32" FilterControlAltText="Filter AccessID column"
HeaderText="Access
ID" ReadOnly="True"SortExpression="AccessID" UniqueName="AccessID">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="FirstName"FilterControlAltText="Filter
FirstName column"
HeaderText="First
Name" ReadOnly="True" SortExpression="FirstName" UniqueName="FirstName">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="LastName"FilterControlAltText="Filter
LastName column"
HeaderText="Last
Name" ReadOnly="True" SortExpression="LastName" UniqueName="LastName">
</telerik:GridBoundColumn>
<telerik:GridDropDownColumnFilterControlAltText="Filter
ddlForm column" UniqueName="ddlForm"
ListTextField="FormName"ListValueField="FormID"SortExpression="FormName"
HeaderText="Form
Name"
DataField="FormID"ListDataMember="FormName"DropDownControlType="DropDownList">
</telerik:GridDropDownColumn>
<telerik:GridBoundColumn DataField="ContactType"FilterControlAltText="Filter
ContactType column"
HeaderText="Contact
Type" SortExpression="ContactType" UniqueName="ContactType">
</telerik:GridBoundColumn>
<telerik:GridEditCommandColumn>
</telerik:GridEditCommandColumn>
</Columns>
<EditFormSettingsColumnNumber="2"CaptionFormatString="Edit
details for Access ID {0}"
CaptionDataField="AccessID">
<FormTableItemStyle Wrap="False"></FormTableItemStyle>
<FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle>
<FormMainTableStyle CellSpacing="0"CellPadding="3"Width="100%"/>
<FormTableStyle GridLines="Horizontal"CellSpacing="0"CellPadding="2"CssClass="module"
Height="110px"Width="100%"/>
<FormTableAlternatingItemStyleWrap="False"></FormTableAlternatingItemStyle>
<FormStyle Width="100%"BackColor="#EEF2EA"></FormStyle>
<EditColumn UpdateText="Update
record" UniqueName="EditCommandColumn1" CancelText="Cancel
edit">
</EditColumn>
<FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle>
</EditFormSettings>
</MasterTableView>
</telerik:RadGrid>
Here is code behind:
protected void RadGrid1_NeedDataSource(object source,
Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
SqlConnection con = newSqlConnection(DataAccess.Connection.StrConn);
SqlCommand cmd = newSqlCommand("dbo.MyStoredProc",
con);
SqlDataAdapter adapter = newSqlDataAdapter();
adapter.SelectCommand = cmd;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 90;
DataTable myDataTable = newDataTable();
con.Open();
try
{
adapter.Fill(myDataTable);
}
finally
{
con.Close();
}
RadGrid1.DataSource = myDataTable;
}
protected void RadGrid1_ItemDataBound(object sender,
Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
GridEditManager editMan =
editedItem.EditManager;
GridDropDownListColumnEditor editor =
editMan.GetColumnEditor("ddlForm") as GridDropDownListColumnEditor;
DropDownList ddList =
editor.DropDownListControl;
ddList.DataSource = GetDataTable("SELECT
[FormID], FormName FROM dbo.[Table_FormList]"); //ds;
ddList.DataTextField = "FormName";
ddList.DataValueField = "FormID";
ddList.DataBind();
}
}
public DataTable GetDataTable(string query)
{
SqlConnection con = new SqlConnection(DataAccess.Connection.StrConn);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(query, con);
DataTable myDataTable = new DataTable();
con.Open();
try
{
adapter.Fill(myDataTable);
}
finally
{
con.Close();
}
return myDataTable;
}