C#Asp.Net使用DropDownList更改GridView的DataSource

问题描述:

我创建了一个web页面来显示来自本地数据库的存储数据。我想使用DropDownList Item来更改GridView中的列和顺序。从DropdownList中选择或更改查询字符串时更改DataGrid数据源会更容易吗?C#Asp.Net使用DropDownList更改GridView的DataSource

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

例如:

DROPDOWNLIST Selctions是(一般概述,组合,系统日志)

在选择一般概述: GridView中显示的列([姓名],[地址],[利润] [当前排名]下令名称在选择投资组合asending从TABEL [主])

GridView控件将显示

从表[主要]的Profit desending订购的列([名称],[当前排名],[收益],[付价],[购买数量],[买/卖])

选择系统日志:

gridview的将显示

柱([时间],[系统信息],[错误代码],[重新启动]有序通过从表[系统日志]时间asending)

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

为了使用一个组合框来改变所选择的dataTbale。将我的列表项目设置为:“常规”,“利润”,“系统日志”。我插入一个DataGrid到表单,然后不设置数据源,写了这个代码在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(); 
      } 

在从列表中选择一个项目,现在将显示在网格组不同的数据,而不试图调用不同的数据源。

+0

要更改gridview中显示的顺序,只需将sqlCmd.CommandText中的列移动到您希望显示的位置。 –

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

谢谢!从我对你的代码的理解来看:你创建了一个要显示的列表。我正在显示从我的数据源中提取的数据项。 –

+0

我相信,而不是尝试自动设置datasouce,我可以调用查询数据源与选定的命令字符串。我目前正在写脚本: –