【问题标题】:C# ASP.NET Application ComboBox binded to GridView Display IssueC# ASP.NET 应用程序组合框绑定到 GridView 显示问题
【发布时间】:2018-02-22 14:31:02
【问题描述】:

我正在创建一个 ASP.NET Web 应用程序,它将显示来自 SQL 数据库的特定数据。我创建了 3 个数据源,每个特定于我想从 DropDownList 中选择的数据查看。

我的DropDownList Coding C#端如下:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string SourceName = DropDownList1.SelectedValue;
            GridView1.DataSourceID = SourceName;
            GridView1.DataBind();
        }

ASP.NET 端的DropDownList 编码:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Value="DataSource_Gerneral">General Overview</asp:ListItem>
            <asp:ListItem Value="DataSource_Portfolio">Portfolio</asp:ListItem>
            <asp:ListItem Value="DataSource_ErrorLog">Error Log</asp:ListItem>
        </asp:DropDownList>

ASP.NET端的Grid View编码如下:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderStyle="Inset" CellPadding="4" DataKeyNames="Call_Sign" DataSourceID= "DataSource_Gerneral" Font-Size="Smaller" ForeColor="#333333" GridLines="None" Height="16px" Width="155px" AllowPaging="True" AllowSorting="True" ClientIDMode="AutoID" HorizontalAlign="Justify" PageSize="100" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />



            <Columns>
                <asp:BoundField DataField="Call_Sign" HeaderText="Call_Sign" ReadOnly="True" SortExpression="Call_Sign" />
                <asp:BoundField DataField="Current_Price" HeaderText="Current_Price" SortExpression="Current_Price" />
                <asp:BoundField DataField="Stock_Market" HeaderText="Stock_Market" SortExpression="Stock_Market" />
            </Columns>



            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>

我已经测试了下拉列表以确保它实际上正在更改 DatasourceID。它工作正常。 However When Selected view is Selected into the dropdownlist, the gridview stays the same.我一直在寻找一种显示新数据的方法。我是不是错过了什么!我希望这只是一行简单的代码,会在运行时改变gridview。

【问题讨论】:

    标签: c# asp.net gridview drop-down-menu


    【解决方案1】:

    我忽略了一个巨大的游戏规则改变者。我没有改变正确的地方。因为所有信息都来自同一个数据库。我必须编写 3 个 if 语句来更改 command.CommandText 语句。在前。

    If (DataName == "DataSource_General)
    {
        command.CommandText = " ... Select...";
    }
    
    If (DataName == "DataSource_Portfolio")
    {
        command.CommandText = " ... Select ...";
    }
    

    通过这样做,我的数据正确显示。您也可以将 DropDownList ItemValue 配置为命令文本,然后通过以下方式调用它:

    String selectStatement = DropDownList.SelectedValue.ToString();
    command.CommandText = selectStatement;
    

    通过第二种方式,您可以获得更简洁的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多