Visual Studio 2012 - Gridview不在webform上显示

时间:2014-05-16 15:29:00

标签: asp.net visual-studio-2012

我想让用户只能选择他想要在gridview上看到的数据而不是表格的每一列。例如,我将每列显示在一个复选框列表中。如果用户只选择名称和地址并点击提交,则在回发时,名称和地址应显示在gridview中。我没有正常工作,除了gridview没有显示任何数据。

这是我的.aspx代码:

     <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AdminReport.aspx.cs" Inherits="WorkOrder.AdminReport" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

     <link href="Style.css" rel="stylesheet" type="text/css" />
    <link href="msgBoxLight.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/jquery-1.8.0.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.msgBox.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel1" runat="server" Width="1020px">
            <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/mast_left.png" />          
        </asp:Panel>       
        </div>

        <div class="Section"> <hr /><h2>
            Online Work Order Form - (Admin Report)</h2>
        </div>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Select data you want to view and click the submit button:" CssClass="label"></asp:Label>
        <br />
        <br />
        <br />
        <br />       
        <asp:CheckBoxList ID="cbSelectData" runat="server" AutoPostBack="False"   AppendDataBoundItems="True" BorderStyle="Ridge" CellPadding="15" CellSpacing="2" RepeatColumns="6" BackColor="#FFD7AE">
        </asp:CheckBoxList>
            <br /><br />

                <asp:Button ID="btnGetResults" runat="server" Text="Submit" OnClick="btnGetResults_Click"/>

              <br />
         <asp:Label ID="Message" runat="server" Text=""></asp:Label>
        <br />  <br />       

        <asp:Label ID="Label1" runat="server" Text="Select to filter by position:"></asp:Label>
        <br /> <br />  <br />
                <asp:GridView ID="GridViewReport" runat="server" AutoGenerateColumns="False"   DataKeyNames="Names" EnableModelValidation ="True" EmptyDataText="No records to display">
            <EditRowStyle BackColor="#0099CC" ForeColor="Black" />
            <HeaderStyle BackColor="Black" ForeColor="White" />
            <RowStyle BackColor="#FFFFCC" />
            <SelectedRowStyle BorderColor="#0099FF" />
        </asp:GridView>
        <br /><br /><br /><br />
    </form>
</body>
</html>

这是我背后的代码:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net.Mail;
using aejw.Network;

namespace WorkOrder
{
    public partial class AdminReport : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack) // this keeps from loading everytime the page does a partial laod
            {
                LoadDataSource1();
            }

           this.MaintainScrollPositionOnPostBack = true;

        }

        protected void btnGetResults_Click(object sender, EventArgs e)
        {

            LoadDataSource2();         

        }

        protected void LoadDataSource1()
        {
            int i = 0;
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WConnectionString"].ConnectionString);
            string query1 = "SELECT Name FROM sys.columns WHERE object_id = OBJECT_ID (@ObjectID)";
            SqlCommand cmd = new SqlCommand(query1, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("ObjectID", "dbo.WO_Submission_Details");
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                cbSelectData.Items.Insert(i, rdr.GetValue(0).ToString());
                i++;
            }
            conn.Close();
        }

        protected void LoadDataSource2()
        {

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WOConnectionString"].ConnectionString);
            string query2 = "SELECT Name FROM sys.columns WHERE object_id = OBJECT_ID (@ObjectID)";
            SqlCommand cmd2 = new SqlCommand(query2, conn);
            conn.Open();
            {
                cmd2.Parameters.AddWithValue("ObjectID", cbSelectData.SelectedItem.ToString());
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd2);
                da.Fill(ds);
                GridViewReport.DataSource = ds;
                //cbSelectData.DataTextField = "Name";
                //cbSelectData.DataValueField = "Name";
                //cbSelectData.DataBind();
                GridViewReport.DataBind();

                conn.Close();
            }


        }

    }
}    

我有gridview绑定到数据源。我错过了一步吗?感谢

1 个答案:

答案 0 :(得分:0)

您有AutoGenerateColumns="False",这意味着您想通过提供template-fieldsbound-fields来指定列。但是你没有向aspx中的GridView添加任何列。

所以改成它:

<asp:GridView ID="GridViewReport" runat="server" AutoGenerateColumns="True" ... 

以下是声明性添加列的示例:

....
<Columns>
    <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" 
        InsertVisible="False" ReadOnly="True" SortExpression="CustomerID" />
    <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" 
        SortExpression="CompanyName" />
    <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
        SortExpression="FirstName" />
    <asp:BoundField DataField="LastName" HeaderText="LastName" 
        SortExpression="LastName" />
</Columns>
....