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

RadComboBox in RadGrid with SqlDataSource

1 Answer 282 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ruben
Top achievements
Rank 1
Ruben asked on 09 Mar 2012, 06:44 PM
Hi there,

I have a problem with the Grid not returning the text I have in my RadComboBox.

I have got a grid with a GridTemplateColumn, with a RadComboBox and checkboxes like so:
<telerik:GridTemplateColumn HeaderText="Roles" UniqueName="Roles"> 
    <EditItemTemplate> 
        <telerik:RadComboBox ID="RadComboBox1" DataSourceID="SqlDataSource3" AllowCustomText="true" OnSelectedIndexChanged="OnSelectedIndexChanged"
        DataTextField="RoleName" DataValueField="RoleName" runat="server">
            <ItemTemplate>
                <div class="combo-item-template">
                    <asp:CheckBox runat="server" ID="chk1" OnCheckedChanged="onCheckBoxClick" AutoPostBack="True" />
                    <asp:Label runat="server" ID="Label1" AssociatedControlID="chk1" Text='<%# Eval("RoleName")%>'>
                    <%# Eval("RoleName")%>
                    </asp:Label>
                </div>
            </ItemTemplate>
        </telerik:RadComboBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label runat="server" ID="Label2"><%# Eval("RoleNames")%></asp:Label>
    </ItemTemplate>
</telerik:GridTemplateColumn>


And using a sql datasource and using the getuserlist stored procedure to retrieve the roles :
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SqlConnection %>" OnInserting="SqlDataSource_OnInserting"
SelectCommandType="StoredProcedure"
SelectCommand="GetUserList"
 
InsertCommandType="StoredProcedure"
InsertCommand="dbo.CreateUser" 
>
<InsertParameters>
    <asp:Parameter Name="UserName" Type="String" />
    <asp:Parameter Name="Password" Type="String" />
    <asp:Parameter Name="IsApproved" Type="Byte" />
    <asp:Parameter Name="IsLockedOut" Type="Byte" />
    <asp:Parameter Name="Email" Type="String" />
    <asp:Parameter Name="Roles" Type="String" />
    <asp:Parameter Name="UserId" DbType="Guid" Direction="Output" />
</InsertParameters>

Roles come back as a comma separated string and the procedure that inserts them can handle it like: 'Role1, Role2' as parameter

But in edit/insert mode, I need to fill the RadComboBox and the checkboxes so I can select any of the ones I have.
<asp:SqlDataSource ID="SqlDataSource3" runat="server" 
ConnectionString="<%$ ConnectionStrings:Sql_Azure %>" 
SelectCommand="SELECT RoleId, RoleName FROM Roles ORDER BY RoleName">
</asp:SqlDataSource>

So I have a oncheckboxclick that modfies the RadComboBox

protected void onCheckBoxClick(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    RadComboBox combobox = (RadComboBox)chk.Parent.Parent;
 
    string text = BuildText(combobox);
    combobox.Text = text;
}

So this all works until I check the roles in the command parameters:
protected void SqlDataSource_OnInserting(object sender, SqlDataSourceCommandEventArgs e)
{
       DbParameterCollection CmdParams = e.Command.Parameters;
}
And I can see the Roles parameter is empty. What I am doing wrong? I think I am not setting the griddataitem, so I looked into it...

And I have seen use reading the text property of the griddataitem with the following, so I tried to modify the text
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
      GridDataItem dataItem = (GridDataItem)combobox.Parent.Parent;
      dataItem["Roles"].Text = text;
}
But that doesn't work, because the Text property is readonly.

For updating I need to do the same thing obviously, plus then I need to set the initial state. I don't know which event I can use.
Thank you so much, been puzzling on this for a long time... :S

1 Answer, 1 is accepted

Sort by
0
Richard
Top achievements
Rank 1
answered on 13 Mar 2012, 03:39 PM
Ruben:

I faced a similar issue in that the text of the selection that I made in a dropdownlist imbedded in an EditItemTemplate when editing existing or inserting new records was not updating the database. I found that setting the Text property of the dropdownlist as follows solved it: Text='<%# Bind("Rating") %>'

<%-- This is the Rating Column--%>
<telerik:GridTemplateColumn DataField="Rating" HeaderText="Rating" Groupable="true"
    GroupByExpression="Rating Group by Rating" SortExpression="Rating">
    <ItemTemplate>
        <asp:Label ID="lblRating" runat="server"><%# Eval("Rating") %></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:DropDownList DataMember="Rating" ID="dllRating" runat="server" Text='<%# Bind("Rating") %>'
            SelectedValue='<%# DataBinder.Eval(Container.DataItem, "Rating") %>'>
            <asp:ListItem Value="" Text="Not Rated" />
            <asp:ListItem Value="1" Text="1" />
            <asp:ListItem Value="2" Text="2" />
            <asp:ListItem Value="3" Text="3" />
            <asp:ListItem Value="4" Text="4" />
            <asp:ListItem Value="5" Text="5" />
        </asp:DropDownList>
        Star(s)
    </EditItemTemplate>
</telerik:GridTemplateColumn>

Hope this helps!

Tags
Grid
Asked by
Ruben
Top achievements
Rank 1
Answers by
Richard
Top achievements
Rank 1
Share this question
or