Solved

Dynamically add checkboxes in gridview

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

21 Experts available now in Live!

Get 1:1 Help Now