【问题标题】:Fetch ID from database in C# - Get ID of hovered panel在 C# 中从数据库中获取 ID - 获取悬停面板的 ID
【发布时间】:2021-03-09 03:52:05
【问题描述】:

所以我正在 Winforms 中开发一个小型银行应用程序,目前正在创建一个可以创建自己的储蓄的部分。我想添加一点额外的功能,即您应该能够看到达到储蓄目标的百分比,它有效,但它只是在所有不同的储蓄上显示相同的数字,这是不应该的。我想如果我知道 ID,我就可以获取特定的储蓄百分比。

代码

private void panel_MouseEnter(object sender, EventArgs e)
    {

        decimal savingMoney;
        decimal money;

        decimal percentage;

        string sql2 = "SELECT * FROM secret.secret where user =  '" + username.Text + "'";
        MySqlDataAdapter ads12 = new MySqlDataAdapter(sql2, conn);
        DataTable td2 = new System.Data.DataTable();
        ads12.Fill(td2);

            for (int i = 0; i < td2.Rows.Count; i++)
                {
            string sql3 = "SELECT * FROM secret.secretwhere user =  '" + username.Text + "'";
            MySqlDataAdapter sda12 = new MySqlDataAdapter(sql3, conn);
            DataTable td = new System.Data.DataTable();
            sda12.Fill(td);

            savingMoney = Convert.ToDecimal(td.Rows[i][4].ToString());
            money = Convert.ToDecimal(td.Rows[i][3].ToString());

            percentage = savingMoney / money * 100;


            bunifuTransition1.ShowSync(bunifuCircleProgressbar1);
            bunifuCircleProgressbar1.Value = Convert.ToInt32(percentage);
             }
        

【问题讨论】:

    标签: c# mysql winforms


    【解决方案1】:

    据我所知,您只需运行迭代并设置每次迭代的百分比,这意味着只有最后一次迭代可见:

                bunifuCircleProgressbar1.Value = Convert.ToInt32(percentage);
    

    通常,您可以使用以下方法读取对象的 Name 属性:

    private void panel_MouseEnter(object sender, EventArgs e)
    {
       Control control = sender as Control;
       
       if (control.Name.Equals("PanelNameYouWant"))
        {
          // enter code here
        }
    }
    

    如果您的数据库集具有面板名称,这是一个快速且非常肮脏的解决方案:

    string dataSetName = td.Rows[i][5].ToString(); //期望保存的名称在第5列,与面板名称匹配

    然后是一个简单的 if 条件:

     if (control.Name.Equals(dataSetName))
        {
          // enter code here to update percentage for the progress bar
        }
    

    【讨论】:

    • 嘿@Moe 这是真的,我只得到最后一次迭代。知道如何获得我悬停的面板的迭代吗?
    • 我实际上在想,当您的数据集发生变化时会发生什么?我在这里看到的问题是没有实际的耦合,一旦数据集消失,进度条将采用错误的数据集。要识别面板,您可能使用 Name 属性或 Tag。
    • 嘿,我尝试过使用 name 属性,但它似乎不起作用。它,只需给我数据库中的姓氏:/
    • @matteo0402 请检查编辑 - 这应该可以解决名称问题,并且可能有助于获得面板的特定保存:)
    猜你喜欢
    • 2022-11-21
    • 2021-03-24
    • 2021-11-09
    • 2017-09-12
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    相关资源
    最近更新 更多