[英]Transform integer to numeric list in combobox from db - C#
我的SQL數據庫中有一個名為“購物”的表。 這個有一個叫做qtPisos的列,它描述了一個購物中心有多少層。 在我的程序中,我想基於此整數的數量創建一個組合框,以便用戶可以在購物中心中選擇級別,然后將列出該級別的商店。
知道我該怎么做嗎?
這是我的代碼。
public partial class Shoppings : Form
{
private DataViewManager dsView;
private DataSet ds;
public Shoppings()
{
InitializeComponent();
}
private void Shoppings_Load(object sender, EventArgs e)
{
Login logForm = new Login();
logForm = logForm.getLoginForm();
ds = new DataSet("DsShoppings");
// Fill the Dataset with Shoppings, map Default Tablename "Table" to "Shoppings".
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Shoppings", logForm.sqlConnection);
sda.TableMappings.Add("Table", "Shoppings");
sda.Fill(this.ds);
// The DataViewManager returned by the DefaultViewManager property
// allows to create custom settings for each DataTable in the DataSet.
this.dsView = ds.DefaultViewManager;
// Combobox Databinding
ComboShopping.DataSource = this.dsView;
ComboShopping.DisplayMember = "Shoppings.nomeShopping";
ComboShopping.ValueMember = "Shoppings.idShopping";
// Text Columns DataBinding
txtTotalLojas.DataBindings.Add("Text", dsView, "Shoppings.totalLojas");
//int qtPisos = Convert.ToInt32(ds.Tables["Shoppings"].Rows[0]["qtPisos"]);
ComboPisos.DataSource = this.dsView;
您可以檢索級別的值,然后使用此值作為最大值創建數字數組:
var levels = Enumerable.Range(1, levels)
然后,您可以將此值綁定到DropDownlist控件等。
control.Source = levels;
control.Bind()
若要顯示該級別的商店列表,可以使用DropDownList的自動回發功能,並根據控件的SelectedValue屬性檢索商店。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.