Solved

Need to display an unbound checkbox for each record in gridview

Posted on 2006-07-02
4
1,489 Views
Last Modified: 2011-09-20
Using VWD Express, ASP.NET 2.0, VB.  I have a gridview on my web form.  In the Columns collection, I have identified one unbound checkbox field and several databound fields.  I use the below code to populate the gridview dynamically.

The unbound field does not appear (in design or browser views) with a checkbox.  The idea is to have several unbound checkboxes that would auto-postback onclick, and allow the codebehind to build an array of selections (using the non-visible ID field from the gridview).

How can I display and use an unbound checkbox for each row in the gridview?


    Protected Sub doPopulateGridviewSubjectCompetitors(Optional ByVal strOrderByFieldName As String = "SubjectID")
       
        Dim datatableSubjectCompetitors As New DataTable("datatableSubjectCompetitors")

        Dim intRows As Integer
        Dim strSql As String
        Dim objConn As OleDb.OleDbConnection
        Dim objCmd As OleDb.OleDbCommand
       
        objConn = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings.Item("connFloratrak").ConnectionString)

        strSql = "PARAMETERS subjRad Long, subjLat IEEEDouble, subjLng IEEEDouble; SELECT Subjects.SubjectID, Subjects.BusinessName, Subjects.City, Subjects.StateCode FROM (Subjects) WHERE (((Subjects.Zip) In (SELECT Zip FROM ZipCodes WHERE [subjRad] > 3959 * ATN( SQR(1 - (SIN([subjLat]/ 57.3) * SIN(LAT/ 57.3) + COS([subjLat]/ 57.3) * COS(LAT/ 57.3) * COS((LNG/ 57.3) - ([subjLng]/ 57.3))) ^2 ) / (SIN([subjLat]/ 57.3) * SIN(LAT/ 57.3) + COS([subjLat]/ 57.3) * COS(LAT/ 57.3) * COS((LNG/ 57.3) - ([subjLng]/ 57.3))) ) )))"
        strSql &= " ORDER BY " & strOrderByFieldName
       
        objCmd = New OleDbCommand(strSql, objConn)
        setParam(objCmd, "subjRad", OleDbType.BigInt, CLng(vRad.Text))
        setParam(objCmd, "subjLat", OleDbType.Double, CDbl(vLat.Text))
        setParam(objCmd, "subjLng", OleDbType.Double, CDbl(vLng.Text))
       
        Dim dataadapterSubjectCompetitors As New OleDbDataAdapter(objCmd)

        dataadapterSubjectCompetitors.GetFillParameters()
       
        dataadapterSubjectCompetitors.Fill(datatableSubjectCompetitors)
       
        intRows = datatableSubjectCompetitors.Rows.Count
       
        With gridviewSubjectCompetitors
            If intRows > 0 Then
                '.EnableSortingAndPagingCallbacks = False
                .DataSource = datatableSubjectCompetitors
                .DataBind()
                .Visible = True
            Else
                .Visible = False
            End If
        End With

    End Sub
0
Comment
Question by:psk1
  • 3
4 Comments
 
LVL 12

Accepted Solution

by:
deanvanrooyen earned 500 total points
ID: 17028288
I am not realy sure what you are asking -            

to display a checkbox
 <Columns>
                 <asp:TemplateField>
                     <ItemTemplate>
                          <asp:CheckBox ID="chkbox" runat="server" Text='<%# System.Convert.ToBoolean(Eval("BoolDataField")) %>' Checked='<%# System.Convert.ToBoolean(Eval("BoolDataField")) %>' />
                     </ItemTemplate>
                </asp:TemplateField>
            </Columns>

to display a checkbox without binding it
maybe
http://www.gridviewguy.com/ArticleDetails.aspx?articleID=178
0
 
LVL 2

Author Comment

by:psk1
ID: 17028560
Within the gridview I have:

                        <Columns>
                            <asp:CheckBoxField HeaderText="Select">
                                <ItemStyle Width="50px" />
                            </asp:CheckBoxField>

I'll try using the template tags for the field.. and I'll check out your link.
0
 
LVL 2

Author Comment

by:psk1
ID: 17031190
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'BoolDataField'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'BoolDataField'.

Source Error:


Line 423:                        <asp:TemplateField>
Line 424:                             <ItemTemplate>
Line 425:                                  <asp:CheckBox ID="chkbox" runat="server" Text='<%# System.Convert.ToBoolean(Eval("BoolDataField")) %>' Checked='<%# System.Convert.ToBoolean(Eval("BoolDataField")) %>' />
Line 426:                             </ItemTemplate>
Line 427:                        </asp:TemplateField>
 
0
 
LVL 2

Author Comment

by:psk1
ID: 17031212
The link you posted did the trick:

<asp:TemplateField  HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>

Thanks!
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
asp.net mvc return json 2 56
Windows 10 IIS and Windows Authentication to SQL 21 54
Allow space in this pattern 2 48
Tool Box 2 35
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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