This is a migrated thread and some comments may be shown as answers.

Editing/Inserting a databound RadGrid checkbox item reverts to value stored in database

3 Answers 265 Views
Grid
This is a migrated thread and some comments may be shown as answers.
D
Top achievements
Rank 1
D asked on 13 Oct 2011, 03:08 PM
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:

<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;
                }
            }
        }

3 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 14 Oct 2011, 08:06 AM
Hello D,

Do make sure that the correct values is coming from the data-base. Is you need to skip the condition just add make it as follows:
if (e.Item is GridDataItem && !e.Item.IsInEditMode)

Hope it helps.

Best wishes, Tsvetoslav
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
D
Top achievements
Rank 1
answered on 17 Oct 2011, 05:24 PM
Hello Tsvetoslav,

Thank your for your suggestion.  Unfortunately, the modification you suggested didn't resolve this issue.  Both segments of this if / else if are being hit on update (i.e. nothing changed).  It appears as if the IsInEditMode is false at first, causing the value to be reset to the db value prior to the update code being hit.  Here is the updated code:  

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 if (e.Item is GridDataItem && !e.Item.IsInEditMode)
          {
               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;
           }
        }

Is there an example of a checkbox column in a radgrid that allows for updating?  Thanks again for your guidance!
0
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Oct 2011, 07:19 PM
Hello,

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnItemDataBound="RadGrid1_ItemDataBound"
            OnNeedDataSource="RadGrid1_NeedDataSource"
            onupdatecommand="RadGrid1_UpdateCommand" >
            <MasterTableView DataKeyNames="ID">
            <Columns>
                 <telerik:GridBoundColumn UniqueName="ID" DataField="ID">
                    </telerik:GridBoundColumn>
                    <telerik:GridCheckBoxColumn   HeaderText="Checked" UniqueName="CheckedColumn"></telerik:GridCheckBoxColumn>
                    <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
            </Columns>
            </MasterTableView>
        </telerik:RadGrid>
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
 
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem item = e.Item as GridEditableItem;
        string strId = item.GetDataKeyValue("ID").ToString();
        if (strId == "2")
        {
            (item["CheckedColumn"].Controls[0] as CheckBox).Checked = true;
        }
         
    }
}
 
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("ID",typeof(String));
    dt.Columns.Add("IsChecked", typeof(Boolean));
    dt.Rows.Add(1,true);
    dt.Rows.Add(2,true);
    dt.Rows.Add(3,true);
 
    RadGrid1.DataSource = dt;
 
}
 
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
    GridEditableItem item = e.Item as GridEditableItem;
    bool flag = (item["CheckedColumn"].Controls[0] as CheckBox).Checked;
    Response.Write(flag.ToString());
}


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
D
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
D
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Share this question
or