Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Controls in datagrid header???

aymanmadkouraymanmadkour Posts: 6Member
Hi... How can I access a control (say, a checkbox) in the header of a template column in a datagrid? any ideas?

Comments

  • iwilld0itiwilld0it Posts: 1,134Member
    : Hi... How can I access a control (say, a checkbox) in the header of a template column in a datagrid? any ideas?
    :

    This was actually harder than I thought. Initially I thought maybe you could get to the Header within the DataGrid.Items collection. However, this collection only holds Item and AlternatingItem DataGridItem objects (No header DataGridItem.)

    To get to the header you must use the DataGrid.Controls collection. The catch is that there is only one child control in this collection that holds a reference to a private DataGridTable object. So you have to furthur navigate into this control's Control collection. So to get a reference to the header you must do this ...

    [code]
    Dim header As DataGridItem = _
    CType(dgrd.Controls(0).Controls(0), DataGridItem)
    [/code]

    Pretty ugly ... anyways, now the next step is to get a reference to a control in the header ... This is accomplished with the FindControl function ...

    [code]
    Dim chk As CheckBox = _
    CType(header.FindControl("chk"), CheckBox)
    [/code]

    Notice, that you have to use the FindControl function off the header object ...

    Anyways, here is a function that demonstrates how to check the state of a checkbox in the header of a datagrid named dgrd ...

    [code]
    Public Function GetCheckBoxState(ByVal cbName As String) As Boolean
    Dim header As DataGridItem = _
    CType(dgrd.Controls(0).Controls(0), DataGridItem)

    Dim chk As CheckBox = _
    CType(header.FindControl(cbName), CheckBox)

    If chk Is Nothing Then
    Return False
    End If

    Return chk.Checked
    End Function
    [/code]

    It takes the name of a check box and returns true or false depending on if the check box was checked.

Sign In or Register to comment.