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

GridCheckBoxColumn with RadGrid

6 Answers 414 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David Kucharski
Top achievements
Rank 1
David Kucharski asked on 11 Feb 2013, 06:19 PM
I am buiding my RadGrid in the Code-Behind because it could change upon a post-back. I am adding my columns one at a time and binding them to the columns within my SqlDataSource. One of the types is the GridCheckBoxColumn.

I set the DataField and HeaderText. The DataField is a column that is a BIT type in Sql Server. I even tried TinyInt.

When I run my program I get an error stating 'String was not recognized as a valid Boolean'.

I am setting the RadGrid's DataSourceID to a asp:SqlDataSource that is a select statement.

How do I get around this error?

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 12 Feb 2013, 07:26 AM
Hi,

I have tried the same scenario with GridCheckBoxColumn bound to a boolean field(bit type in sql server) and it is working fine.  Here is the sample code that I tried which worked as expected.
protected void Page_Init(object sender, System.EventArgs e)
{
       RadGrid1 = new RadGrid();
      RadGrid1.ID = "RadGrid1";
      GridCheckBoxColumn check;
      check = new GridCheckBoxColumn();
      RadGrid1.MasterTableView.Columns.Add(check);
      check.DataField = "bitField";
      check.HeaderText = "bitField";
      PlaceHolder1.Controls.Add(RadGrid1);
}


Check the following post which discusses the same issue. Please try the following code in ItemDataBound event as explained in the forum.

Thanks,
Shinu
0
David Kucharski
Top achievements
Rank 1
answered on 12 Feb 2013, 05:18 PM
Thanks for the insight but unfortunately I am still having issues. To clarify what I am trying to do, I have added my code. The grid will not be created on my page until I click a submit button. Here is my aspx code.

<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" />
    <asp:Button ID="submit" Text="submit" runat="server" />
    <telerik:RadGrid ID="RadGrid1" runat="server"></telerik:RadGrid>
    <asp:SqlDataSource ID="sds1" ProviderName="System.Data.SqlClient" runat="server" />
    </form>
</body>

Upon clicking the submit button the following code is fired.

On Page_Load, I set up my SqlDataSource and set the AJAX Settings.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        sds1.ConnectionString = ConfigurationManager.ConnectionStrings("iAnalyzeConnectionStringTest").ConnectionString
        sds1.SelectCommand = "SELECT [ID], [VendorListID], [Geography], [CirculationTypeDescription] " & _
  ",[HouseHold_Count] ,[TotalCoverage] ,[TotalCirculationForGeography] ,[LocationMarket] ,[LocationName]" & _
  ",[LocationNumber] ,[Distance] ,[Sales] ,[TotalSales] ,[BOS] ,[CumeBOS] ,[SPH] ,[SPHIndex]" & _
  ",[SundaySelected] ,[SundayApplied] ,[Sunday] ,[SundayForced] ,[SundayCoverage] ,[SundayZones] ,[SundayVersion]" & _
      ",[SundayVersionID] ,[SundayVersionOverrideInd] ,[SundayDeliveryCode] ,[SundayDeliveryCodeID] ,[SundaySubVersionCategory] " & _
      "FROM [iBuy].[iBuy].[TMP_WB_GeographySelectionDetails204738]"
        sds1.DataBind()
  
        RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadGrid1, RadGrid1)
    End Sub

