2016-05-31 74 views
0

我希望我的gridview根據用戶點擊哪個按鈕來顯示特定的列。現在用我的下面的代碼,我得到了錯誤消息「名稱'CaseNumber'字段或屬性未找到選定的數據源上。」是否有可以更改的設置,以便我的select語句不必包含每列?GridView根據點擊按鈕顯示不同的列

這裏是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

你也應該隱藏你不想列通過將他們的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塊中(具有相反的值),這非常容易出錯。

+0

我愛你。同樣重要的是要注意第一列是[0]。這使我絆倒了一分鐘。 – Emmily

+0

你能解釋一下你的第二種方法嗎?你不是說可見的真或假? – Emmily

+0

「vendor ==」elephant「'表達式是一個返回布爾值(true或false)的比較。然後將該值分配給「可見」屬性。 – ConnorsFan

相關問題