Datagird checkbox populating

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Paulson
    New Member
    • Apr 2007
    • 63

    #1

    Datagird checkbox populating

    [code=vbnet]
    Dim i As Integer
    Dim dr As SqlDataReader
    Dim objDataAccess As New DataAccess.Data Access
    dr = objDataAccess.g etThirdLevelPre vileges(CInt(dd lUserGroup.Sele ctedValue))
    clearCheckedLis tBox()

    While dr.Read()

    If dr.Item("Status ") = True Then
    chkLevel3.Items .Add(dr.Item("L evel3"), True)
    Else
    chkLevel3.Items .Add(dr.Item("L evel3"))
    End If

    End While

    [/code]
    I am bringing value from data base and trying to bind in the above shown way.
    Some check boxes are not checked as per the database.
    Can anyone help me please?


    paulson
    Last edited by Frinavale; Mar 18 '08, 05:58 PM. Reason: added [code] tags
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    You need to loop through your datagrid and use the FindControl method to find the checkbox control in each row.

    Code:
     DataTable dataTable = dataset.Tables[0]; 
    for(int i = 0; i < dataGrid1.Items.Count; i ++)
    {
    DataRow row = dataTable.Rows[i];
    bool checkBoxChecked = Convert.ToBoolean(row["checkBoxValue"]);
    CheckBox chk = (CheckBox)dataGrid1.Items[i].FindControl("CheckBox1");
    if(checkBoxChecked)
    {
    chk.Checked = true;
    }
    else
    {
    chk.Checked = false;
    }
    }
    Give this a try.

    Nathan

    Comment

    Working...