Solved

Dynamically add checkboxes in gridview

Posted on 2010-11-22
4
763 Views
Last Modified: 2012-05-10
Hi,
i have a gridview which lists the project and shows for each project whether member of the specified group are stafed or not. I thus have 1 column for the project and would like to dynamically add one column per group member.
What I want to do is:
1 create the columns on page_load
2 bound the data for each colum on rowdatabound event

1 is working find and I create columns with user.initials as headertext
I'm not able to retrieve the checkbox on 2 and to bound the data

thks for your help
 
1. page_load

                Dim select_user_list = "select id_user, initials from [user] where id_group = " + ADTools.GetGroupID(ADTools.GetADUserGroups(My.User.Name, "time_")).ToString
                Dim ds_users = DataTools.DataSql(select_user_list)
                For Each objRow As DataRow In ds_users.Tables(0).Rows
                    Dim chkf_staf As New CheckBoxField
                    Dim user_initials = objRow("initials").ToString
                    chkf_staf.HeaderText = user_initials
                    chkf_staf.Text = False
                    gvStaff.Columns.Add(chkf_staf)

2. rowdatabound

            Dim chk_staf As CheckBox = CType(gvStaff.FindControl(user_initials), CheckBox)
            chk_staf.Checked = True

Open in new window

0
Comment
Question by:jeebee75
  • 2
4 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 34193681
Try this..

Dim chk_staf As CheckBox = CType(gvStaff.FindControl(chkf_staf), CheckBox)
0
 

Author Comment

by:jeebee75
ID: 34193999
hi,

i've tried that but it doesn't work. I finally found a partial solution to my problem in rowdatabound (my dynamic checkbox columns start at index 4):

            For i = 4 To gvStaff.Columns.Count - 1
                Dim chkBox = New CheckBox
                chkBox.Enabled = True
                e.Row.Cells(i).Controls.Add(chkBox)
                Dim user_initials = gvStaff.Columns.Item(i).ToString
                Dim test_staf = "select count(*) from staffing inner join [user] on staffing.id_user = [user].id_user where id_comp_proj = " + id_cp + " and [user].initials = '" + user_initials + "'"
                If DataTools.ExecScSql(test_staf) = 0 Then
                    chkBox.Checked = False
                Else : chkBox.Checked = True
                End If
            Next

This create checkbox and bind the correct value in it.
Now I want to catch the event when user change one of the checkboxes and make the relevant update in the database.
If you have any idea... I was thinking using rowcommand but I don't know how to retrieve the information from the chekkbox that has been modified (column index??).

Regards,

JBS
0
 
LVL 29

Accepted Solution

by:
Kumaraswamy R earned 500 total points
ID: 34194059
0
 

Author Closing Comment

by:jeebee75
ID: 34201921
works
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 5 115
Form design in vb.net 7 19
Hidden Field Value 10 34
Copy/Clone an object. 9 15
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now