将组合框中的整数从 db 转换为数字列表 - C#

发布于 2024-11-07 13:20:39 字数 1465 浏览 0 评论 0原文

我的 SQL 数据库中有一个名为“Shoppings”的表。这个有一个名为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;

I have a table called "Shoppings" in my SQL db. This one has a column called qtPisos, which describes how many levels a shopping mall has. In my program I would like to create a combobox based on the quantity of this integer, so the user can select the level in the shopping mall, which will then list the shops for that particular level.

Any idea how can I do that?

Here is my code.

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠魁 2024-11-14 13:20:39

您可以检索级别的值,然后使用该值作为最大值创建一个数值数组:

var levels = Enumerable.Range(1, levels)

然后可以将该值绑定到 DropDownlist 控件等。

control.Source = levels;
control.Bind()

然后,要显示该级别的商店列表,您可以使用 DropDownList 的自动回发功能并根据控件的 SelectedValue 属性检索商店。

You can retrieve the value of the levels and then create a numeric array using this value as the maximum value:

var levels = Enumerable.Range(1, levels)

You can then bind this value to a DropDownlist control or such.

control.Source = levels;
control.Bind()

To then display a list of shops for that level, you could use the DropDownList's autopostback feature and retrieve your shops based upon the SelectedValue property of the control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文