Solved

How to add checkbox programatically in Gridview?

Posted on 2009-05-19
8
650 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
Comment Utility
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
Comment Utility
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
Comment Utility
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
Comment Utility
You need to replace "Subject" with some other field like "IsCurrent" or whatever but a boolean field.
0
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

 

Author Comment

by:tangteng78
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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