On the Submit Click, I am setting up my RadGrid1 and calling a second procedure to create my columns. In the column create procedure I am either adding a CheckBoxColumn or BoundColumn
Private Sub submit_Click(sender As Object, e As System.EventArgs) Handles submit.Click
       
     Dim sundayGroup As GridColumnGroup = New GridColumnGroup
     RadGrid1.MasterTableView.ColumnGroups.Add(sundayGroup)
     sundayGroup.HeaderText = "Sunday"
     sundayGroup.Name = "sundayColumns"
     sundayGroup.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
     RadGrid1.DataSourceID = "sds1"
     RadGrid1.AllowPaging = True
     RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric
     RadGrid1.AutoGenerateColumns = False
     RadGrid1.ShowStatusBar = True
     RadGrid1.AllowSorting = True
     RadGrid1.MasterTableView.PageSize = 100
     RadGrid1.ClientSettings.Scrolling.AllowScroll = True
     RadGrid1.ClientSettings.Scrolling.UseStaticHeaders = True
     RadGrid1.ClientSettings.Scrolling.SaveScrollPosition = True
     RadGrid1.MasterTableView.Width = Unit.Percentage(100)
     RadGrid1.MasterTableView.DataKeyNames = New String() {"VendorListID"}
     columnCreation("Geography", "Geography", RadGrid1)
     columnCreation("Circ Type", "CirculationTypeDescription", RadGrid1)
     columnCreation("Household", "HouseHold_Count", RadGrid1)
     columnCreation("Total Cov", "TotalCoverage", RadGrid1)
     columnCreation("Location Market", "LocationMarket", RadGrid1)
     columnCreation("Location Number", "LocationNumber", RadGrid1)
     columnCreation("Location Name", "LocationName", RadGrid1)
     columnCreation("Distance", "Distance", RadGrid1)
     columnCreation("Sales", "Sales", RadGrid1)
     columnCreation("Total Sales", "TotalSales", RadGrid1)
     columnCreation("BOS", "BOS", RadGrid1)
     columnCreation("Cume BOS", "CumeBOS", RadGrid1)
     columnCreation("SPH", "SPH", RadGrid1)
     columnCreation("SPH Index", "SPHIndex", RadGrid1)
     columnCreation("Selected", "SundaySelected", RadGrid1)
     columnCreation("Forced", "SundayForced", RadGrid1)
     columnCreation("Applied Circ", "SundayApplied", RadGrid1)
     columnCreation("Circ", "Sunday", RadGrid1)
     columnCreation("Cov", "SundayCoverage", RadGrid1)
     columnCreation("Zone", "SundayZones", RadGrid1)
     columnCreation("Version", "SundayVersion", RadGrid1)
     columnCreation("Sub-Version", "SundaySubVersionCategory", RadGrid1)
     columnCreation("Delivery Code", "SundayDeliveryCode", RadGrid1)
 End Sub
Private Sub columnCreation(ByVal colName As String, ByVal fieldName As String, ByVal oRadGrid As RadGrid)
        If colName = "Selected" Or colName = "Forced" Then
            Dim columnCheckBox As New GridCheckBoxColumn
            oRadGrid.MasterTableView.Columns.Add(columnCheckBox)
            columnCheckBox.DataField = fieldName
            columnCheckBox.HeaderText = colName
  
            If Left(fieldName, 6) = "Sunday" Then
                columnCheckBox.ColumnGroupName = "sundayColumns"
            End If
        Else
            Dim column As New GridBoundColumn
            oRadGrid.MasterTableView.Columns.Add(column)
            column.DataField = fieldName
            column.HeaderText = colName
            If Left(fieldName, 6) = "Sunday" Then
                column.ColumnGroupName = "sundayColumns"
            End If
        End If
    End Sub

then I have my RadGrid1_ItemDataBound procedure. The funny thing is, the system never gets into the If statement because it is always seeing the e.Item as a GridPagerItem instead of a GridDataItem. If I comment out the code under the If block in the columnCreation function, the RadGrid1_ItemDataBound procedure recognizes the e.Item as a GridDataItem, but since I did not set the cell type to GridCheckBoxColumn, I cannot do what you were having me do.
Private Sub RadGrid1_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
        If (TypeOf (e.Item) Is GridDataItem) Then
            Dim dataBoundItem As GridDataItem = e.Item
            Dim dataRow As DataRowView = dataBoundItem.DataItem
            Dim chk As CheckBox = dataBoundItem("SundaySelected").Controls(0)
  
            Dim chkValue As String = dataRow("SundaySelected").ToString()
  
            If chkValue = "1" Then
                chk.Checked = True
            Else
                chk.Checked = False
            End If
  
            If dataBoundItem("TotalCoverage").Text > 0 And UCase(Request("TargetGeo")) <> "TRUE" Then
                dataBoundItem("TotalCoverage").Attributes.Add("onClick", "Javascript:fnOpenGeoInfoView('" & dataBoundItem("Geography").Text.ToString.Trim & "'," & 1 & "," & 2 & ")")
                dataBoundItem("TotalCoverage").CssClass = "GridLink"
            End If
        End If
    End Sub

