Binding InputPanel with DataTable
InputPanel supports data binding through data tables. You can create a data table using the standard DataTable class and bind it to the InputPanel control by setting the ItemsSource property.
Complete the following steps to bind InputPanel to data through data table.
- Set up the application
- Create a data table to bind with InputPanel
- Bind the data table to InputPanel
Set up the application
- Create a WPF application.
- Add the InputPanel control and name it InPanel.
Back to Top
Create a data table to bind with InputPanel
- Add a new class, Employee, to the application.
- Add the following import statement.
- Create an object, employee, of DataTable class.
'Create a data table
Private _employees As DataTable = Nothing
//Create a data table
private DataTable employees = null;
- Add data fields to be added in the table.
'Add data fields
Shared _rnd As New Random()
Shared _firstNames As String() = "Andy|Ben|Charlie|Dan|Ed|Fred|Gil".Split("|"c)
Shared _lastNames As String() = "Ambers|Bishop|Cole|Danson|Evers|Trask|Ulam".Split("|"c)
Shared _countries As String() = "China|India|United States|Japan|Myanmar".Split("|"c)
//Add data fields
static Random _rnd = new Random();
static string[] _firstNames = "Andy|Ben|Charlie|Dan|Ed|Fred|Gil".Split('|');
static string[] _lastNames = "Ambers|Bishop|Cole|Danson|Evers|Trask|Ulam".Split('|');
static string[] _countries = "China|India|United States|Japan|Myanmar".Split('|');
- Add class definition to assign fields to the data table.
'Initialize data table
Public ReadOnly Property Employees() As DataTable
Get
If _employees Is Nothing Then
_employees = New DataTable("Employees")
_employees.Columns.Add("ID", System.Type.[GetType]("System.String"))
_employees.Columns.Add("FirstName", System.Type.[GetType]("System.String"))
_employees.Columns.Add("LastName", System.Type.[GetType]("System.String"))
_employees.Columns.Add("Countries", System.Type.[GetType]("System.String"))
_employees.Columns.Add("BirthDate", System.Type.[GetType]("System.DateTime"))
For row As Integer = 0 To 9
Dim dRow As DataRow = _employees.NewRow()
dRow("ID") = _rnd.[Next](100000, 999999).ToString()
dRow("FirstName") = _firstNames(_rnd.[Next](_firstNames.Length))
dRow("LastName") = _lastNames(_rnd.[Next](_lastNames.Length))
dRow("Countries") = _countries(_rnd.[Next](_countries.Length))
dRow("BirthDate") =
DateTime.Today.AddDays(-_rnd.[Next](1, 365))
_employees.Rows.Add(dRow)
Next
End If
Return _employees
End Get
End Property
//Initialize data table
public DataTable Employees
{
get
{
if (employees == null)
{
employees = new DataTable("Employees");
employees.Columns.Add("ID",
System.Type.GetType("System.String"));
employees.Columns.Add("FirstName",
System.Type.GetType("System.String"));
employees.Columns.Add("LastName",
System.Type.GetType("System.String"));
employees.Columns.Add("Countries",
System.Type.GetType("System.String"));
employees.Columns.Add("BirthDate",
System.Type.GetType("System.DateTime"));
for (int row = 0; row < 10; row++)
{
DataRow dRow = employees.NewRow();
dRow["ID"] = _rnd.Next(100000, 999999).ToString();
dRow["FirstName"] = _firstNames[_rnd.Next(_firstNames.Length)];
dRow["LastName"] = _lastNames[_rnd.Next(_lastNames.Length)];
dRow["Countries"] = _countries[_rnd.Next(_countries.Length)]; ;
dRow["BirthDate"] = DateTime.Today.AddDays(-_rnd.Next(1, 365));
employees.Rows.Add(dRow);
}
}
return employees;
}
}
Back to Top
Bind the data table to InputPanel
- Set the ItemsSource property in XAML view to bind InputPanel with the data table.
XAML |
Copy Code
|
<c1:C1InputPanel Name="InPanel" ItemsSource="{Binding Employees}"/>
|
- Switch to the MainWindow.xaml.cs file and set the DataContext property.
'Set data context
Me.DataContext = New EmployeeDataContext()
//Set data context
this.DataContext = new Employee();
Back to Top