I have a project where I have to read a delimited file and show the data in the grid. The user can edit the data and press the save button which will take the data table and write it out to a delimited file. I cannot use a business object as the format of the input file is always different. I am able to parse my input file and build dynamically the grid columns and populate it with data. The issue that I have not been able to resolve is when the user edits a cell in the grid and presses enter, I see the new data in the CellEditEnd event, but do not see the data in the RowEditEnded. Also the grid never updates the screen with the new change, it is just showing the original data and the data table is also not being updated with the new value. I wrote this small Helloworld application to demonstrate the issue and what I am trying to do. Any help will be greatly appreciated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Data;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
namespace WpfPOC2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
LoadRowTable();
}
private void LoadRowTable()
{
DataTable dt = new DataTable();
RowDataGrid.KeyboardCommandProvider = new CustomKeyboardCommandProvider(RowDataGrid);
RowDataGrid.CanUserDeleteRows = false;
RowDataGrid.CanUserInsertRows = false;
RowDataGrid.CanUserReorderColumns = false;
RowDataGrid.SelectionMode = SelectionMode.Multiple;
GridViewSelectColumn selectBox = new GridViewSelectColumn() { UniqueName = "SelectBox", Header = "Select" };
RowDataGrid.Columns.Add(selectBox);
GridViewDataColumn colElement = new GridViewDataColumn() { DataMemberBinding = new Binding("Col1"), Header = "Col1", UniqueName = "Col1" };
RowDataGrid.Columns.Add(colElement);
DataColumn[] dc = new DataColumn[1];
dc[0] = new DataColumn();
dc[0].ColumnName = "Col1";
dt.Columns.Add(dc[0]);
dt.PrimaryKey = dc;
colElement = new GridViewDataColumn() { DataMemberBinding = new Binding("Col2"), Header = "Col2", UniqueName = "Col2" };
RowDataGrid.Columns.Add(colElement);
dt.Columns.Add("Col2", typeof(string));
colElement = new GridViewDataColumn() { DataMemberBinding = new Binding("Col3"), Header = "Col3", UniqueName = "Col3" };
RowDataGrid.Columns.Add(colElement);
dt.Columns.Add("Col3", typeof(string));
for (int i = 0; i < 20; i++)
{
object[] rowArray = new object[3];
DataRow row = dt.NewRow();
rowArray[0] = "ColA-" + i.ToString();
rowArray[1] = "ColB-" + i.ToString();
rowArray[2] = "ColC-" + i.ToString();
row.ItemArray = rowArray;
dt.Rows.Add(row);
}
RowDataGrid.ItemsSource = dt;
}
private void RowDataGrid_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
{
Console.Write(e);
}
private void RowDataGrid_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
{
if (e.EditOperationType == GridViewEditOperationType.Edit)
{
RowDataGrid.CommitEdit();
//Update the entry in the data base based on your logic.
}
}
}
class CustomKeyboardCommandProvider : DefaultKeyboardCommandProvider
{
private GridViewDataControl parentGrid;
private DefaultKeyboardCommandProvider defaultKeyboardProvider;
private CustomKeyboardCommandProvider customKeyboardProvider;
public CustomKeyboardCommandProvider(GridViewDataControl grid)
: base(grid)
{
this.parentGrid = grid;
}
public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
{
List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList();
if (key == Key.Enter)
{
commandsToExecute.Clear();
commandsToExecute.Remove(RadGridViewCommands.MoveDown);
commandsToExecute.Add(RadGridViewCommands.CommitEdit);
}
return commandsToExecute;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Data;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
namespace WpfPOC2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
LoadRowTable();
}
private void LoadRowTable()
{
DataTable dt = new DataTable();
RowDataGrid.KeyboardCommandProvider = new CustomKeyboardCommandProvider(RowDataGrid);
RowDataGrid.CanUserDeleteRows = false;
RowDataGrid.CanUserInsertRows = false;
RowDataGrid.CanUserReorderColumns = false;
RowDataGrid.SelectionMode = SelectionMode.Multiple;
GridViewSelectColumn selectBox = new GridViewSelectColumn() { UniqueName = "SelectBox", Header = "Select" };
RowDataGrid.Columns.Add(selectBox);
GridViewDataColumn colElement = new GridViewDataColumn() { DataMemberBinding = new Binding("Col1"), Header = "Col1", UniqueName = "Col1" };
RowDataGrid.Columns.Add(colElement);
DataColumn[] dc = new DataColumn[1];
dc[0] = new DataColumn();
dc[0].ColumnName = "Col1";
dt.Columns.Add(dc[0]);
dt.PrimaryKey = dc;
colElement = new GridViewDataColumn() { DataMemberBinding = new Binding("Col2"), Header = "Col2", UniqueName = "Col2" };
RowDataGrid.Columns.Add(colElement);
dt.Columns.Add("Col2", typeof(string));
colElement = new GridViewDataColumn() { DataMemberBinding = new Binding("Col3"), Header = "Col3", UniqueName = "Col3" };
RowDataGrid.Columns.Add(colElement);
dt.Columns.Add("Col3", typeof(string));
for (int i = 0; i < 20; i++)
{
object[] rowArray = new object[3];
DataRow row = dt.NewRow();
rowArray[0] = "ColA-" + i.ToString();
rowArray[1] = "ColB-" + i.ToString();
rowArray[2] = "ColC-" + i.ToString();
row.ItemArray = rowArray;
dt.Rows.Add(row);
}
RowDataGrid.ItemsSource = dt;
}
private void RowDataGrid_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
{
Console.Write(e);
}
private void RowDataGrid_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
{
if (e.EditOperationType == GridViewEditOperationType.Edit)
{
RowDataGrid.CommitEdit();
//Update the entry in the data base based on your logic.
}
}
}
class CustomKeyboardCommandProvider : DefaultKeyboardCommandProvider
{
private GridViewDataControl parentGrid;
private DefaultKeyboardCommandProvider defaultKeyboardProvider;
private CustomKeyboardCommandProvider customKeyboardProvider;
public CustomKeyboardCommandProvider(GridViewDataControl grid)
: base(grid)
{
this.parentGrid = grid;
}
public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
{
List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList();
if (key == Key.Enter)
{
commandsToExecute.Clear();
commandsToExecute.Remove(RadGridViewCommands.MoveDown);
commandsToExecute.Add(RadGridViewCommands.CommitEdit);
}
return commandsToExecute;
}
}
}