如何在数据库上设置我的NumericUpDown

时间:2016-02-27 08:32:03

标签: c# numericupdown

例如,我桌上的My Item_Quantity为50, 然后我的NumericUpDown的最小值将是1和50? 我使用C#和MySQL,但我无法使其工作。

修改

这是我的代码:

string MyConnectionString = "Server=localhost;Port=3307;database=invpos;Uid=root;Pwd=''";

    public void LoadGrid()
    {
        MySqlConnection connection = new MySqlConnection(MyConnectionString);
        connection.Open();

        try
        {
            MySqlCommand cmd = connection.CreateCommand();
            cmd.CommandText = "Select * from items";
            MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adap.Fill(ds);
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            if (connection.State == ConnectionState.Open)
            {
                connection.Clone();
            }
        }
    }

    public void ComboBox()
    {
        MySqlConnection connection = new MySqlConnection(MyConnectionString);
        connection.Open();        
        MySqlCommand cmd = connection.CreateCommand();
        MySqlDataAdapter adap = new MySqlDataAdapter("Select Item_Name from items", connection);
        DataSet ds = new DataSet();
        adap.Fill(ds);
        comboBox1.DataSource = ds.Tables[0];
        comboBox1.DisplayMember = "Item_Name";
    }     
    private void Form4_Load(object sender, EventArgs e)
    {
        LoadGrid();
        ComboBox();

    }

我希望有人可以帮助我:(

1 个答案:

答案 0 :(得分:0)

  

这就是我想要发生的事情:示例:数据库:Item_Quantity = 50,我想将NumericUpDown的最大值设置为Item_Quantity的值:(

据我了解,您不知道如何获得NumericUpDown的最大值?

试试这个:

int itemQuantity = numericUpDown1.Maximum; //Get maximum value of `NumericUpDown`
int currentValue = numericUpdown1.Value; //Get current value of `NumericUpDown`

它能解决你的问题吗?

<强> *更新

try 
{
   MySqlCommand cmd = connection.CreateCommand();
   cmd.CommandText = "Update <your table> set <column> = " + itemQuantity;
   //Use Command Parameter will be better.
   cmd.executeNonQuery(); //execute update.
}
catch (Exception ex)
{
   //Catch exception
}