Back to Devexpress

How to: Bind a Gauge Control to a Data Source at Runtime

windowsforms-18271-controls-and-libraries-gauges-examples-how-to-bind-a-gauge-control-to-a-data-source-at-runtime.md

latest1.8 KB
Original Source

How to: Bind a Gauge Control to a Data Source at Runtime

  • Nov 13, 2018

The following example shows how to bind a digital gauge’s DigitalGauge.Text property to a field in the NWind database using the traditional .NET binding mechanism.

In the example, the Text property is bound to a UnitPrice field in the Products table.

To learn about the data-binding mechanism, see MSDN.

csharp
using System.Data.OleDb;
using System.Windows.Forms;

// Create a connection object.
OleDbConnection connection = new OleDbConnection(
  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\DATA\\NWIND.MDB");

// Create a data adapter.
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Products", connection);

// Create a BindingSource and populate it with data.
BindingSource bs = new BindingSource(new DataTable(), "");
adapter.Fill(bs.DataSource as DataTable);

// Bind the Text property to the UnitPrice field in the Products table.
digitalGauge1.DataBindings.Add("Text", bs, "UnitPrice");
vb
Imports System.Data.OleDb
Imports System.Windows.Forms

' Create a connection object.
Private connection As OleDbConnection = _
    New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\DATA\NWIND.MDB")

' Create a data adapter.
Private adapter As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Products", connection)

' Create a BindingSource and populate it with data.
Private bs As BindingSource = New BindingSource(New DataTable(), "")
adapter.Fill(TryCast(bs.DataSource, DataTable))

' Bind the Text property to the UnitPrice field in the Products table.
digitalGauge1.DataBindings.Add("Text", bs, "UnitPrice")