C#Asp.Net使用DropDownList

时间:2017-09-14 15:58:14

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

我创建一个webPage来显示来自本地DataBase的存储数据。我想使用DropDownList Item来更改GridView中的列和顺序。从DropdownList或Changing Query字符串中选择更改DataGrid数据源会更容易吗?

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

例如:

DropdownList选项是(概述,投资组合,系统日志)

选择概述后

:  GridView将显示列([名称],[位置],[利润],[当前排名]按名称排序,从表格[Main]开始)

选择投资组合时:

GridView将显示

列[[名称],[当前排名],[利润],[价格支付],[购买数量],[买/卖]按表[主要]下降利润排序)

选择系统日志时:

gridview将显示

列([时间],[系统消息],[错误代码],[重启]按表格[系统日志]的时间顺序排序)

任何帮助都会受到极大的赞赏!我已经搜索了两天的答案而没有结果。想法欢迎!!谢谢!

2 个答案:

答案 0 :(得分:0)

只要在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();
    }
}

答案 1 :(得分:0)

为了使用组合框更改所选的dataTbale。将我的列表项设置为:“常规”,“利润”,“系统日志”。我在表单中插入了一个datagrid,然后没有设置dataSource,在c#side上编写了这段代码

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();
            }

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