This seems like pretty common functionality, so I'm hoping that there is a simple answer, but I've tried a hundred different ways. Basically, I have a RadGrid that contains an editable checkbox field. This field is populated based on a boolean database field (using information gained from this link, I was able to get this to display properly). This field is editable, and when in edit mode, I am able to check the box. However, when I click "Update", the ItemDataBound event fires again and resets the checkbox item to the value in the database. I have tried to combine this with the method I found here to determine if the Item is InEditMode and skip the "set the checkbox value" part of the code, but this process hits the "else" part of the script first (resetting the value to the database value). I'd certainly appreciate any guidance!
Here is my aspx code:
and here is the ItemDataBound segment of my code behind page where I attempted to combine the two methods above:
Here is my aspx code:
<telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" AllowSorting="True" AllowAutomaticDeletes="true" AllowPaging="True" PageSize="20" runat="server" GridLines="None" Width="95%" AllowAutomaticUpdates="true" OnItemDataBound="RadGrid1_ItemDataBound" AllowMultiRowEdit="true" OnItemUpdate="RadGrid1_ItemUpdated" OnItemDeleted="RadGrid1_ItemDeleted" OnItemInserted="RadGrid1_ItemInserted"> <MasterTableView Width="100%" CommandItemDisplay="Top" HorizontalAlign="NotSet" AutoGenerateColumns="false"> <Columns> <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn"> <ItemStyle CssClass="MyImageButton" /> </telerik:GridEditCommandColumn> <telerik:GridButtonColumn ConfirmText="Remove this subscription?" ConfirmDialogType="RadWindow" ConfirmTitle="Unsubscribe" ButtonType="ImageButton" CommandName="Delete" Text="Unsubscribe" UniqueName="DeleteColumn"> <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" /> </telerik:GridButtonColumn> <telerik:GridCheckBoxColumn HeaderText="Subscribed" UniqueName="Subscribed" /> <telerik:GridDropDownColumn DataField="SubscriptionId" DataSourceID="SqlDataSource1" HeaderText="Subscription" ListTextField="SubscriptionName" ListValueField="SubscriptionId" UniqueName="SubscriptionId" ColumnEditorID="GridDropDownColumnEditor1" ReadOnly="true"> </telerik:GridDropDownColumn> <telerik:GridBoundColumn DataField="SubscriptionDescription" HeaderText="SubscriptionDescription" UniqueName="SubscriptionDescription" ReadOnly="true"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="TargetAudience" HeaderText="TargetAudience" UniqueName="TargetAudience" ReadOnly="true"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="DateAdded" HeaderText="DateAdded" UniqueName="DateAdded" ColumnEditorID="GridTextBoxColumnEditor1" ReadOnly="true"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="DateRemoved" HeaderText="DateRemoved" UniqueName="DateRemoved" ColumnEditorID="GridTextBoxColumnEditor1" ReadOnly="true"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="Feedback" HeaderText="Feedback" UniqueName="Feedback" ColumnEditorID="GridTextBoxColumnEditor1"> </telerik:GridBoundColumn> </Columns> <PagerStyle Mode="NextPrevNumericAndAdvanced" /> </MasterTableView></telerik:RadGrid>and here is the ItemDataBound segment of my code behind page where I attempted to combine the two methods above:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) { if (e.Item is GridEditFormItem && e.Item.IsInEditMode) { if (e.Item.OwnerTableView.IsItemInserted) { //item is about to be inserted } else { //item is about to be updated } } else {
//need to find a way to skip this if we are in Edit mode, as this resets any saved data to the db values
if (e.Item is GridDataItem) { GridDataItem item = (GridDataItem)e.Item; DataRowView row = (DataRowView)item.DataItem; CheckBox chk = (CheckBox)item["Subscribed"].Controls[0]; string value = row["Subscribed"].ToString(); if (value == "Yes" || (value == "1")) chk.Checked = true; else chk.Checked = false; } } }