Hi,
I' having some problems with binding data to a checkbox in a WebDataGrid.
I tried a few things myself, and for some reason I just cant get it to work, Basicly i'm looking for something like:
preview of my WebDataGrid: http://img41.imageshack.us/f/permissionsb.png/
I got Users, wich on check get permissions, so when I check an checkbox for a user in his row, OnClick add permission I want to add permission by wich loopin through the WebDataGrid and all checkboxes wich are checked need to be added.
If any1 could explain how to do this, or show me an example
I'd really appriciate it
Dim cb As CheckBox Dim dgi As DataGridItem Dim idList As String = ""
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click For Each dgi In WebDataGrid1.Items cb = CType(dgi.Cells(CB_Algemeen).Controls(1), CheckBox) If cb.Checked Then Dim permissionController As New Permissions.PermissionController() Dim chkList As CheckBoxList = DirectCast(sender, CheckBoxList) chkList.DataSource = permissionController.GetPermissionByCodeAndKey("SYSTEM_TAB", "VIEW") chkList.DataTextField = "PermissionName" chkList.DataValueField = "PermissionID" chkList.DataBind() End If Next End Sub
<ig:WebDataGrid ID="WebDataGrid1" runat="server" EnableAjax="False" Height="350px" Width="637px" AutoGenerateColumns="False"> <Columns> <ig:BoundDataField DataFieldName="UserID" Key="UserID" Hidden="True"> <Header Text="UserID" /> </ig:BoundDataField> <ig:BoundDataField DataFieldName="Username" Key="Username" Width="90px"> <Header Text="Username" /> </ig:BoundDataField> <ig:TemplateDataField Key="Algemeen" Width="90px"> <ItemTemplate> <div style="text-align: center;"> <asp:CheckBox runat="server" ID="Algemeen" /></div> </ItemTemplate> <HeaderTemplate> <div style="text-align: center;"> <asp:Label ID="Label3" runat="server" Text="Algemeen"></asp:Label> <input type="checkbox" name="SelectAlgemeen" onclick="Check(this);" /> </div> </div> </HeaderTemplate> </ig:TemplateDataField> <ig:TemplateDataField Key="Financieel" Width="90px"> <ItemTemplate> <div style="text-align: center;"> <asp:CheckBox runat="server" ID="Financieel" /></div> </ItemTemplate> <HeaderTemplate> <div style="text-align: center;"> <asp:Label ID="Label3" runat="server" Text="Financieel"></asp:Label> <input type="checkbox" name="SelectFinancieel" onclick="Check(this);" /> </div> </div> </HeaderTemplate> </ig:TemplateDataField> <ig:TemplateDataField Key="Licentie" Width="90px"> <ItemTemplate> <div style="text-align: center;"> <asp:CheckBox runat="server" ID="Licentie" /></div> </ItemTemplate> <HeaderTemplate> <div style="text-align: center;"> <asp:Label ID="Label3" runat="server" Text="Licentie"></asp:Label> <input type="checkbox" name="SelectLicentie" onclick="Check(this);" /> </div> </div> </HeaderTemplate> </ig:TemplateDataField> <ig:TemplateDataField Key="Applicatie" Width="90px"> <ItemTemplate> <div style="text-align: center;"> <asp:CheckBox runat="server" ID="Applicatie" /></div> </ItemTemplate> <HeaderTemplate> <div style="text-align: center;"> <asp:Label ID="Label3" runat="server" Text="Applicatie"></asp:Label> <input type="checkbox" name="SelectApplicatie" onclick="Check(this);" /> </div> </div> </HeaderTemplate> </ig:TemplateDataField> <ig:TemplateDataField Key="UitDienst" Width="90px"> <ItemTemplate> <div style="text-align: center;"> <asp:CheckBox runat="server" ID="UitDienst" /></div> </ItemTemplate> <HeaderTemplate> <div style="text-align: center;"> <asp:Label ID="Label3" runat="server" Text="Uit Dienst"></asp:Label> <input type="checkbox" name="SelectUitDienst" onclick="Check(this);" /> </div> </div> </HeaderTemplate> </ig:TemplateDataField> </Columns></ig:WebDataGrid>
Visual Basic Code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim userInfo As UserInfo userInfo = UserController.GetCurrentUserInfo() If userInfo.UserID = -1 Then End If Company = userInfo.Profile.GetPropertyValue("Company") Dim strSQLconnection As String = ("Data Source=.\SQLExpress;Initial Catalog=dnn2;User ID=vb1;Password=Klompsrv1") cSQL = "SELECT Users.UserID, Users.Username, UserProfile.PropertyValue FROM ProfilePropertyDefinition INNER JOIN UserProfile ON ProfilePropertyDefinition.PropertyDefinitionID = UserProfile.PropertyDefinitionID INNER JOIN Users ON UserProfile.UserID = Users.UserID WHERE (ProfilePropertyDefinition.PropertyName = 'Company') AND (UserProfile.PropertyValue = '" + Company + "')" Dim sqlConnection As New SqlConnection(strSQLconnection) Dim sqlCommand As New SqlCommand(cSQL, sqlConnection) Dim SqlAdapter As New SqlDataAdapter(sqlCommand) Dim DataSet As New DataSet() SqlAdapter.Fill(DataSet) If sqlConnection.State = ConnectionState.Closed Then sqlConnection.Open() End If WebDataGrid1.DataSource = DataSet WebDataGrid1.DataBind() End Sub
That's true, you can cast something that's not there. Your if statement should be if x=(the column index of your checkbox) then cast...
You're also right that this is not the easiest way to work with checkboxes in a grid. Infragistics is coming out with a checkbox provider for this grid in their first release of 2011. Then, it will work smoothly. Until then, you have to work with one of these workarounds.
Ed
creatieven said: Ed I noticed the following that for each item of each row, FindControl will return a null reference for all columns except the one which contains the desired checkbox. When FindControl returns a null reference, you cannot cast it to a CheckBox - hence the null reference error. Also, this is a very inefficient way of iterating through the role checkboxes. Aint there a more sefisticaded way of doing this? Sebastian
Ed I noticed the following that for each item of each row, FindControl will return a null reference for all columns except the one which contains the desired checkbox. When FindControl returns a null reference, you cannot cast it to a CheckBox - hence the null reference error. Also, this is a very inefficient way of iterating through the role checkboxes.
Aint there a more sefisticaded way of doing this?
Sebastian
Hi ed,
When pressin btnUpdate it returns a error message ' Object reference not set to an instance of an object' and I cant figure out what i'm doing wrong
For i As Integer = 0 To WebDataGrid1.Rows.Count - 1 For x As Integer = 0 To WebDataGrid1.Columns.Count - 1 If DirectCast(WebDataGrid1.Rows(i).Items(x).FindControl("Algemeen"), CheckBox).Checked() Then If DirectCast(WebDataGrid1.Rows(i).Items.Item(1).Value, objusername) Then Dim User As System.Security.Principal.IPrincipal User = System.Web.HttpContext.Current.User If Not User.IsInRole("Algemeen") Then Dim rc As New DotNetNuke.Security.Roles.RoleController() Dim ri As DotNetNuke.Security.Roles.RoleInfo = rc.GetRoleByName(PortalId, "Algemeen") Dim ui As DotNetNuke.Entities.Users.UserInfo = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo() rc.AddUserRole(PortalId, ui.UserID, ri.RoleID, Null.Null End If
End If
Username you can access directly with it's column index which is 1 since it's the second column:
webdatagrid.rows(rowindex).items.item(1).value
You only need the findcontrol and directcast when working with a template column which contains other controls like a checkbox.
how would it be for a username then?
I trierd DirectCast(WebDataGrid.Rows(rowindex).Items(columnindex).FindControl("Username").Value()
but that doesnt work
Crea