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

RadGrid checkboxlist

10 Answers 145 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Windhoek2010
Top achievements
Rank 1
Windhoek2010 asked on 01 Aug 2014, 01:21 PM
Hello,

I have a field in my RadGrid where I want to be able to select multiple items. For example, field "Colors" will allow users to choose Red, Blue, Green, Yellow and/or Orange. Multiple selections allowed. How can this be implemented within RadGrid? I am aware of GridCheckBoxColumn but that only provides a single checkbox.

I'm open to using other selection controls, like combobox. 

Thank you.

10 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 01 Aug 2014, 02:09 PM
Hello Windhoek2010,

Could you please refer to the answer in the ticket you have opened for the same requirement (Ticket ID: 845615).

If you have any further questions I suggest that we continue our communication in the ticket.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Eyup
Telerik team
answered on 06 Aug 2014, 10:46 AM
Hello Windhoek2010,

You can use the EditItemTemplate of GridTemplateColumn provided by RadGrid to implement any custom editing functionality:
http://www.telerik.com/help/aspnet-ajax/grid-column-types.html#template

Or an entire new FormTemplate:
http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/form-template-update/defaultcs.aspx

Hope this helps. If you have different requirements or further instructions, please elaborate on your exact configuration and send us sample screenshots or video demonstrating the desired behavior. Thus, we will be able to figure out your specific scenario and suggest an accurate and more-to-point approach.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Princy
Top achievements
Rank 1
answered on 06 Aug 2014, 11:33 AM
Hi Windhoek2010,

I guess you want to have multiple selection for a column value in Edit mode. You can use the GridTemplateColumn to achieve this scenario. Please take a look at the following code snippet. Elaborate on your requirement if this doesn't help.

ASPX:
<telerik:GridTemplateColumn HeaderText="Colors">
    <ItemTemplate>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem Text="Red"></asp:ListItem>
            <asp:ListItem Text="Green"></asp:ListItem>
            <asp:ListItem Text="Blue"></asp:ListItem>
            <asp:ListItem Text="Yellow"></asp:ListItem>
        </asp:CheckBoxList>
    </EditItemTemplate>
</telerik:GridTemplateColumn>

Thanks,
Princy
0
Windhoek2010
Top achievements
Rank 1
answered on 12 Aug 2014, 02:53 AM
Thanks Eyup and Princy.

Princy, yes this is what I'm trying to achieve. I've placed a CheckBoxList within my RadGrid the way you showed me. However, the checkbox data is not getting updated in the table. Could you share a more comprehensive example of how CheckBoxList is used within RadGrid?

What should be the data type for the checkbox field in the table? Will it be saved as multiple, comma-separated values?

In my code below, I'm assuming the field to store the checkbox values ("Colors") is data type nvarchar(200).

<telerik:GridTemplateColumn DataField="Colors" DataType="System.String" HeaderText="Choose Colors">
<ItemTemplate>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBoxList ID="Colors" DataSourceID="SqlDataSource6" DataValueField="ColorID" DataTextField="ColorCode" runat="server"></asp:CheckBoxList>
</EditItemTemplate>
</telerik:GridTemplateColumn>






0
Princy
Top achievements
Rank 1
answered on 12 Aug 2014, 05:13 AM
Hi Windhoek2010,

Please check the below sample code, I have set the datatype as nvarchar(MAX), and depends on how you want to save it to db, you can use comma separated values.

C#:
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
    string values = string.Empty;
    string selectedvalue;
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem editItem = (GridEditableItem)e.Item;     
        CheckBoxList chk = (CheckBoxList)editItem.FindControl("Colors");
        for (int i = 0; i < chk.Items.Count; i++)
        {
         if (chk.Items[i].Selected)
         {
          //Storing the selected values
           values = values + "," + chk.Items[i].Text.ToString();
          }
        }
        selectedvalue = values.Trim(',');
      //update selectedvalue to db.
    }
}

Thanks,
Princy
0
Windhoek2010
Top achievements
Rank 1
answered on 12 Aug 2014, 06:53 AM
Hi Princy,

Thanks again. Please excuse my ignorance but could you also share what is the code to "updated selectedvalue to db"?
0
Princy
Top achievements
Rank 1
answered on 13 Aug 2014, 04:00 AM
Hi Windhoek2010,

Please take a look at the sample code snippet in which I update the db with selected values from CheckBoxList.

C#:
public static string connection = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connection);    
protected void rgrdSample_UpdateCommand(object sender, GridCommandEventArgs e)
{
    string values = string.Empty;
    string selectedvalue;
 
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem editItem = (GridEditableItem)e.Item;
        string OrderID = editItem.GetDataKeyValue("OrderID").ToString();
        CheckBoxList chk = (CheckBoxList)editItem.FindControl("chklistCountry");
        for (int i = 0; i < chk.Items.Count; i++)
        {
            if (chk.Items[i].Selected)
            {
                values = values + "," + chk.Items[i].Text.ToString();
            }
        }
        selectedvalue = values.Trim(',');
        conn.Open();
        string query = "UPDATE Orders SET ShipCountry='" + selectedvalue + "' WHERE OrderID = '" + OrderID + "'";
        SqlCommand cmd = new SqlCommand(query, conn);
        cmd.ExecuteNonQuery();
        conn.Close();
    }
}

Thanks,
Princy
0
Windhoek2010
Top achievements
Rank 1
answered on 13 Aug 2014, 05:25 AM
Thanks Princy. I am now able to update the nvarchar field with the checkbox values comma-separated.

Can you show me how to populate the checkboxes with these values when I am doing an edit? Do I modify the ItemDataBound method?

I've done this:

01.protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
02.{
03.    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
04.    {
05.        GridEditableItem editItem = (GridEditableItem)e.Item;
06.        CheckBoxList cb = (CheckBoxList)editItem.FindControl("Colors");
07.        //Split the Funding field (comma-separated) into an array
08.        //How do I find the field?
09.         
10.        for (int i = 0; i < cb.Items.Count; i++)
11.        {
12.            if (cb.Items[i].ToString() == [ARRAY Field])
13.            {
14.                cb.Items[i].Selected = true;
15.            }
16.        }
17.    }
18.}


How do I retrieve the field that contains the comma-separated list of Colors?

Thanks.
0
Princy
Top achievements
Rank 1
answered on 14 Aug 2014, 05:04 AM
Hi Windhoek2010,

Please try the following code snippet to set the items in checkbox list selected to the corresponding row values.

C#:
protected void rgrdSample_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem editItem = (GridEditableItem)e.Item;
        CheckBoxList Colors = (CheckBoxList)editItem.FindControl("Colors");         
        string value = ((DataRowView)e.Item.DataItem)["Colors"].ToString();
        string[] selectedColors= value.Split(',');
        for (int j = 0; j < Colors.Items.Count; j++)
        {
            for (int i = 0; i < selectedColors.Length; i++)
            {
                if (Colors.Items[j].Text == selectedColors[i])
                {
                    Colors.Items[j].Selected = true;
                }
            }
        }
    }
}

Thanks,
Princy
0
Windhoek2010
Top achievements
Rank 1
answered on 29 Aug 2014, 01:02 PM
Hi Princy,

Thanks very much. It works now.
Tags
Grid
Asked by
Windhoek2010
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Eyup
Telerik team
Princy
Top achievements
Rank 1
Windhoek2010
Top achievements
Rank 1
Share this question
or