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;
}
}
}
我愛你。同樣重要的是要注意第一列是[0]。這使我絆倒了一分鐘。 – Emmily
你能解釋一下你的第二種方法嗎?你不是說可見的真或假? – Emmily
「vendor ==」elephant「'表達式是一個返回布爾值(true或false)的比較。然後將該值分配給「可見」屬性。 – ConnorsFan