Hello,
I am using a ComboBox within a RadGrid that uses DbContext to get its data from a table of States. While in edit mode I wish to have the ComboBox display the State from the Customers table if exists and/or be able to select a State if needed. Currently the ItemTemplate shows the current State selected for a customer just fine but while in edit mode the ComboBox is not populated with current State or a selection of States. Here is my code:
Customer.aspx
Customer.aspx.cs
Please let me know what I have done incorrectly or what I am missing. Thanks for the help!
I am using a ComboBox within a RadGrid that uses DbContext to get its data from a table of States. While in edit mode I wish to have the ComboBox display the State from the Customers table if exists and/or be able to select a State if needed. Currently the ItemTemplate shows the current State selected for a customer just fine but while in edit mode the ComboBox is not populated with current State or a selection of States. Here is my code:
Customer.aspx
<
telerik:GridTemplateColumn
HeaderText
=
"State"
EditFormColumnIndex
=
"2"
>
<
ItemTemplate
>
<%#DataBinder.Eval(Container.DataItem, "State")%>
</
ItemTemplate
>
<
EditItemTemplate
>
<
telerik:RadComboBox
ID
=
"selectState"
runat
=
"server"
Skin
=
"Web20"
DataTextField
=
"stateName"
DataValueField
=
"stateCode"
/>
</
EditItemTemplate
>
</
telerik:GridTemplateColumn
>
Customer.aspx.cs
protected void RadGrid1_EditCommand(object source, GridCommandEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem edititem = (GridEditableItem)e.Item;
DropDownList ddlStates = (DropDownList)edititem.FindControl("selectState");
ddlStates.DataSource = DbContext.States;
ddlStates.DataTextField = "stateName";
ddlStates.DataValueField = "stateCode";
ddlStates.DataBind();
}
}
Please let me know what I have done incorrectly or what I am missing. Thanks for the help!
17 Answers, 1 is accepted
0
Hi Tom,
Did you try to set the SelectedValue of RadComboBox:
http://www.telerik.com/community/forums/aspnet-ajax/combobox/radgrid---radcombobox-selected-value.aspx
Regards,
Helen
the Telerik team
Did you try to set the SelectedValue of RadComboBox:
http://www.telerik.com/community/forums/aspnet-ajax/combobox/radgrid---radcombobox-selected-value.aspx
Regards,
Helen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
ADAPT
Top achievements
Rank 2
answered on 10 Apr 2013, 01:32 PM
Thank you for responding Helen,
So this is my code now which works to display the state and on edit it displays a list of states. But now i need it to display the state that is in the customer table already for any given record. How do I do that?
customer.aspx
Please note that I am using a class library with a .dbml database file to gain access to the database. Thus my datasource is: combo.DataSource = DbContext.States;
Thanks for the help.
So this is my code now which works to display the state and on edit it displays a list of states. But now i need it to display the state that is in the customer table already for any given record. How do I do that?
customer.aspx
<
telerik:GridTemplateColumn
HeaderText
=
"State"
EditFormColumnIndex
=
"2"
>
<
ItemTemplate
>
<%#DataBinder.Eval(Container.DataItem, "State")%>
</
ItemTemplate
>
<
EditItemTemplate
>
<
telerik:RadComboBox
ID
=
"selectState"
runat
=
"server"
Skin
=
"Web20"
DataSourceID
=
"LinqDataSource1"
DataTextField
=
"stateName"
DataValueField
=
"stateCode"
/>
</
EditItemTemplate
>
</
telerik:GridTemplateColumn
>
Please note that I am using a class library with a .dbml database file to gain access to the database. Thus my datasource is: combo.DataSource = DbContext.States;
Thanks for the help.
0
Hello Tom,
Probably you will find helpful:
http://www.telerik.com/community/forums/aspnet-ajax/grid/databinding-of-a-combobox-inside-of-a-grid-using-entity-framwork.aspx
Regards,
Helen
the Telerik team
Probably you will find helpful:
http://www.telerik.com/community/forums/aspnet-ajax/grid/databinding-of-a-combobox-inside-of-a-grid-using-entity-framwork.aspx
Regards,
Helen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
ADAPT
Top achievements
Rank 2
answered on 12 Apr 2013, 01:20 PM
Thanks again Helen for your reply.
I have looked at the example you provided in the link but it does not solve my problem. This seems to be a very simple and normal thing to do. I am just missing something in my code. It would be very helpful to have someone show me the fix instead of giving me links to other solutions that are not what I am trying to accomplish. Let me start again.
2 Table in SQL: State and Customer....
I need a combobox within a grid to show states when in view mode. That combobox on edit needs to allow me to edit the state by selecting a new one from State table but needs to display what state is in the customer record already.
Here is my code so far that does not work. It displays the state that is currently in the customer record but on edit does not display the state in the customer table nor does it allow me to select a new state from the state table.
Customer.aspx
Customer.aspx.cs
I am desperate for a solution as I have not been able to get this to work for almost 2 weeks.
I have looked at the example you provided in the link but it does not solve my problem. This seems to be a very simple and normal thing to do. I am just missing something in my code. It would be very helpful to have someone show me the fix instead of giving me links to other solutions that are not what I am trying to accomplish. Let me start again.
2 Table in SQL: State and Customer....
I need a combobox within a grid to show states when in view mode. That combobox on edit needs to allow me to edit the state by selecting a new one from State table but needs to display what state is in the customer record already.
Here is my code so far that does not work. It displays the state that is currently in the customer record but on edit does not display the state in the customer table nor does it allow me to select a new state from the state table.
Customer.aspx
<
telerik:GridTemplateColumn
DataField
=
"State"
HeaderText
=
"State"
EditFormColumnIndex
=
"2"
>
<
ItemTemplate
>
<%#DataBinder.Eval(Container.DataItem, "State")%>
</
ItemTemplate
>
<
EditItemTemplate
>
<
telerik:RadComboBox
ID
=
"selectState"
runat
=
"server"
Skin
=
"Web20"
DataTextField
=
"stateName"
DataValueField
=
"stateCode"
/>
</
EditItemTemplate
>
</
telerik:GridTemplateColumn
>
protected void RadGrid1_EditCommand(object source, GridCommandEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem edititem = (GridEditableItem)e.Item;
DropDownList ddlStates = (DropDownList)edititem.FindControl("selectState");
ddlStates.DataSource = DbContext.States; //Derived from a Class Library from a .dbml file
ddlStates.DataTextField = "stateName";
ddlStates.DataValueField = "stateCode";
ddlStates.DataBind();
}
}
I am desperate for a solution as I have not been able to get this to work for almost 2 weeks.
0
ADAPT
Top achievements
Rank 2
answered on 12 Apr 2013, 03:33 PM
I have changed my customer.aspx.cs code to the following:
I know have the combobox displaying the list of states but it does not show the preselected state from the customer table in SQL. What else do I need? I currently have the following code on edit but does not work.
Thanks.
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem edititem = (GridEditableItem)e.Item;
RadComboBox ddlStates = (RadComboBox)edititem.FindControl("selectState");
ddlStates.DataSource = DbContext.States;
ddlStates.DataTextField = "stateName";
ddlStates.DataValueField = "stateCode";
ddlStates.DataBind();
}
}
I know have the combobox displaying the list of states but it does not show the preselected state from the customer table in SQL. What else do I need? I currently have the following code on edit but does not work.
protected void RadGrid1_EditCommand(object source, GridCommandEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem edititem = (GridEditableItem)e.Item;
if (!(e.Item is IGridInsertItem))
{
RadComboBox combo = (RadComboBox)edititem.FindControl("selectState");
RadComboBoxItem preselectedItem = new RadComboBoxItem();
preselectedItem.Text = edititem["stateName"].Text;
preselectedItem.Value = edititem["stateCode"].Text;
combo.Items.Insert(0, preselectedItem);
combo.SelectedIndex = 0;
}
}
}
Thanks.
0
ADAPT
Top achievements
Rank 2
answered on 15 Apr 2013, 08:32 PM
Anyone out there that will assit me? Any help is appreciated!
0
Hello,
I would suggest using the RadComboBox ItemDataBound server-side event handler and select a specific item using the following expression:
//markup code
//code behind
Regards,
Boyan Dimitrov
the Telerik team
I would suggest using the RadComboBox ItemDataBound server-side event handler and select a specific item using the following expression:
//markup code
<
telerik:RadComboBox
runat
=
"server"
ID
=
"RadComboBox1"
DataTextField
=
"ContactName"
DataSourceID
=
"LinqDataSource1"
OnItemDataBound
=
"RadComboBox1_ItemDataBound"
>
.....
</
telerik:RadComboBox
>
//code behind
protected
void
RadComboBox1_ItemDataBound(
object
sender, RadComboBoxItemEventArgs e)
{
//perform some custom logic in order to determine which item to select
e.Item.Selected =
true
;
}
Regards,
Boyan Dimitrov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
ADAPT
Top achievements
Rank 2
answered on 19 Apr 2013, 01:24 PM
Thank you Boyan for your reply. That logic to determine which item to select is what i am missing. Can anyone show me an example?
0
Hello,
You can use the session object to store the edititem object used in your RadGrid1_EditCommand server-side handler and use that stored value in the RadComboBox ItemDataBound event handler as shown in the code snippet below:
//code behind
Kind regards,
Boyan Dimitrov
the Telerik team
You can use the session object to store the edititem object used in your RadGrid1_EditCommand server-side handler and use that stored value in the RadComboBox ItemDataBound event handler as shown in the code snippet below:
//code behind
protected
void
RadGrid1_EditCommand(
object
source, GridCommandEventArgs e)
{
if
(e.Item
is
GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem edititem = (GridEditableItem)e.Item;
if
(!(e.Item
is
IGridInsertItem))
{
.....
Session[
"SelectedItem"
] = edititem[
"stateName"
].Text;
}
}
}
protected
void
RadComboBox1_ItemDataBound(
object
sender, RadComboBoxItemEventArgs e)
{
if
(Session[
"SelectedItem"
] !=
null
&& e.Item.Text == Session[
"SelectedItem"
].ToString())
{
e.Item.Selected =
true
;
}
}
Kind regards,
Boyan Dimitrov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
ADAPT
Top achievements
Rank 2
answered on 10 May 2013, 07:14 PM
Sorry it has taken so long to get back to this thread. I had some family matters to attend to.
I have implemented the code Boyan however when trying to edit or add to the grid nothing happens.
This is the issue for adding a new record: Not sure why.
I have implemented the code Boyan however when trying to edit or add to the grid nothing happens.
This is the issue for adding a new record: Not sure why.
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem edititem = (GridEditableItem)e.Item;
RadComboBox rcbStates = (RadComboBox)edititem.FindControl("selectState");
rcbStates.DataSource = DbContext.States;
rcbStates.DataTextField = "stateName";
rcbStates.DataValueField = "stateCode";
rcbStates.DataBind();
}
}
0
Hello,
Boyan Dimitrov
the Telerik team
The provided code snippet in my very last response will select by default a specific item from your RadComboBox items collection. This way when a user starts the application the RadComboBox will have an selected item. Could you please clarify what issue exactly you are facing when a new record is added and what is the expected behavior?
Regards,Boyan Dimitrov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
ADAPT
Top achievements
Rank 2
answered on 29 May 2013, 02:03 PM
Hi Boyan
When I click on the add button to add a record the radgrid just refreshes without going into edit mode. If I remove the code you suggested, I can get into edit mode but the selectState RadComboBox does not work.
manage.aspx page
manage.aspx.cs codebehind
When I click on the add button to add a record the radgrid just refreshes without going into edit mode. If I remove the code you suggested, I can get into edit mode but the selectState RadComboBox does not work.
manage.aspx page
<%@ Page Title="" Language="C#" MasterPageFile="~/Layouts/Manage.Master" AutoEventWireup="true" CodeBehind="ManageFirms.aspx.cs" Inherits="THOR.Management.ManageFirms" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<
asp:Content
ID
=
"Content2"
ContentPlaceHolderID
=
"CPHCenter"
runat
=
"server"
>
<
div
class
=
"pageTitleNormal"
>
Law Firm Management
</
div
>
<
br
/>
<
telerik:RadAjaxManager
runat
=
"server"
ID
=
"RadAjaxManager1"
DefaultLoadingPanelID
=
"RadAjaxLoadingPanel1"
>
<
AjaxSettings
>
<
telerik:AjaxSetting
AjaxControlID
=
"RadGrid1"
>
<
UpdatedControls
>
<
telerik:AjaxUpdatedControl
ControlID
=
"RadGrid1"
/>
<
telerik:AjaxUpdatedControl
ControlID
=
"RadWindowManager1"
/>
<
telerik:AjaxUpdatedControl
ControlID
=
"RadInputManager1"
/>
</
UpdatedControls
>
</
telerik:AjaxSetting
>
</
AjaxSettings
>
</
telerik:RadAjaxManager
>
<
telerik:RadAjaxLoadingPanel
runat
=
"server"
ID
=
"RadAjaxLoadingPanel1"
/>
<
telerik:RadGrid
runat
=
"server"
ID
=
"RadGrid1"
AutoGenerateColumns
=
"false"
AllowPaging
=
"true"
OnEditCommand
=
"RadGrid1_EditCommand"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
OnUpdateCommand
=
"RadGrid1_UpdateCommand"
OnItemCreated
=
"RadGrid1_ItemCreated"
OnDeleteCommand
=
"RadGrid1_DeleteCommand"
OnInsertCommand
=
"RadGrid1_InsertCommand"
OnItemDataBound
=
"RadGrid1_ItemDataBound"
Skin
=
"Web20"
>
<
MasterTableView
DataKeyNames
=
"ID"
CommandItemDisplay
=
"Top"
InsertItemPageIndexAction
=
"ShowItemOnCurrentPage"
EditItemStyle-BackColor
=
"#FFCCFF"
>
<
CommandItemSettings
ShowExportToWordButton
=
"true"
ShowExportToExcelButton
=
"true"
ShowExportToCsvButton
=
"true"
ShowExportToPdfButton
=
"true"
></
CommandItemSettings
>
<
Columns
>
<
telerik:GridEditCommandColumn
ButtonType
=
"ImageButton"
/>
<
telerik:GridBoundColumn
DataField
=
"ID"
HeaderText
=
"ID"
ReadOnly
=
"true"
ForceExtractValue
=
"Always"
ConvertEmptyStringToNull
=
"true"
/>
<
telerik:GridBoundColumn
DataField
=
"FirmKey"
HeaderText
=
"Firm Key"
ReadOnly
=
"True"
Visible
=
"False"
/>
<
telerik:GridBoundColumn
DataField
=
"FirmName"
HeaderText
=
"Firm Name"
ItemStyle-HorizontalAlign
=
"Left"
EditFormColumnIndex
=
"0"
/>
<
telerik:GridBoundColumn
DataField
=
"FirstName"
HeaderText
=
"First Name"
ItemStyle-HorizontalAlign
=
"Left"
EditFormColumnIndex
=
"1"
/>
<
telerik:GridBoundColumn
DataField
=
"MidName"
HeaderText
=
"Middle Name"
ItemStyle-HorizontalAlign
=
"Left"
EditFormColumnIndex
=
"2"
Display
=
"False"
/>
<
telerik:GridBoundColumn
DataField
=
"LastName"
HeaderText
=
"Last Name"
ItemStyle-HorizontalAlign
=
"Left"
EditFormColumnIndex
=
"3"
/>
<
telerik:GridBoundColumn
DataField
=
"Title"
HeaderText
=
"Title"
ItemStyle-HorizontalAlign
=
"Left"
EditFormColumnIndex
=
"0"
Display
=
"False"
/>
<
telerik:GridBoundColumn
DataField
=
"Email"
HeaderText
=
"Email"
ItemStyle-HorizontalAlign
=
"Left"
EditFormColumnIndex
=
"1"
/>
<
telerik:GridBoundColumn
DataField
=
"Address"
HeaderText
=
"Address"
ItemStyle-HorizontalAlign
=
"Left"
EditFormColumnIndex
=
"2"
/>
<
telerik:GridBoundColumn
DataField
=
"Suite"
HeaderText
=
"Suite"
ItemStyle-HorizontalAlign
=
"Left"
EditFormColumnIndex
=
"3"
/>
<
telerik:GridBoundColumn
DataField
=
"City"
HeaderText
=
"City"
ItemStyle-HorizontalAlign
=
"Left"
EditFormColumnIndex
=
"0"
/>
<
telerik:GridBoundColumn
DataField
=
"County"
HeaderText
=
"County"
ItemStyle-HorizontalAlign
=
"Left"
EditFormColumnIndex
=
"1"
Display
=
"False"
/>
<
telerik:GridTemplateColumn
DataField
=
"State"
HeaderText
=
"State"
EditFormColumnIndex
=
"2"
>
<
ItemTemplate
>
<%#DataBinder.Eval(Container.DataItem, "State")%>
</
ItemTemplate
>
<
EditItemTemplate
>
<
telerik:RadComboBox
ID
=
"selectState"
runat
=
"server"
Skin
=
"Web20"
DataTextField
=
"state"
DataValueField
=
"state"
SelectedValue='<%#Bind("state") %>' />
</
EditItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridBoundColumn
DataField
=
"PostalCode"
HeaderText
=
"Postal Code"
ItemStyle-HorizontalAlign
=
"Left"
EditFormColumnIndex
=
"3"
/>
<
telerik:GridBoundColumn
DataField
=
"Country"
HeaderText
=
"Country"
ItemStyle-HorizontalAlign
=
"Left"
EditFormColumnIndex
=
"0"
Display
=
"False"
/>
<
telerik:GridBoundColumn
DataField
=
"HomePhone"
HeaderText
=
"Home Phone"
ItemStyle-HorizontalAlign
=
"Left"
EditFormColumnIndex
=
"1"
Display
=
"False"
/>
<
telerik:GridBoundColumn
DataField
=
"BizPhone"
HeaderText
=
"Biz Phone"
ItemStyle-HorizontalAlign
=
"Left"
EditFormColumnIndex
=
"2"
/>
<
telerik:GridBoundColumn
DataField
=
"CellPhone"
HeaderText
=
"Cell Phone"
ItemStyle-HorizontalAlign
=
"Left"
EditFormColumnIndex
=
"3"
/>
<
telerik:GridBoundColumn
DataField
=
"Fax"
HeaderText
=
"Fax"
ItemStyle-HorizontalAlign
=
"Left"
EditFormColumnIndex
=
"0"
/>
<
telerik:GridCheckBoxColumn
DataField
=
"Active"
HeaderText
=
"Active"
ItemStyle-HorizontalAlign
=
"center"
EditFormColumnIndex
=
"1"
/>
<
telerik:GridDateTimeColumn
DataField
=
"CreationDate"
HeaderText
=
"Creation Date"
ItemStyle-HorizontalAlign
=
"Right"
DataFormatString
=
"{0:MMM dd yyyy}"
ReadOnly
=
"True"
Reorderable
=
"True"
EditFormColumnIndex
=
"2"
/>
<
telerik:GridButtonColumn
ConfirmText
=
"Delete this Law Firm?"
ConfirmDialogType
=
"RadWindow"
ConfirmTitle
=
"Delete"
ButtonType
=
"ImageButton"
CommandName
=
"Delete"
/>
</
Columns
>
<
EditFormSettings
ColumnNumber
=
"4"
CaptionDataField
=
"ID"
CaptionFormatString
=
"YOU ARE NOW EDITING RECORD No. {0}"
InsertCaption
=
"New Law Firm"
FormCaptionStyle-HorizontalAlign
=
"Left"
FormTableAlternatingItemStyle-HorizontalAlign
=
"Left"
FormTableItemStyle-HorizontalAlign
=
"Left"
FormCaptionStyle-BackColor
=
"#f2ea08"
>
<
FormTableItemStyle
Wrap
=
"False"
></
FormTableItemStyle
>
<
FormCaptionStyle
CssClass
=
"EditFormHeader"
></
FormCaptionStyle
>
<
FormMainTableStyle
GridLines
=
"Vertical"
CellSpacing
=
"0"
CellPadding
=
"2"
Width
=
"100%"
BorderColor
=
"#FFFFCC"
></
FormMainTableStyle
>
<
FormTableStyle
CellSpacing
=
"0"
CellPadding
=
"2"
Height
=
"110px"
></
FormTableStyle
>
<
FormTableAlternatingItemStyle
Wrap
=
"False"
></
FormTableAlternatingItemStyle
>
<
EditColumn
ButtonType
=
"ImageButton"
InsertText
=
"Insert Law Firm"
UpdateText
=
"Update Law Firm"
UniqueName
=
"EditCommandColumn1"
CancelText
=
"Cancel edit"
>
</
EditColumn
>
<
FormTableButtonRowStyle
HorizontalAlign
=
"Right"
CssClass
=
"EditFormButtonRow"
></
FormTableButtonRowStyle
>
</
EditFormSettings
>
</
MasterTableView
>
<
PagerStyle
Mode
=
"NextPrevAndNumeric"
/>
<
ClientSettings
>
<
ClientEvents
OnRowDblClick
=
"rowDblClick"
/>
</
ClientSettings
>
</
telerik:RadGrid
>
<
telerik:RadInputManager
runat
=
"server"
ID
=
"RadInputManager1"
Enabled
=
"true"
>
</
telerik:RadInputManager
>
<
telerik:RadWindowManager
ID
=
"RadWindowManager1"
runat
=
"server"
/>
</
asp:Content
>
manage.aspx.cs codebehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Linq;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using Telerik.Web.UI;
using THORMasterLibrary;
namespace THOR.Management
{
public partial class ManageFirms : System.Web.UI.Page
{
private THORDBConDataContext _dataContext;
protected THORDBConDataContext DbContext
{
get
{
if (_dataContext == null)
{
_dataContext = new THORDBConDataContext();
}
return _dataContext;
}
}
public override void Dispose()
{
if (_dataContext != null)
{
_dataContext.Dispose();
}
base.Dispose();
}
protected void selectState_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
{
if (Session["SelectedItem"] != null && e.Item.Text == Session["SelectedItem"].ToString())
{
e.Item.Selected = true;
}
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem edititem = (GridEditableItem)e.Item;
RadComboBox rcbStates = (RadComboBox)edititem.FindControl("selectState");
rcbStates.DataSource = DbContext.States;
rcbStates.DataTextField = "stateName";
rcbStates.DataValueField = "stateCode";
rcbStates.DataBind();
}
}
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = DbContext.LawFirms;
}
protected void RadGrid1_EditCommand(object source, GridCommandEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem edititem = (GridEditableItem)e.Item;
if (!(e.Item is IGridInsertItem))
{
Session["SelectedItem"] = edititem["stateName"].Text;
}
}
//if (e.Item is GridEditableItem && e.Item.IsInEditMode)
//{
// GridEditableItem edititem = (GridEditableItem)e.Item;
// if (!(e.Item is IGridInsertItem))
// {
// RadComboBox combo = (RadComboBox)edititem.FindControl("selectState");
// RadComboBoxItem preselectedItem = new RadComboBoxItem();
// preselectedItem.Text = edititem["stateName"].Text;
// preselectedItem.Value = edititem["stateCode"].Text;
// combo.Items.Insert(0, preselectedItem);
// combo.SelectedIndex = 0;
// }
//}
}
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
{
var editableItem = ((GridEditableItem)e.Item);
var ID = (int)editableItem.GetDataKeyValue("ID");
//retrive entity from the Db
var firms = DbContext.LawFirms.Where(n => n.ID == ID).FirstOrDefault();
if (firms != null)
{
//update entity's state
//GridEditableItem item = (GridEditableItem)e.Item;
//RadComboBox combo = (RadComboBox)item.FindControl("selectState");
//string value = combo.SelectedValue.ToString();
editableItem.UpdateValues(firms);
try
{
//submit chanages to Db
DbContext.SubmitChanges();
}
catch (System.Exception)
{
ShowErrorMessage();
}
}
}
/// <
summary
>
/// Shows a <
see
cref
=
"RadWindow"
/> alert if an error occurs
/// </
summary
>
private void ShowErrorMessage()
{
RadAjaxManager1.ResponseScripts.Add(string.Format("window.radalert(\"Please enter valid data!\")"));
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && (e.Item.IsInEditMode))
{
GridEditableItem editableItem = (GridEditableItem)e.Item;
SetupInputManager(editableItem);
}
}
private void SetupInputManager(GridEditableItem editableItem)
{
// style and set ProductName column's textbox as required
//var textBox = ((GridTextBoxColumnEditor)editableItem.EditManager.GetColumnEditor("FirmName")).TextBoxControl;
//InputSetting inputSetting = RadInputManager1.GetSettingByBehaviorID("TextBoxSetting1");
//inputSetting.TargetControls.Add(new TargetInput(textBox.UniqueID, true));
//inputSetting.InitializeOnClient = true;
//inputSetting.Validation.IsRequired = true;
}
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
{
var editableItem = ((GridEditableItem)e.Item);
//create new entity
var firm = new LawFirm();
//populate LawFirm
Hashtable values = new Hashtable();
editableItem.ExtractValues(values);
firm.FirmKey = Guid.NewGuid();
firm.FirmName = (string)values["FirmName"];
firm.FirstName = (string)values["FirstName"];
firm.MidName = (string)values["MidName"];
firm.LastName = (string)values["LastName"];
firm.Title = (string)values["Title"];
firm.Email = (string)values["Email"];
firm.Address = (string)values["Address"];
firm.Suite = (string)values["Suite"];
firm.City = (string)values["City"];
firm.County = (string)values["County"];
firm.State = (string)values["State"];
firm.PostalCode = (string)values["PostalCode"];
firm.Country = (string)values["Country"];
firm.HomePhone = (string)values["HomePhone"];
firm.BizPhone = (string)values["BizPhone"];
firm.CellPhone = (string)values["CellPhone"];
firm.Fax = (string)values["Fax"];
firm.Active = true;
firm.CreationDate = DateTime.Now;
DbContext.LawFirms.InsertOnSubmit(firm);
try
{
//submit chanages to Db
DbContext.SubmitChanges();
}
catch (System.Exception)
{
ShowErrorMessage();
}
}
protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
{
var ID = (int)((GridDataItem)e.Item).GetDataKeyValue("ID");
//retrive entity form the Db
var firms = DbContext.LawFirms.Where(n => n.ID == ID).FirstOrDefault();
if (firms != null)
{
//add the category for deletion
DbContext.LawFirms.DeleteOnSubmit(firms);
try
{
//submit chanages to Db
DbContext.SubmitChanges();
}
catch (System.Exception)
{
ShowErrorMessage();
}
}
}
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == Telerik.Web.UI.RadGrid.ExportToExcelCommandName ||
e.CommandName == Telerik.Web.UI.RadGrid.ExportToWordCommandName ||
e.CommandName == Telerik.Web.UI.RadGrid.ExportToCsvCommandName ||
e.CommandName == Telerik.Web.UI.RadGrid.ExportToPdfCommandName)
{
ConfigureExport();
}
}
public void ConfigureExport()
{
RadGrid1.ExportSettings.Excel.Format = GridExcelExportFormat.ExcelML;
RadGrid1.MasterTableView.ExportToExcel();
}
}
}
0
Hello,
Please find attached a sample project that implements very similar scenario. When user clicks the button for enabling edit mode for the RadGrid control, you are able to select a specific item from the RadComboBox control.
Boyan Dimitrov
Telerik
Please find attached a sample project that implements very similar scenario. When user clicks the button for enabling edit mode for the RadGrid control, you are able to select a specific item from the RadComboBox control.
Regards,
Boyan Dimitrov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
ADAPT
Top achievements
Rank 2
answered on 04 Jun 2013, 08:28 PM
Hi Boyan
Thank you for your post. I have tried to implement your solution but since I am using Dbcontext for database connectivity and not sqldatasource, I am unable to get it to work. Is there anyway to take my code in my last post and alter it so that I can change the state in edit mode. I am so confused now. Thanks for your help.
Thank you for your post. I have tried to implement your solution but since I am using Dbcontext for database connectivity and not sqldatasource, I am unable to get it to work. Is there anyway to take my code in my last post and alter it so that I can change the state in edit mode. I am so confused now. Thanks for your help.
0
ADAPT
Top achievements
Rank 2
answered on 09 Jul 2013, 03:53 PM
Hello
It has been more than a month now and I still do not have a solution to this issue. Is there no one that has some code example for DbContext with combo in grid that allows the user to se a preselected state if exists? Surely someone out there has done this already.
It has been more than a month now and I still do not have a solution to this issue. Is there no one that has some code example for DbContext with combo in grid that allows the user to se a preselected state if exists? Surely someone out there has done this already.
0
Hello,
It seems that your issue a bit more complicated and related to the database or the DbContext. At this point we are not able to inspect that behavior locally and find what might cause it. In this case I would suggest isolating your project following the steps from this blog post and open a support ticket. That you can attached that sample project and send us for further investigation.
Regards,
Boyan Dimitrov
Telerik
It seems that your issue a bit more complicated and related to the database or the DbContext. At this point we are not able to inspect that behavior locally and find what might cause it. In this case I would suggest isolating your project following the steps from this blog post and open a support ticket. That you can attached that sample project and send us for further investigation.
Regards,
Boyan Dimitrov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
ADAPT
Top achievements
Rank 2
answered on 07 Aug 2013, 01:36 PM
I would love to post a ticket but apparently my account is not allowing me to do so. any ideas?