Hello,
I have a RadGrid that binds to a collection of custom object. In my application users can insert/update the rows of the grid and the changes will not be persisted to the database until they click on a save button. Each row has a key column containing the key value (ProviderLanguageID) assigned to each of the object when data is persisted to DB.
The grid's markup is like this:
<
telerik:RadGrid
ID
=
"grdProviderLanguage"
runat
=
"server"
AutoGenerateColumns
=
"False"
Height
=
"140px"
Width
=
"360px"
OnNeedDataSource
=
"grdProviderLanguage_NeedDataSource"
oninsertcommand
=
"grdProviderLanguage_InsertCommand"
onitemcommand
=
"grdProviderLanguage_ItemCommand"
CellSpacing
=
"0"
GridLines
=
"None"
onupdatecommand
=
"grdProviderLanguage_UpdateCommand"
onitemdatabound
=
"grdProviderLanguage_ItemDataBound"
>
<
ClientSettings
>
<
Selecting
AllowRowSelect
=
"True"
/>
<
KeyboardNavigationSettings
AllowActiveRowCycle
=
"True"
/>
<
Scrolling
AllowScroll
=
"True"
UseStaticHeaders
=
"True"
/>
</
ClientSettings
>
<
MasterTableView
CommandItemDisplay
=
"Top"
InsertItemDisplay
=
"Top"
EditMode
=
"InPlace"
EnableNoRecordsTemplate
=
"false"
DataKeyNames
=
"ProviderLanguageID"
>
<
CommandItemSettings
AddNewRecordImageUrl
=
"Images/AddNew.png"
AddNewRecordText
=
""
ShowRefreshButton
=
"false"
></
CommandItemSettings
>
<
RowIndicatorColumn
Visible
=
"False"
FilterControlAltText
=
"Filter RowIndicator column"
>
<
HeaderStyle
Width
=
"20px"
></
HeaderStyle
>
</
RowIndicatorColumn
>
<
ExpandCollapseColumn
Visible
=
"False"
FilterControlAltText
=
"Filter ExpandColumn column"
>
<
HeaderStyle
Width
=
"20px"
></
HeaderStyle
>
</
ExpandCollapseColumn
>
<
EditFormSettings
>
<
EditColumn
FilterControlAltText
=
"Filter EditCommandColumn column"
></
EditColumn
>
</
EditFormSettings
>
<
Columns
>
<
telerik:GridTemplateColumn
HeaderText
=
"Language"
UniqueName
=
"ProviderLanguage"
DataField
=
"ProviderLanguageID"
>
<
HeaderStyle
Width
=
"170px"
/>
<
ItemTemplate
>
<
asp:Label
ID
=
"lblLanguageName"
runat
=
"server"
Text='<%# Eval("LanguageObject.LanguageName") %>' />
</
ItemTemplate
>
<
EditItemTemplate
>
<
telerik:RadComboBox
ID
=
"cmbProviderLanguage"
runat
=
"server"
width
=
"130px"
EnableLoadOnDemand
=
"true"
DataValueField
=
"LanguageID"
DataTextField
=
"LAnguageObject.LanguageName"
OnItemsRequested
=
"cmbProviderLanguage_ItemsRequested"
Text='<%# Eval("LanguageObject.LanguageName") %>'>
</
telerik:RadComboBox
>
</
EditItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridCheckBoxColumn
HeaderText
=
"Primary"
UniqueName
=
"IsPrimary"
DataField
=
"IsPrimary"
/>
<
telerik:GridEditCommandColumn
ButtonType
=
"LinkButton"
InsertText
=
"Done"
EditText
=
"Edit"
CancelText
=
"Cancel"
UpdateText
=
"Done"
/>
</
Columns
>
</
MasterTableView
>
<
FilterMenu
EnableImageSprites
=
"False"
></
FilterMenu
>
</
telerik:RadGrid
>
When multiple rows are inserted to the grid, and one of the rows is subsequently edited (before saved to DB), the edited row is accessible in UpdateCommand event like this:
Guid editedItemID = new Guid(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ProviderLanguageID"].ToString());
I'm using the key value to locate the actual custom object in the collection stored in the page.
This works if each of the record already has a key. When a new record is created, the key value is empty (Guid.Empty). So we'd have to use different means to locate the custom object. I'm thinking of comparing the SavedOldValues with the saved values to locate the actual object.
I notcied that e.Item.DataItem is null in UpdateCommand event, but not in ItemDataBound event. Is there any way we can access the custom object the row being updated is bound to?
Thanks in advance,
Makoto