Solved

Dynamically add checkboxes in gridview

Posted on 2010-11-22
4
765 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

11 Experts available now in Live!

Get 1:1 Help Now