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

Grid - Check and Check All

3 Answers 72 Views
Grid
This is a migrated thread and some comments may be shown as answers.
PAYDAY
Top achievements
Rank 1
PAYDAY asked on 19 Feb 2009, 07:20 PM

Requirements

RadControls version

.NET version

3.5
Visual Studio version

2008
programming language

C#
browser support

all browsers supported by RadControls


PROJECT DESCRIPTION
This code can be used on a RadGrid to allow Check/Uncheck of items. If a single row checkbox is checked, it posts back saving the checked value to the database. If the CheckAll is selected, it loops through saving the checked value of each row to the applicable row in the database.

Assumptions:
- Using a custom Entity model
    - all methods to get and save data on entity model (NOT INCLUDED IN CODE BELOW)

- You have a field in the database called "IsActive"

---- GRID HTML -----
OnPreRender="RadGrid1_PreRender"

<Columns>
        <telerik:GridTemplateColumn UniqueName="CheckBoxColumn" HeaderText="Blocked">
                <HeaderTemplate>
                        <asp:CheckBox ID="CheckAll" AutoPostBack="true" OnCheckedChanged="CheckAll_CheckChanged" runat="server" />
                </HeaderTemplate>
                <ItemTemplate>
                        <asp:CheckBox id="CheckBox1" OnCheckedChanged="CheckBox1_CheckChanged" AutoPostBack="True"  runat="server"></asp:CheckBox>
                </ItemTemplate>
        </telerik:GridTemplateColumn>
</Columns>


------ CODE BEHIND --------
#region Members
    DataTable _gridData = null;
#endregion

#region Properties
    /// <summary>
    /// locally cache grid data table - did this so I could set the checkbox values
    /// </summary>
    private DataTable GridData
    {
        get
        {
            if (_gridData == null)
            {
                _gridData = MyEntity.GetGridData();
            }

            return _gridData;
        }
    }

#endregion

#region Custom Methods

    private void CheckAll(string checkBoxValue)
    {
        foreach (GridItem item in RadGrid1.Items)
        {            
            SaveCheckValueChanges(item, checkBoxValue);
        }
    }

    /// <summary>
    /// Updates entity with current Checked value
    /// </summary>
    /// <param name="item"></param>
    private void SaveCheckValueChanges(GridItem item)
    {
        SaveCheckValueChanges(item, null);
    }

    /// <summary>
    /// Updates entity with current Checked value
    /// </summary>
    /// <param name="item"></param>
    private void SaveCheckValueChanges(GridItem item, string defaultBlockedValue)
    {
        if (item is GridEditableItem)
        {
            CheckBox IsActive = item.FindControl("IsActive") as CheckBox;
            string ID = item.OwnerTableView.DataKeyValues[item.ItemIndex][MyEntity.DBFieldName.ID.ToString()].ToString();
            MyEntity entity = MyEntity.GetByPrimaryKey(ID);
            
            if (!String.IsNullOrEmpty(defaultBlockedValue))
            {
                entity.IsActive = defaultBlockedValue;
            }
            else
            {
                entity.IsActive = IsActive.Checked.ToString();
            }

            entity.SaveData();
        }
    }

    /// <summary>
    /// Update grid checkboxes. Set checked=true for each selected item
    /// </summary>
    /// <param name="dt">DataTable representing bound grid items</param>
    public void SetSelectedGridItems(DataTable dt)
    {
        foreach (DataRow dr in dt.Rows)
        {
            foreach (GridItem item in RadGrid1.Items)
            {
                GridDataItem dataitem = (GridDataItem)item;
                if (dataitem[MyEntity.DBFieldName.ID.ToString()].Text.Equals(dr[MyEntity.DBFieldName.ID.ToString()].ToString()))
                {
                    //check row
                    CheckBox checkBox = item.FindControl(MyEntity.DBFieldName.IsActive.ToString()) as CheckBox;
                    checkBox.Checked = ConvertValueToBoolean(dr[MyEntity.DBFieldName.IsActive.ToString()].ToString());
                    break;
                }
            }
        }
        
    }
   
#endregion


#region Page Events
    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
                RadGrid1.DataSource = this.GridData;
                RadGrid1.MasterTableView.DataKeyNames = new string[] { MyEntity.DBFieldName.ID.ToString() };
    }

    protected void CheckAll_CheckChanged(object sender, EventArgs e)
    {
            CheckBox checkBox = sender as CheckBox;
            if (checkBox != null)
            {
                    CheckAll(checkBox.Checked.ToString());
            }
    }
    
    protected void CheckBox1_CheckChanged(object sender, EventArgs e)
    {
            GridItem item = (sender as CheckBox).Parent.Parent as GridItem;
            SaveCheckValueChanges(item);
    }


    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
            //use locally cached data table to set IsActive checkboxes on grid
            SetSelectedGridItems(this.GridData);
    }
    
#endregion

3 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 23 Feb 2009, 01:31 PM
Hello Richard,

Thank you for posting the code.
I have transferred it to a forum post, since it is not of the complexity level required for a code library, so that other community members can benefit from it as well.
Thank you for your involvement!

Regards,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
PAYDAY
Top achievements
Rank 1
answered on 23 Feb 2009, 07:29 PM
I don't mind sharing but I don't understand what you mean by "it is not of the complexity level required for a code library". Naturally, I could create an entire project to facilitate this functionality but did not know that was required. Please explain - and feel free to email the response to me.
0
Yavor
Telerik team
answered on 25 Feb 2009, 11:12 AM
Hi Richard,

Basically, we are trying to sort out the projects to some degree, as to ensure that the code library does not grow out of control, rendering its usage very difficult. Nevertheless, I am confident the community will benefit from this code as well, regardless of it being located in the forums.

Best wishes,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
PAYDAY
Top achievements
Rank 1
Answers by
Yavor
Telerik team
PAYDAY
Top achievements
Rank 1
Share this question
or