BindingSource.Position Property

Gets or sets the index of the current item in the underlying list.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[BrowsableAttribute(false)]
public int Position { get; set; }

Property Value

Type: System.Int32
A zero-based index that specifies the position of the current item in the underlying list.

When setting the Position property, out-of-range values are treated in the following manner:

  • Negative values are treated as 0.

  • Values greater than or equal to Count are treated as Count minus 1.

Changing the Position property will adjust the Current property likewise.

The following code example demonstrates the MoveNext, MoveFirst, Current, and Position members. To run this example, paste the code into a form that imports the System.Drawing.Drawing2D namespace and contains a BindingSource named BindingSource1 and a button named button1. Associate the Form1_Load and Form1_Paint methods with the Load and Paint events for the form, and associate the button1_click method with the Click event for button1. Visual Basic users will need to add a reference to System.Data.dll.

		void Form1_Load(object sender, EventArgs e)
		{
			// Set the data source to the Brush type and populate 
			// BindingSource1 with some brushes.
			BindingSource1.DataSource = typeof(System.Drawing.Brush);
			BindingSource1.Add(
				new TextureBrush(new Bitmap(typeof(Button), "Button.bmp")));
			BindingSource1.Add(new HatchBrush(HatchStyle.Cross, Color.Red));
			BindingSource1.Add(new SolidBrush(Color.Blue));
		}


		private void button1_Click(object sender, EventArgs e)
		{
			// If you are not at the end of the list, move to the next item 
			// in the BindingSource. 
			if (BindingSource1.Position + 1 < BindingSource1.Count)
				BindingSource1.MoveNext();

			// Otherwise, move back to the first item. 
			else
				BindingSource1.MoveFirst();

			// Force the form to repaint. 
			this.Invalidate();
		}

		void Form1_Paint(object sender, PaintEventArgs e)
		{
			// Get the current item in the BindingSource.
			Brush item = (Brush)BindingSource1.Current;

			// If the current type is a TextureBrush, fill an ellipse. 
			if (item.GetType() == typeof(TextureBrush))
				e.Graphics.FillEllipse(item,
				   e.ClipRectangle);

			// If the current type is a HatchBrush, fill a triangle. 
			else if (item.GetType() == typeof(HatchBrush))
				e.Graphics.FillPolygon(item,
					new Point[] { new Point(0, 0), new Point(0, 200),
                    new Point(200, 0)});

			// Otherwise, fill a rectangle. 
			else
				e.Graphics.FillRectangle(
					(Brush)BindingSource1.Current, e.ClipRectangle);
		}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft