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

Fill checkbox list on edit mode radgrid

3 Answers 203 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 03 Apr 2012, 06:28 PM
I am trying to fill and edit Checkboxlist with values as in the datagrid i combine all those values into one field to show better, long story.  Anyway i need to first populate all teh avialable checkbox list items to choose from, got this done but next I need to hit the database and get the checkboxlist items that are checked already and populate that.  So far unsuccessfull.

Protected Sub myRadGrid_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles myRadGrid.ItemDataBound
       If TypeOf e.Item Is GridEditableItem And e.Item.IsInEditMode Then
           Dim Item As GridEditableItem = CType(e.Item, GridEditableItem)
           Dim cbl As CheckBoxList = Item.FindControl("cblReg")
           Dim Id As Integer = e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("intCategoryId")
           'populate the checkboxlist will all avialable items
           sql = "Select COLUMN_NAME from ITSystems.INFORMATION_SCHEMA.COLUMNS where Table_name = 'Drat_Registration' and COLUMN_NAME not like 'int%'"
           myDataTable = New DataTable
           myDataTable = getData(sql)
           cbl.DataSource = myDataTable
           cbl.DataTextField = "COLUMN_NAME"
           cbl.DataValueField = "COLUMN_NAME"
           cbl.DataBind()
           'get the existing items that should be checked when editing
           sql = "Select strFieldName from Drat_regRelation where intCategoryId = " & Id
           myDataTable = New DataTable
           myDataTable = getData(sql)
           For Each row As DataRow In myDataTable.Rows
               For Each Item In cbl.Items
                   If row(0) = cbl.DataValueField Then
                       cbl.SelectedValue = True
                   End If
               Next
           Next
       End If
   End Sub

<telerik:RadGrid ID="myRadGrid" runat="server" Width="100%" BorderWidth="1px" CellPadding="6" GridLines="None" BorderColor="#404040" Skin="Web20">
                                        <MasterTableView AutoGenerateColumns="false" DataKeyNames="Category" Name="MasterGrid" BorderColor="#404040" Font-Size="9" Font-Names="Veranda,arial,sans-serif" 
                                        HeaderStyle-HorizontalAlign="Center" GridLines="Both" BorderWidth="1px"><AlternatingItemStyle BackColor="#B0C4DE" />
                                        <HeaderStyle ForeColor="White" Font-Bold="true" BorderColor="#404040" BorderWidth="1px" />
                                            <Columns>
                                                 <telerik:GridEditCommandColumn UniqueName="EditCommandColumn"></telerik:GridEditCommandColumn>
                                                <telerik:GridBoundColumn DataField="Category" HeaderText="Category" />
                                                <telerik:GridBoundColumn DataField="DataFields" HeaderText="REG DATA" />
                                            </Columns>
                                             <EditFormSettings EditFormType="Template">
                                                <FormTemplate>
                                                    <table width="100%">
                                                        <tr>
                                                            <td style="height:8px"></td>
                                                        </tr>
                                                        <tr>
                                                            <td><b>EDIT DETAILS</b></td>
                                                        </tr>
                                                    </table>
                                                    <table width="100%">
                                                        <tr>
                                                            <td align="left"><asp:CheckBoxList ID="cblReg" runat="server" DataTextField="strCategory" DataValueField="intCategoryId"></asp:CheckBoxList></td>
                                                        </tr>
                                                        <tr>
                                                            <td style="height:5px"></td>
                                                        </tr>
                                                    </table>
                                                    <table width="100%">
                                                        <tr>
                                                            <td>
                                                                <asp:LinkButton ID="lnkSubmit" runat="server" text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>' 
                                                                CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>'></asp:LinkButton>
                                                                      
                                                                <asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="false" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td style="height:8px"></td>
                                                        </tr>
                                                    </table>
                                                </FormTemplate>
                                            </EditFormSettings>
                                        </MasterTableView>
                                     </telerik:RadGrid>



3 Answers, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 1
answered on 03 Apr 2012, 07:02 PM
ok changed my code behind to work as follwoing and I checked the values to make sure them match and they do, but nothing gets checked.
Protected Sub myRadGrid_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles myRadGrid.ItemDataBound
       If TypeOf e.Item Is GridEditableItem And e.Item.IsInEditMode Then
           Dim Item As GridEditableItem = CType(e.Item, GridEditableItem)
           Dim cbl As CheckBoxList = Item.FindControl("cblReg")
           Dim Id As Integer = e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("intCategoryID")
           'populate the checkboxlist will all avialable items
           sql = "Select COLUMN_NAME from ITSystems.INFORMATION_SCHEMA.COLUMNS where Table_name = 'Drat_Registration' and COLUMN_NAME not like 'int%'"
           myDataTable = New DataTable
           myDataTable = getData(sql)
           cbl.DataSource = myDataTable
           cbl.DataTextField = "COLUMN_NAME"
           cbl.DataValueField = "COLUMN_NAME"
           cbl.DataBind()
           'get the existing items that should be checked when editing
           sql = "Select strFieldName from Drat_regRelation where intCategoryId = " & ID
           myDataTable = New DataTable
           myDataTable = getData(sql)
           For Each row As DataRow In myDataTable.Rows
               For Each Items As ListItem In cbl.Items
                   If (row(0).ToString = Items.Value.ToString) Then
                       Item.Selected = True
                   End If
               Next
           Next
       End If
   End Sub
0
Accepted
Shinu
Top achievements
Rank 2
answered on 04 Apr 2012, 09:37 AM
Hi Kevin,

Please take a look into the following code snippet I tried and worked as expected.

VB:
Protected Sub grid1_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs)
    If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode Then
        Dim item As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
        Dim ck As CheckBoxList = DirectCast(item.FindControl("CheckBoxList1"), CheckBoxList)
        Dim Id As String = item.OwnerTableView.DataKeyValues(item.ItemIndex)("EmployeeID").ToString()
        'populate the checkboxlist will all avialable items
        Dim selectQuery As String = "select EmployeeID from Employees"
        Dim adapter As New SqlDataAdapter(selectQuery, conn)
        Dim dt As New DataTable()
        conn.Open()
        adapter.Fill(dt)
        conn.Close()
        ck.DataSource = dt
        ck.DataTextField = "EmployeeID"
        ck.DataValueField = "EmployeeID"
        ck.DataBind()
 
        Dim selectQuery2 As String = "select EmployeeID from EmployeeDetails where EmployeeID='" + Id + "'"
        Dim adapter1 As New SqlDataAdapter(selectQuery2, conn)
        Dim dt1 As New DataTable()
        conn.Open()
        adapter1.Fill(dt1)
        conn.Close()
        For Each row As DataRow In dt1.Rows
            For Each Items As ListItem In ck.Items
                If (row.ItemArray(0).ToString() = Items.Value.ToString()) Then
                    Items.Selected = True
                End If
            Next
        Next
    End If
End Sub

Thanks,
Shinu.
0
Kevin
Top achievements
Rank 1
answered on 04 Apr 2012, 01:34 PM
Shinu,
thnsk for the response, as soon as I looked at your code I saw that I put in item instead of items, sometimes it just takes a day of no banging your head agianst a desk to get rid of the headache.  Thanks for the help.
Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or