繁体   English   中英

C#Asp.Net使用DropDownList更改GridView的数据源

[英]C# Asp.Net changing DataSource for GridView using DropDownList

我创建一个webPage来显示本地数据库中的存储数据。 我想使用DropDownList项目来更改GridView中的列和顺序。 从DropdownList或更改查询字符串中进行选择会更容易更改DataGrid数据源。

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

例如:

DropdownList选择为(常规概述,产品组合,系统日志)

选择“概述”后:GridView将显示列([名称],[位置],[利润],[当前排名],按名称从Tabel [Main]升序排列)

选择投资组合后:

GridView将显示

栏目([名称],[当前排名],[利润],[已付价格],[购买数量],[买/卖]由利润从表[main]出发排序)

选择系统日志后:

gridview将显示

列([时间],[系统消息],[错误代码],[重新启动]从表[系统日志]开始按时间排序)

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

只要将属性AutoGenerateColumns="true" (默认设置)保留在GridView中,并且不设置任何<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();
    }
}

为了使用组合框来更改选定的dataTbale。 我的列表项设置为:“常规”,“利润”,“系统日志”。 我将datagrid插入表单,然后未设置dataSource,在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();
            }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM