Home All Groups Group Topic Archive Search About

Adding checkbox to datagrid headers

Author
18 Nov 2005 4:34 PM
Victor Heijke
Can anyone help me with this.
I have a datagrid on a page which is used to show different views from
SQL-Server.
With autogenerate columns, this works fine for me. But now I want a checkbox
before every caption in the header. With this checkbox I'm goint to determine
if the user wants that field in his/her final print or export.

I'm able to add the checkbox on designtime by using Itemtemplates, but I can
not get this to work on runtime.

Thanks in advance

Author
21 Nov 2005 12:06 AM
Calvin KD
Hi Victor,
Shouldn't you be adding the checkbox control in the
<HeaderTemplate></HeaderTemplate> instead of <ItemTemplate>? then in the
ItemDataBound event of the datagrid, write:
public void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Header)
    {
        CheckBox myCheckbox = (CheckBox)e.Item.FindControl("CheckBox1");
        if (myCheckbox != null)
        {
            // Inject clientside click event to capture the checkbox's value
and put it into a hidden field txtColumnSelected
            myCheckbox.Attributes.Add("onclick",
"document.all.txtColumnSelected.value = " + myCheckbox.checked);
        }
    }
}

Hope that's what you're looking for.
Calvin.

Show quote
"Victor Heijke" wrote:

> Can anyone help me with this.
> I have a datagrid on a page which is used to show different views from
> SQL-Server.
> With autogenerate columns, this works fine for me. But now I want a checkbox
> before every caption in the header. With this checkbox I'm goint to determine
> if the user wants that field in his/her final print or export.
>
> I'm able to add the checkbox on designtime by using Itemtemplates, but I can
> not get this to work on runtime.
>
> Thanks in advance
Author
21 Nov 2005 9:06 AM
Victor Heijke
Thanks for your reaction, you are right.
I'm not even able to add the items to the header on runtime.
If I use something like this in ItemDataBound

        Dim chkBox As CheckBox
        Try

            If e.Item.ItemType = ListItemType.Header Then
                chkBox = New CheckBox
                e.Item.Controls.Add(chkBox)
            End If
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try

than I get a 'TableRow' cannot have children of type 'CheckBox'. error

Any ideas?

Victor

Show quote
"Calvin KD" wrote:

> Hi Victor,
> Shouldn't you be adding the checkbox control in the
> <HeaderTemplate></HeaderTemplate> instead of <ItemTemplate>? then in the
> ItemDataBound event of the datagrid, write:
> public void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
> {
>     if (e.Item.ItemType == ListItemType.Header)
>     {
>         CheckBox myCheckbox = (CheckBox)e.Item.FindControl("CheckBox1");
>         if (myCheckbox != null)
>         {
>             // Inject clientside click event to capture the checkbox's value
> and put it into a hidden field txtColumnSelected
>             myCheckbox.Attributes.Add("onclick",
> "document.all.txtColumnSelected.value = " + myCheckbox.checked);
>         }
>     }
> }
>
> Hope that's what you're looking for.
> Calvin.
>
> "Victor Heijke" wrote:
>
> > Can anyone help me with this.
> > I have a datagrid on a page which is used to show different views from
> > SQL-Server.
> > With autogenerate columns, this works fine for me. But now I want a checkbox
> > before every caption in the header. With this checkbox I'm goint to determine
> > if the user wants that field in his/her final print or export.
> >
> > I'm able to add the checkbox on designtime by using Itemtemplates, but I can
> > not get this to work on runtime.
> >
> > Thanks in advance
Author
23 Nov 2005 3:00 AM
Calvin KD
Hi Victor,
First of all, I'd like to establish that you're using .Net Framework 1.1
aren't you? because my solution only works with 1.1, not 2.0.
OK, I always use an HTML Table rather than the Webcontrol table. Here's what
i do:
<asp:DataGrid ...>
    <HeaderTemplate>
        <Table id="..." ... runat="server">
            <tr>
                <td width="10%"><asp:CheckBox id="CheckBox1" ...> Column1
Header</td>
                <td width="10%"><asp:CheckBox id="CheckBox2" ...> Column2
Header</td>
                ...
            </tr>
        </Table>
    </HeaderTemplate>
</asp:DataGrid>

The check boxes are created at design time rather than at runtime. Once
that's done, use the solution that i previously proposed to try and obtain
the values from those checkboxes.

Let me know if still have problems. More than happy to help you out.
Cheers,
Calvin
Show quote
"Victor Heijke" wrote:

> Thanks for your reaction, you are right.
> I'm not even able to add the items to the header on runtime.
> If I use something like this in ItemDataBound
>
>         Dim chkBox As CheckBox
>         Try
>
>             If e.Item.ItemType = ListItemType.Header Then
>                 chkBox = New CheckBox
>                 e.Item.Controls.Add(chkBox)
>             End If
>         Catch ex As Exception
>             Response.Write(ex.Message)
>         End Try
>
> than I get a 'TableRow' cannot have children of type 'CheckBox'. error
>
> Any ideas?
>
> Victor
>
> "Calvin KD" wrote:
>
> > Hi Victor,
> > Shouldn't you be adding the checkbox control in the
> > <HeaderTemplate></HeaderTemplate> instead of <ItemTemplate>? then in the
> > ItemDataBound event of the datagrid, write:
> > public void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
> > {
> >     if (e.Item.ItemType == ListItemType.Header)
> >     {
> >         CheckBox myCheckbox = (CheckBox)e.Item.FindControl("CheckBox1");
> >         if (myCheckbox != null)
> >         {
> >             // Inject clientside click event to capture the checkbox's value
> > and put it into a hidden field txtColumnSelected
> >             myCheckbox.Attributes.Add("onclick",
> > "document.all.txtColumnSelected.value = " + myCheckbox.checked);
> >         }
> >     }
> > }
> >
> > Hope that's what you're looking for.
> > Calvin.
> >
> > "Victor Heijke" wrote:
> >
> > > Can anyone help me with this.
> > > I have a datagrid on a page which is used to show different views from
> > > SQL-Server.
> > > With autogenerate columns, this works fine for me. But now I want a checkbox
> > > before every caption in the header. With this checkbox I'm goint to determine
> > > if the user wants that field in his/her final print or export.
> > >
> > > I'm able to add the checkbox on designtime by using Itemtemplates, but I can
> > > not get this to work on runtime.
> > >
> > > Thanks in advance

AddThis Social Bookmark Button