【问题标题】:C# Asp.Net changing DataSource for GridView using DropDownListC# Asp.Net 使用 DropDownList 更改 GridView 的 DataSource
【发布时间】:2018-02-23 16:56:02
【问题描述】:

我创建了一个网页来显示本地数据库中存储的数据。我想使用 DropDownList Item 来更改 GridView 中的列和顺序。从下拉列表中选择或更改查询字符串时更改 DataGrid 数据源会更容易吗?

所有数据都是从同一个数据源中提取的,但是显示不同的列和不同的顺序。

例如:

DropdownList Selctions 是(General Overview、portfolio、System Log)

选择一般概览后: GridView 将显示 Columns([name],[Location],[Profit],[Currentstandings] Ordered by Name asending From Tabel[Main])

在选择投资组合时:

GridView 会显示

Column([name],[Current Standings],[profit],[Price],[Quantity Bought],[Buy/Sell] 按从 Table[main] 降序的利润排序)

选择系统日志时:

gridview 会显示

Column([Time],[System Message],[Error Code], [Restart] 按时间排序,从表[System Log]开始)

任何帮助将不胜感激!我已经搜索了两天没有结果的答案。欢迎提出想法!!谢谢!

【问题讨论】:

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


    【解决方案1】:

    只要您在 GridView 中保留属性 AutoGenerateColumns="true"(这是默认值)并且不设置任何 <Columns>。 GridView 将自动生成适合您数据源的列。下面作为一个简单的例子

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
        <asp:ListItem>NameOnly</asp:ListItem>
        <asp:ListItem>WithPosition</asp:ListItem>
    </asp:DropDownList>
    <br />
    <asp:GridView runat="server" ID="GridView1">
    </asp:GridView>
    

    代码隐藏

    protected void Page_Load(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "WithPosition")
        {
            GridView1.DataSource = new List<dynamic>() {
                new { Name = "Andy", LastName="Wayne", Position = "PG"},
                new { Name = "Bill", LastName="Johnson", Position = "SD" },
                new { Name = "Caroline", LastName="Barry", Position = "Manager"}
            };
            GridView1.DataBind();
        }
        else if (DropDownList1.SelectedValue == "NameOnly")
        {
            GridView1.DataSource = new List<dynamic>() {
                new { Name = "Andy", LastName = "Wayne"},
                new { Name = "Bill", LastName = "Johnson"},
                new { Name = "Caroline", LastName = "Barry"}
            };
            GridView1.DataBind();
        }
    }
    

    【讨论】:

    • 谢谢!根据我对您的代码的理解:您创建了一个要显示的列表。我正在显示从我的数据源中提取的数据项。
    • 我相信我可以使用选定的命令字符串调用查询数据源,而不是尝试自动设置数据源。我目前正在编写脚本:
    【解决方案2】:

    为了使用组合框来更改选定的dataTbale。我的列表项设置为:“常规”、“利润”、“系统日志”。我在表单中插入了一个数据网格,然后没有设置数据源,在 c# 端编写了这段代码

    private void drop1_SelectedIndexChanged(object sender, EventArgs e)
            {
                String selI = drop1.SelectedItem.ToString();
                String strConnect = ("Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = C:\\Users\\smith\\Documents\\SqlStockDB.mdf; Integrated Security = True; Connect Timeout = 30");
                SqlConnection Connect = new SqlConnection(strConnect);
                SqlCommand sqlcmd = new SqlCommand();
                sqlcmd.Connection = Connect;
                sqlcmd.CommandType = CommandType.Text;
    
                if (selI == "General")
                {
                    sqlcmd.CommandText = "SELECT [Call Sign] AS Call_Sign, [Current Price] AS Current_Price FROM [Main] ORDER BY [Call Sign]";
    
                    SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
    
                    DataTable dtRecord = new DataTable();
                    adp.Fill(dtRecord);
                    dataGridView1.DataSource = dtRecord;
                    dataGridView1.Refresh();
                }
                dataGridView1.ClearSelection();
    
                if (selI == "Profit")
                {
                    sqlcmd.CommandText = "SELECT [Call Sign] AS Call_Sign, [Current Price] AS Current_Price, [Profit], [Buy], [Sell], [Available Money] AS Available_Money, [Quantity Bought] AS Quantity_Bought FROM [Main] ORDER BY [Profit] DESC, [Quantity Bought] DESC, [Call Sign]";
    
                    SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
    
                    DataTable dtRecord = new DataTable();
                    adp.Fill(dtRecord);
                    dataGridView1.DataSource = dtRecord;
                    dataGridView1.Refresh();
                }
    
                if (selI == "System Logs")
                {
                    sqlcmd.CommandText = "SELECT [Time], [Module], [Error Code] AS Error_Code, [Restart] FROM [Error] ORDER BY [Time], [Module], [Restart]";
    
                    SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
    
                    DataTable dtRecord = new DataTable();
                    adp.Fill(dtRecord);
                    dataGridView1.DataSource = dtRecord;
    
                    dataGridView1.Refresh();
                }
    

    从列表中选择一个项目后,它现在将在网格上显示不同的数据集,而无需尝试调用不同的数据源。

    【讨论】:

    • 要更改显示在gridview 中的顺序,只需将sqlCmd.CommandText 中的列移动到您想要显示的位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多