I am trying to retrieve value of the selected item in GridDropDownColumn on RadGrid1_UpdateCommand event. I get
Error: "Cannot find a cell bound to column name 'FormID'". I am giving all the code, but the issue is in RadGrid1_UpdateCommand when it tries to get the FormID.
<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 behine:
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;
}
protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.Item isGridEditableItem &&
e.Item.IsInEditMode)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
string AccessID =
editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["AccessID"].ToString();
//error on this line
string FormID = (editedItem["FormID"].Controls[0] as DropDownList).SelectedItem.Text;
string ContactType = (editedItem["ContactType"].Controls[0] as TextBox).Text;
try
{
//do update to database
}
catch (Exception ex)
{
RadGrid1.Controls.Add(new LiteralControl("Unable to update data. Reason: " +
ex.Message));
e.Canceled = true;
}
}
}