让Gridview根据点击的按钮显示不同的列

时间:2016-05-31 17:45:28

标签: c# sql asp.net gridview

我希望我的gridview根据用户点击的按钮显示特定列。现在使用我的下面的代码我收到错误消息“在所选数据源上找不到名称为'CaseNumber'的字段或属性。”是否有我可以更改的设置,以便我的select语句不必包含每一列?

这是MasterTable.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MasterTable.aspx.cs" Inherits="WebApplication1.Table" %>

<!DOCTYPE html>

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

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="mygv" runat="server" class="table table-inverse table-sm" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display." EnableSortingAndPagingCallbacks="True">
              <Columns>
                <asp:BoundField DataField="VendorID" HeaderText="Vendor ID" SortExpression="VendorID" />
                <asp:BoundField DataField="VendorName" HeaderText="Vendor Name" SortExpression="VendorName" />
                <asp:BoundField DataField="OrgID" HeaderText="Org ID" SortExpression="OrgID" />
                <asp:BoundField DataField="Organization" HeaderText="Organization" SortExpression="Organization" />
                <asp:BoundField DataField="ProductID" HeaderText="Product ID" SortExpression="ProductID" />
                <asp:BoundField DataField="ProductDescription" HeaderText="Product Description" SortExpression="ProductDescription" />
                <asp:BoundField DataField="EffectiveDate" HeaderText="Effective Date" SortExpression="EffectiveDate" DataFormatString="{0:d}" HtmlEncode=false />
                <asp:BoundField DataField="ProductYearStartDate" HeaderText="Product Year Start Date" SortExpression="ProductYearStartDate" DataFormatString="{0:d}" HtmlEncode=false  />
                <asp:BoundField DataField="OEOpen" HeaderText="OE Open" SortExpression="OEOpen" DataFormatString="{0:d}" HtmlEncode=false />
                <asp:BoundField DataField="OEClose" HeaderText="OEC lose" SortExpression="OEClose" DataFormatString="{0:d}" HtmlEncode=false />
                <asp:BoundField DataField="FileFeedAddDate" HeaderText="File Feed Add Date" SortExpression="FileFeedAddDate" DataFormatString="{0:d}" HtmlEncode=false />
                <asp:BoundField DataField="WhichFile" HeaderText="Which File" SortExpression="WhichFile" />
                <asp:BoundField DataField="ChangesAuditIndicator" HeaderText="Changes Audit Indicator" SortExpression="ChangesAuditIndicator" />
                <asp:BoundField DataField="MapsIndicator" HeaderText="Maps Indicator" SortExpression="MapsIndicator" />
                <asp:BoundField DataField="TestFileSentDate" HeaderText="Test File Sent Date" SortExpression="TestFileSentDate" DataFormatString="{0:d}" HtmlEncode=false  />
                <asp:BoundField DataField="ProdFileSentDate" HeaderText="Prod File Sent Date" SortExpression="ProdFileSentDate" DataFormatString="{0:d}" HtmlEncode=false  />
                <asp:BoundField DataField="GroupNumber" HeaderText="Group Number" SortExpression="GroupNumber" />
                <asp:BoundField DataField="CaseNumber" HeaderText="Case Number" SortExpression="CaseNumber" />
                <asp:BoundField DataField="BrokerID" HeaderText="Broker ID" SortExpression="BrokerID" />
                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
                <asp:BoundField DataField="AccType" HeaderText="Acc Type" SortExpression="AccType" />
                <asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" />
                <asp:BoundField DataField="TermedIndicator" HeaderText="Termed Indicator" SortExpression="TermedIndicator" />
            </Columns>

        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ETLDataConnectionString %>" />
    </div>
    </form>

这是MasterTable.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Table : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

           string vendor = "";
            string sql = null;

            vendor = Request.QueryString.Get("vendor");


            if (vendor == "elephant")
            {
                sql = @"select [VendorID] , [VendorName] , [OrgID] , [Organization] , [ProductID] , [ProductTypeID] , [ProductDescription] , [EffectiveDate] , [ProductYearStartDate] ,
                [OEOpen] , [OEClose] ,  [FileFeedAddDate] , [WhichFile] , [ChangesAuditIndicator] , [MapsIndicator] , [TestFileSentDate] , [ProdFileSentDate] , [GroupNumber] ,
                [Notes] , [TermedIndicator] from [CarrierDatabase] where vendorid = 1234";
            }
            if (vendor == "piglet")
            {
                sql = @"select [VendorID] , [VendorName] , [OrgID] , [Organization] , [ProductID] , [ProductTypeID] , [ProductDescription] , [EffectiveDate] , [ProductYearStartDate] ,
                [OEOpen] , [OEClose] ,[TestFileSentDate] , [ProdFileSentDate], [Notes] , [TermedIndicator] from [CarrierDatabase] where vendorid = 5678";
            }


            SqlDataSource1.SelectCommand = sql;

        }
    }
}

1 个答案:

答案 0 :(得分:1)

您还应该通过将Visible属性设置为false来隐藏您不想要显示的列:

   if (vendor == "elephant")
    {
        sql = @"select [VendorID] , [VendorName] , [OrgID] , [Organization] , [ProductID] , [ProductTypeID] , [ProductDescription] , [EffectiveDate] , [ProductYearStartDate] ,
        [OEOpen] , [OEClose] ,  [FileFeedAddDate] , [WhichFile] , [ChangesAuditIndicator] , [MapsIndicator] , [TestFileSentDate] , [ProdFileSentDate] , [GroupNumber] ,
        [Notes] , [TermedIndicator] from [CarrierDatabase] where vendorid = 1234";
        mygv.Columns[10].Visible = true;
        mygv.Columns[11].Visible = true;
        mygv.Columns[14].Visible = false;
        mygv.Columns[15].Visible = false;
        ...
    }
    if (vendor == "piglet")
    {
        sql = @"select [VendorID] , [VendorName] , [OrgID] , [Organization] , [ProductID] , [ProductTypeID] , [ProductDescription] , [EffectiveDate] , [ProductYearStartDate] ,
        [OEOpen] , [OEClose] ,[TestFileSentDate] , [ProdFileSentDate], [Notes] , [TermedIndicator] from [CarrierDatabase] where vendorid = 5678";
        mygv.Columns[10].Visible = false;
        mygv.Columns[11].Visible = false;
        mygv.Columns[14].Visible = true;
        mygv.Columns[15].Visible = true;
        ...
    }

在上面的示例中,我还将Visible属性设置为true,用于可能已被先前用户选择隐藏的列,但应该可以使用新选择显示。

这是另一种方法,使用布尔变量(以减少由于字符串中的拼写错误而导致错误的风险):

    bool isElephant = vendor == "elephant";
    bool isPiglet = vendor == "piglet";
    bool isOrange = vendor == "orange";
    ...

    if (isElephant)
    {
        ...
    }

    if (isPiglet)
    {
        ...
    }

    ...

    mygv.Columns[10].Visible = isElephant;
    mygv.Columns[11].Visible = isElephant;
    mygv.Columns[14].Visible = isPiglet;
    mygv.Columns[15].Visible = isPiglet;
    mygv.Columns[20].Visible = isElephant || isOrange;
    mygv.Columns[21].Visible = isPiglet || isOrange;
    ...

该方法还确保隐藏在一个案例中的列在另一个案例中可见。在我的第一个代码示例中,必须确保一个if块中的列也出现在另一个if块中(具有相反的值),这非常容易出错。