Solved

itemTemplate within a Gridview not displaying in run mode

Posted on 2008-06-12
6
652 Views
Last Modified: 2013-11-26
I could not figure out why the itemtemplate for chkbox ID = "Lunch"  column is not displaying.The chkbox Id = "Select"  column is displaying.
thanks >
Here's the source

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="592px" style="left: 7px; position: relative; top: 5px">
        <Columns>
        <asp:TemplateField HeaderText="Select">
            <ItemTemplate>
            <asp:CheckBox ID ="chkSelect" runat="server" />
            </ItemTemplate>
       </asp:TemplateField>
            <asp:CommandField ButtonType="Button" CausesValidation="False" ShowSelectButton="True" />
            <asp:TemplateField HeaderText="Lunch">
            <ItemTemplate>
            <asp:CheckBox ID ="chkLunch" runat="server" Enabled="true" />
            </ItemTemplate>
         </asp:TemplateField>
                <asp:BoundField DataField="MASTERID" HeaderText="ID" />
                <asp:BoundField DataField="FULLNAME" HeaderText="FullName" />
                <asp:BoundField DataField="CATEGORY" HeaderText="Category" />
      </Columns>
        <AlternatingRowStyle BackColor="Azure" />
    </asp:GridView></form>
0
Comment
Question by:zachvaldez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 22

Accepted Solution

by:
prairiedog earned 400 total points
ID: 21772795
Well, I don't see anything wrong in your code. Besides, I made a page with your code above, and everything is displaying fine. I added a SqlDataScource connecting to Northwind database so that I can display those BoundField. Please see the attached screen shot and attached code snippet.

There must be something else that hides the check box.
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="GridViewDemo.aspx.vb" Inherits="WebApplication1.GridViewDemo" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="592px" style="left: 7px; position: relative; top: 5px" DataSourceID="SqlDataSource1">
        <Columns>
        <asp:TemplateField HeaderText="Select">
            <ItemTemplate>
            <asp:CheckBox ID ="chkSelect" runat="server" />
            </ItemTemplate>
       </asp:TemplateField>
            <asp:CommandField ButtonType="Button" CausesValidation="False" ShowSelectButton="True" />
            <asp:TemplateField HeaderText="Lunch">
            <ItemTemplate>
            <asp:CheckBox ID ="chkLunch" runat="server" Enabled="true" />
            </ItemTemplate>
         </asp:TemplateField>
                <asp:BoundField DataField="EmployeeID" HeaderText="ID" />
                <asp:BoundField DataField="FirstName" HeaderText="FullName" />
                <asp:BoundField DataField="LastName" HeaderText="Category" />
      </Columns>
        <AlternatingRowStyle BackColor="Azure" />
    </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=myServer;Initial Catalog=Northwind;Integrated Security=True"
            ProviderName="System.Data.SqlClient" SelectCommand="SELECT EmployeeID,  FirstName, LastName FROM Employees">
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

Open in new window

2008-06-12-141627.jpg
0
 

Author Comment

by:zachvaldez
ID: 21773458
I use this methid to pupolate it. It may have something to do with it?
        If dr.HasRows Then
            Dim bf As New BoundField
            bf.HeaderText = "ID"
            bf.HeaderStyle.BackColor = Drawing.Color.PowderBlue
            bf.DataField = "MASTERID"
            bf.ItemStyle.Width = "80"
            GridView1.Columns.Add(bf)
            Dim bf2 As New BoundField
            bf2.HeaderText = "Name"
            bf2.HeaderStyle.BackColor = Drawing.Color.PowderBlue
            bf2.DataField = "FULLNAME"
            bf2.ItemStyle.Width = "200"
            GridView1.Columns.Add(bf2)
            Dim bf3 As New BoundField
            bf3.HeaderText = "Category"
            bf3.HeaderStyle.BackColor = Drawing.Color.PowderBlue
            bf3.DataField = "CATEGORY"
            bf3.ItemStyle.Width = "300"
            GridView1.Columns.Add(bf3)
            GridView1.DataSource = dr
            GridView1.DataBind()
        End If
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21773910
Just a quick guess (haven't tested it yet). What if you do:
If dr.HasRows Then
            GridView1.DataSource = dr
            GridView1.DataBind()
 End If

I am heading off home and will check back tomorrow if you still have trouble.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:zachvaldez
ID: 21774916
First, because of the datasource, there is conflict with this...
   <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=myServer;Initial Catalog=Northwind;Integrated Security=True"
            ProviderName="System.Data.SqlClient" SelectCommand="SELECT EmployeeID,  FirstName, LastName FROM Employees">
        </asp:SqlDataSource>

MY main concern is why can't i display the lunch itemtemplate in the grid...
thanks for your help.
0
 
LVL 22

Assisted Solution

by:prairiedog
prairiedog earned 400 total points
ID: 21779006
It does not conflict. The only difference between my example and yours is the way to retrieve data for the GridView. I changed my code to use SqlDataReader and got the same result.
Also, in your example, I don't understand why you have to manually add those fields again if you have those fields already defined in your GridView. Your code will append duplicated columns to the GridView. See the attached screen shot.
Since I still can't reproduce the problem you mentioned in your question, I am thinking the problem resides in a different location. Can you post a screen shot of your results? Do you do anything else after you bind the GridView that might cause the Lunch field be invisible?
Imports System.Configuration
Imports System.Data.SqlClient
 
Partial Public Class GridViewDemo
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strConn As String
        strConn = ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ToString()
 
        Dim conn As SqlConnection = New SqlConnection(strConn)
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.Connection = conn
        cmd.CommandText = "SELECT EmployeeID,  FirstName, LastName FROM Employees"
        cmd.CommandType = CommandType.Text
        Dim dr As SqlDataReader
        Try
            conn.Open()
            dr = cmd.ExecuteReader()
            If dr.HasRows Then
                Dim bf As New BoundField
                bf.HeaderText = "ID"
                bf.HeaderStyle.BackColor = Drawing.Color.PowderBlue
                bf.DataField = "EmployeeID"
                bf.ItemStyle.Width = "80"
                GridView1.Columns.Add(bf)
                Dim bf2 As New BoundField
                bf2.HeaderText = "First Name"
                bf2.HeaderStyle.BackColor = Drawing.Color.PowderBlue
                bf2.DataField = "FirstName"
                bf2.ItemStyle.Width = "200"
                GridView1.Columns.Add(bf2)
                Dim bf3 As New BoundField
                bf3.HeaderText = "Last Name"
                bf3.HeaderStyle.BackColor = Drawing.Color.PowderBlue
                bf3.DataField = "LastName"
                bf3.ItemStyle.Width = "300"
                GridView1.Columns.Add(bf3)
                GridView1.DataSource = dr
                GridView1.DataBind()
            End If
        Catch ex As Exception
 
        Finally
            conn.Close()
        End Try
    End Sub
End Class

Open in new window

2008-06-13-090756.jpg
0
 

Author Comment

by:zachvaldez
ID: 21783730
I don't understand why you have to manually add those fields again if you have those fields already defined in your GridView. Your code will append duplicated columns to the GridView. See the attached screen shot.>>>> YOU"RE ABSOLUTELY CORRECT!!
Also, there was this code that removes the columns and repopulate it. Between those two, I am losing the column for the "lunch"
I appreciate your help. Now I'll try to figure out how to insert the rows selected to  the " Event" table.
0

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 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