• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 659
  • Last Modified:

itemTemplate within a Gridview not displaying in run mode

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
zachvaldez
Asked:
zachvaldez
  • 3
  • 3
2 Solutions
 
prairiedogCommented:
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
 
zachvaldezAuthor Commented:
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
 
prairiedogCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
zachvaldezAuthor Commented:
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
 
prairiedogCommented:
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
 
zachvaldezAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now