The following example demonstrates how to programmatically add a new row to a GridControl.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace AddNewRow {
public class DemoViewModel : INotifyPropertyChanged {
BindingList<TestData> itemsSource;
public DemoViewModel() {
ItemsSource = GenerateData();
}
BindingList<TestData> GenerateData() {
BindingList<TestData> list = new BindingList<TestData>();
for(int i = 0; i < 5; i++) {
list.Add(CreateTestData(Guid.NewGuid().ToString(), "text2 " + i, "text3 " + i));
}
return list;
}
TestData CreateTestData(string text1, string text2, string text3) {
return new TestData() {
Text1 = text1,
Text2 = text2,
Text3 = text3
};
}
public BindingList<TestData> ItemsSource {
get { return itemsSource; }
set {
if(value == itemsSource)
return;
itemsSource = value;
NotifyPropertyChanged("ItemsSource");
}
}
public void AddNewRow(string text1, string text2, string text3) {
ItemsSource.Add(CreateTestData(text1, text2, text3));
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info) {
if(PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
}
#region Test data
public class TestData {
public string Text1 { get; set; }
public string Text2 { get; set; }
public string Text3 { get; set; }
}
#endregion
}
using System;
using System.ComponentModel;
using System.Windows;
namespace AddNewRow {
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
}
public DemoViewModel ViewModel { get { return (DemoViewModel)DataContext; } }
private void addNewRowButton_Click(object sender, RoutedEventArgs e) {
CreateNewRowWindow newRowWindow = new CreateNewRowWindow();
newRowWindow.ShowDialog();
if (newRowWindow.DialogResult.Value == true) {
ViewModel.AddNewRow(newRowWindow.text1.Text, newRowWindow.text2.Text, newRowWindow.text3.Text);
view.MoveLastRow();
grid.Focus();
}
}
}
}
<Window x:Class="AddNewRow.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:local="clr-namespace:AddNewRow" Title="Window1" Height="400" Width="500" >
<Window.DataContext>
<local:DemoViewModel />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Name="addNewRowButton" Content="Add new row..." Focusable="False" Click="addNewRowButton_Click" />
<dxg:GridControl x:Name="grid" ItemsSource="{Binding ItemsSource}" AutoGenerateColumns="AddNew" Grid.Row="1">
<dxg:GridControl.View>
<dxg:TableView x:Name="view" AutoWidth="True" />
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</Window>
using System.Windows;
namespace AddNewRow {
public partial class CreateNewRowWindow : Window {
public CreateNewRowWindow() {
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e) {
DialogResult = true;
Close();
}
}
}
<Window x:Class="AddNewRow.CreateNewRowWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="CreateNewRowWindow" Height="180" Width="300">
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="0,1,0,1"/>
</Style>
</Window.Resources>
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="Text1: " />
<TextBox Name="text1" Grid.Column="1"/>
<TextBlock Text="Text2: " Grid.Row="1"/>
<TextBox Name="text2" Grid.Row="1" Grid.Column="1"/>
<TextBlock Text="Text3: " Grid.Row="2"/>
<TextBox Name="text3" Grid.Row="2" Grid.Column="1"/>
<StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Right" Orientation="Horizontal" Grid.Row="3" Grid.ColumnSpan="2">
<Button IsDefault="True" Content="OK" Click="Button_Click" Width="55"/>
<Button IsCancel="True" Content="Cancel" Width="73" Margin="5,0,0,0"/>
</StackPanel>
</Grid>
</Window>
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.ComponentModel
Namespace AddNewRow
Public Class DemoViewModel
Implements INotifyPropertyChanged
Private itemsSource_Renamed As BindingList(Of TestData)
Public Sub New()
ItemsSource = GenerateData()
End Sub
Private Function GenerateData() As BindingList(Of TestData)
Dim list As New BindingList(Of TestData)()
For i As Integer = 0 To 4
list.Add(CreateTestData(Guid.NewGuid().ToString(), "text2 " & i, "text3 " & i))
Next i
Return list
End Function
Private Function CreateTestData(ByVal text1 As String, ByVal text2 As String, ByVal text3 As String) As TestData
Return New TestData() With {.Text1 = text1, .Text2 = text2, .Text3 = text3}
End Function
Public Property ItemsSource() As BindingList(Of TestData)
Get
Return itemsSource_Renamed
End Get
Set(ByVal value As BindingList(Of TestData))
If value Is itemsSource_Renamed Then
Return
End If
itemsSource_Renamed = value
NotifyPropertyChanged("ItemsSource")
End Set
End Property
Public Sub AddNewRow(ByVal text1 As String, ByVal text2 As String, ByVal text3 As String)
ItemsSource.Add(CreateTestData(text1, text2, text3))
End Sub
#Region "INotifyPropertyChanged"
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
#End Region
End Class
#Region "Test data"
Public Class TestData
Private privateText1 As String
Public Property Text1() As String
Get
Return privateText1
End Get
Set(ByVal value As String)
privateText1 = value
End Set
End Property
Private privateText2 As String
Public Property Text2() As String
Get
Return privateText2
End Get
Set(ByVal value As String)
privateText2 = value
End Set
End Property
Private privateText3 As String
Public Property Text3() As String
Get
Return privateText3
End Get
Set(ByVal value As String)
privateText3 = value
End Set
End Property
End Class
#End Region
End Namespace
Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Windows
Namespace AddNewRow
Partial Public Class Window1
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Public ReadOnly Property ViewModel() As DemoViewModel
Get
Return CType(DataContext, DemoViewModel)
End Get
End Property
Private Sub addNewRowButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim newRowWindow As New CreateNewRowWindow()
newRowWindow.ShowDialog()
If newRowWindow.DialogResult.Value = True Then
ViewModel.AddNewRow(newRowWindow.text1.Text, newRowWindow.text2.Text, newRowWindow.text3.Text)
view.MoveLastRow()
grid.Focus()
End If
End Sub
End Class
End Namespace
Imports Microsoft.VisualBasic
Imports System.Windows
Namespace AddNewRow
Partial Public Class CreateNewRowWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
DialogResult = True
Close()
End Sub
End Class
End Namespace