2017-09-14 136 views
0

我創建了一個web頁面來顯示來自本地數據庫的存儲數據。我想使用DropDownList Item來更改GridView中的列和順序。從DropdownList中選擇或更改查詢字符串時更改DataGrid數據源會更容易嗎?C#Asp.Net使用DropDownList更改GridView的DataSource

所有數據都從相同的DataSource中提取,但顯示不同的列和不同的順序。

例如:

DROPDOWNLIST Selctions是(一般概述,組合,系統日誌)

在選擇一般概述: GridView中顯示的列([姓名],[地址],[利潤] [當前排名]下令名稱在選擇投資組合asending從TABEL [主])

GridView控件將顯示

從表[主要]的Profit desending訂購的列([名稱],[當前排名],[收益],[付價],[購買數量],[買/賣])

選擇系統日誌:

gridview的將顯示

柱([時間],[系統信息],[錯誤代碼],[重新啓動]有序通過從表[系統日誌]時間asending)

任何幫助,將不勝感激!!我已經搜索了兩天的答案,現在沒有結果。想法歡迎!謝謝!

回答

0

爲了使用一個組合框來改變所選擇的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中的列移動到您希望顯示的位置。 –

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

謝謝!從我對你的代碼的理解來看:你創建了一個要顯示的列表。我正在顯示從我的數據源中提取的數據項。 –

+0

我相信,而不是嘗試自動設置datasouce,我可以調用查詢數據源與選定的命令字符串。我目前正在寫腳本: –

相關問題