C# .NET - numericupdown value from database - Asked By Trisha on 10-Feb-12 03:26 AM

i want to take numeric up down control value from id column of access database. How to write code for that ????
kalpana aparnathi replied to Trisha on 10-Feb-12 03:36 AM
hi,

Use below code for your solution

RowCount = DataTable row Count

for(int i=0 ; i < RowCount ; i++)
{
     NumericUpDown nupDynCtrl = new NumericUpDown ();

     // This makes your Id unique

     nupDynCtrl.ID = "NumericUpDown" + "i";
    
 }

When you want to retrieve values from Control u can use Controls and type Cast it to NumericUpdown


for(int i=0 ;i < RowCount ; i++)

{
       NumericUpDown nupDynCtrlNew  = (NumericUpdown) this.Controls["NumericUpDown" + "i"]
}

Hope it helps!!!

Thanks,
Web Star replied to Trisha on 10-Feb-12 03:39 AM
See here is complete sample with source code how can use numeric up downs in window form in c#.net
http://www.codeproject.com/Articles/30899/Extended-NumericUpDown-Control 
Web Star replied to Trisha on 10-Feb-12 03:40 AM
Also this is another good sample for that
http://www.dotnetperls.com/numericupdown 
Somesh Yadav replied to Trisha on 10-Feb-12 03:49 AM
Hi Trisha,

Programs sometimes need a control that allows the user to select a number by clicking on an up or down arrow. The Windows Forms platform offers the NumericUpDown control, which features a textbox with a number and also two scroll buttons to change the value. This article describes and uses the NumericUpDown control.

NumericUpDown

Get started

To get started, please add a NumericUpDown control to your Windows Forms program in the Visual Studio designer by double-clicking on the entry in the Toolbox. Next, you can change some properties of the NumericUpDown by right-clicking on it and selecting Properties; these options are described in the latter sections. In this code example, we added the ValueChanged event handler by double-clicking on the NumericUpDown control.


so here is the code For numeric dropdown and value changed.

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
	public Form1()
	{
	    InitializeComponent();
	}

	private void numericUpDown1_ValueChanged(object sender, EventArgs e)
	{
	    // You can change other program parts when the value changes.
	    this.Text = "Value changed to " + numericUpDown1.Value.ToString();
	}
    }
}

For more refer to the below link:-

http://www.dotnetperls.com/numericupdown

Hope this will help you.
dipa ahuja replied to Trisha on 10-Feb-12 04:03 AM
public ComboBind()
    {
          
      string conn = "Connectiostring";
 
      OleDbDataAdapter da = new OleDbDataAdapter("Select * from emp", conn);
      DataSet ds = new DataSet();
      da.Fill(ds);
      comboBox1.DataSource = ds.Tables[0];
      comboBox1.DisplayMember = "empid";
      comboBox1.ValueMember = "empid";
 
 
    }
 

Sreekumar P replied to Trisha on 10-Feb-12 05:40 AM
Hi,

I hope u can get a DatTable from Access database.

den u can use this code (self tested).

DataTable dtNumberUpDownValues = null;
    private void Form1_Load(object sender, EventArgs e)
    {
      BindDropDown();
    }
    bool isFIrst = false;
    private void BindDropDown()
    {
      isFIrst = true;
      //Some how get the Data from DB - it should be ordered (sorted).
      dtNumberUpDownValues = getValues();
      if (dtNumberUpDownValues.Rows.Count > 0)
      {
        //zero th row contains the smallest value
        numericUpDown1.Minimum = Convert.ToDecimal(dtNumberUpDownValues.Rows[0]["NumberValue"]);
        //last th row contains the max value
        numericUpDown1.Maximum = Convert.ToDecimal(dtNumberUpDownValues.Rows[dtNumberUpDownValues.Rows.Count - 1]["NumberValue"]);
 
      }
      oldValue = numericUpDown1.Value;
    }
 
    private DataTable getValues()
    {
      DataTable dt = new DataTable();
      dt.Columns.Add(new DataColumn("NumberValue", typeof(decimal)));
      DataRow dr = dt.NewRow();
      dr["NumberValue"] = 10;
      dt.Rows.Add(dr);
 
      dr = dt.NewRow();
      dr["NumberValue"] = 22;
      dt.Rows.Add(dr);
 
      dr = dt.NewRow();
      dr["NumberValue"] = 33;
      dt.Rows.Add(dr);
 
      dr = dt.NewRow();
      dr["NumberValue"] = 47;
      dt.Rows.Add(dr);
 
      dr = dt.NewRow();
      dr["NumberValue"] = 660;
      dt.Rows.Add(dr);
 
      dr = dt.NewRow();
      dr["NumberValue"] = 1100;
      dt.Rows.Add(dr);
      return dt;
    }
    decimal oldValue = 0;
    bool isOnceChanged = false;
    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
      if (isFIrst)
      {
        isFIrst = false;
        return;
      }
      if (isOnceChanged)
        return;
      if (dtNumberUpDownValues != null)
      {
        if (oldValue < numericUpDown1.Value)
        {
          DataRow[] drs = dtNumberUpDownValues.Select("NumberValue > " + numericUpDown1.Value);
          if (drs.Length > 0)
          {
            isOnceChanged = true;
            numericUpDown1.Value = Convert.ToDecimal(drs[0]["NumberValue"]);
          }
        }
        else
        {
          DataRow[] drs = dtNumberUpDownValues.Select("NumberValue < " + numericUpDown1.Value);
          if (drs.Length > 0)
          {
            isOnceChanged = true;
            numericUpDown1.Value = Convert.ToDecimal(drs[drs.Length - 1]["NumberValue"]);
          }
        }
      }
      oldValue = numericUpDown1.Value;
    }
 
    private void numericUpDown1_Click(object sender, EventArgs e)
    {
      isOnceChanged = false;
    }