Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am developing an application using Rad Controls.In this there is a rad grid which contains checkbox,on selection of it based on the id i am loading relevant data to the another radGrid next to it in the same page.
For the first time selection data is getting loaded properly,But on the next checkbox selection i am getting the error an item with the same key has already been added.

Sharing the code
ASP.NET
<telerik:RadGrid ID="radGrid1" AutoGenerateColumns="false" AllowPaging="true"  runat="server"
                ShowHeader="true" AllowAutomaticUpdates="true" AllowAutomaticInserts="true" AllowAutomaticDeletes="true"
                Width="43%" Style="float: left;" Visible="true" >
                <MasterTableView DataKeyNames="USERNAME,USERID">
                    <Columns>
                        <telerik:GridTemplateColumn>
                            <ItemTemplate>
                                <asp:CheckBox ID="UserSelect" runat="server" HeaderText="Select" AutoPostBack="true"
                                    OnCheckedChanged="UserSelect_CheckedChanged" />
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridBoundColumn UniqueName="USERID" DataField="USERID" HeaderText="User Id"
                            Visible="false">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="USERNAME" DataField="USERNAME" HeaderText="User Name">
                        </telerik:GridBoundColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>

Checkbox change event
C#
protected void UserSelect_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    GridDataItem itm = (GridDataItem)chk.NamingContainer;
    RadRoles.DataSource = new string[] { };
    RadRoles.DataBind();

    foreach (GridDataItem row in radGrid1.MasterTableView.Items)
    {
        CheckBox chk1 = (CheckBox)row.FindControl("UserSelect");

        if (chk1.Checked == true)
        {
            DataTable dt = null;
            //dt will fetch the values from the DB
            RadRoles.DataSource = dt.DefaultView;
            RadRoles.DataBind();
        }
    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900