Solved

How to add checkbox programatically in Gridview?

Posted on 2009-05-19
8
662 Views
Last Modified: 2013-12-17
Hi,
I'm able to add button programmatically, BUT not checkbox. Any ideas?
gvwInbox.AllowSorting = True

                gvwInbox.AllowPaging = True

                gvwInbox.PageSize = 10
 

                Dim btnView1 As ButtonField = New ButtonField()

                btnView1.ButtonType = ButtonType.Button

                btnView1.Text = "View"

                btnView1.CommandName = "View"
 

                Dim chk As CheckBoxField = New CheckBoxField

                chk.DataField = "subject"
 

                'Add in web controls

                gvwInbox.Columns.Add(btnView1)

                gvwInbox.Columns.Add(chk)
 

                gvwInbox.DataSource = objInbox.GetInbox(User.Identity.Name.ToString)

                gvwInbox.DataBind()

Open in new window

0
Comment
Question by:tangteng78
  • 4
  • 2
  • 2
8 Comments
 
LVL 4

Expert Comment

by:RamanaChoudary
ID: 24424845
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24424892
Make sure the database field set using DataField property of the CheckBoxField is a boolean field rather than a textual one, e.g. Bit (SQL Server) or Yes/No (Access). I just tested it here and it works with boolean fields only.
0
 
LVL 4

Expert Comment

by:RamanaChoudary
ID: 24424900
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24424910
You need to replace "Subject" with some other field like "IsCurrent" or whatever but a boolean field.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:tangteng78
ID: 24428750
The links doesn't provide the insight on how to add checkbox programatically.
However, i put in the checkbox in the itemtemplate. See below.

how do i access the "subject" value if i selected on certain checkbox on a click of a button (the button defined separate outside of the gridview)?

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        For Each row As GridViewRow In gvwInbox.Rows

            Dim cb As CheckBox = row.FindControl("chk")
           
            'I'm stuck here...do not know how to get the 'subject' value on specific checkbox checked.

        Next

    End Sub

<asp:GridView ID="gvwInbox" runat="server" AutoGenerateColumns="false" AllowSorting="true" >

                <Columns>

                    <asp:TemplateField>

                        <HeaderTemplate>

                            <%#Eval("subject")%>

                        </HeaderTemplate>

                        <ItemTemplate>

                        <table>

                            <tr>

                                <td>

                                    <asp:CheckBox ID="chk" runat="server" /></td>

                                <td><%#Eval("subject")%></td>

                            </tr>

                            <tr>

                                <td>

                                    <%#Eval("body")%>

                                </td>

                            </tr>

                        </table>

                        </ItemTemplate>

                    </asp:TemplateField>

                </Columns>

            </asp:GridView>

Open in new window

0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24428775
Please use the snippet below to get the checkbox, and then use its Checked property to evaluate if it is checked or not. Make sure if you use multiple ItemTemplates, pass the correct index position value into the Cells() collection.

Dim chkTemp As CheckBox = CType(row.Cells(0).FindControl("chk"), CheckBox)

Open in new window

0
 

Author Comment

by:tangteng78
ID: 24428827
can you elaborate on this?

"Make sure if you use multiple ItemTemplates, pass the correct index position value into the Cells() collection."
0
 
LVL 11

Accepted Solution

by:
Muhammad Ousama Ghazali earned 500 total points
ID: 24428980
Right now you have declared only one TemplateField and most probably it will be the first if you are generating other columns automatically, hence making its index position within Columns or Cells collection 0. That is why my code has Cells(0) but in case you need to have more fields whether TemplateFields or BoundFields, make sure to pass the correct index postiion based upon the number of columns. Maybe the fact that I used the word ItemTemplates rather than TemplateFields has given wrong understanding above.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Name space syntax error 12 44
ASP.net Have sliders change in order 2 10
Capture logon name 13 48
Automated testing suggestions? 2 29
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

23 Experts available now in Live!

Get 1:1 Help Now