Please let me know what I am doing wrong.

Thanks again.
0
Kostadin
Telerik team
answered on 14 Feb 2013, 12:24 PM
Hello,

Note that if you want to change the RadGrid structure dynamically you have to do that on Page_Init. You could check out the following help article where is demonstrated how you can achieve that.  

All the best,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Shinu
Top achievements
Rank 2
answered on 14 Feb 2013, 12:26 PM
Hi David,

I have created a sample with your approach and it is working fine at my end. It is firing ItemDataBound event and binding the corresponding data to CheckBoxColumn. Please have a look into the following code.

Please note here my DataField 'IsApproved' is of datatype bit in my database.

ASPX:
<form id="form1" runat="server">
   <div>
       <asp:ScriptManager ID="scr" runat="server">
       </asp:ScriptManager>
       <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" />
       <asp:Button ID="submit" Text="submit" runat="server" onclick="submit_Click" />
       <telerik:RadGrid ID="RadGrid1" runat="server"
           onitemdatabound="RadGrid1_ItemDataBound">
       </telerik:RadGrid>
       <asp:SqlDataSource ID="sds1" ProviderName="System.Data.SqlClient" runat="server" />
   </div>
   </form>

VB:
Protected Sub Page_Load(sender As Object, e As EventArgs)
    sds1.ConnectionString = ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString
    sds1.SelectCommand = "SELECT ID,Name,IsApproved FROM BigField"
    sds1.DataBind()
    RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadGrid1, RadGrid1)
End Sub
Protected Sub submit_Click(sender As Object, e As EventArgs)
    RadGrid1.DataSourceID = "sds1"
    RadGrid1.AllowPaging = True
    RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric
    RadGrid1.AutoGenerateColumns = False
    RadGrid1.ShowStatusBar = True
    RadGrid1.AllowSorting = True
    RadGrid1.MasterTableView.PageSize = 100
    RadGrid1.ClientSettings.Scrolling.AllowScroll = True
    RadGrid1.ClientSettings.Scrolling.UseStaticHeaders = True
    RadGrid1.ClientSettings.Scrolling.SaveScrollPosition = True
    RadGrid1.MasterTableView.Width = Unit.Percentage(100)
    RadGrid1.MasterTableView.DataKeyNames = New String() {"ID"}
    columnCreation("First Name", "Name", RadGrid1)
    columnCreation("Approved", "IsApproved", RadGrid1)
End Sub
 
 
Private Sub columnCreation(colName As String, fieldName As String, oRadGrid As RadGrid)
    If colName = "Approved" Then
        Dim columnCheckBox As New GridCheckBoxColumn()
        oRadGrid.MasterTableView.Columns.Add(columnCheckBox)
        columnCheckBox.UniqueName = fieldName
        columnCheckBox.DataField = fieldName
        columnCheckBox.HeaderText = colName
    Else
        Dim column As New GridBoundColumn()
        oRadGrid.MasterTableView.Columns.Add(column)
        column.UniqueName = fieldName
        column.DataField = fieldName
        column.HeaderText = colName
    End If
End Sub
 
 
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim dataBoundItem As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim dataRow As DataRowView = DirectCast(dataBoundItem.DataItem, DataRowView)
        Dim chk As CheckBox = DirectCast(dataBoundItem("IsApproved").Controls(0), CheckBox)
        Dim chkValue As String = dataRow("IsApproved").ToString()
        If chkValue = "True" Then
            chk.Checked = True
        Else
            chk.Checked = False
        End If
    End If
End Sub

-Shinu.

0
David Kucharski
Top achievements
Rank 1
answered on 19 Feb 2013, 03:38 PM
Shinu,

Thank you for the help and I am going to try this. One further question, if I click the checkbox on/off will the value persist upon paging or sorting? The reason I ask is I read documentation that you need to setup the grid through the page_init for viewstate to persist.

Let me know.
0
Kostadin
Telerik team
answered on 22 Feb 2013, 11:48 AM
Hello David,

As I already mentioned in my previous reply you could check out the following help topic where is demonstrated how to achieve your scenario.

Regards,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
David Kucharski
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
David Kucharski
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or