This is a migrated thread and some comments may be shown as answers.

Updating DataTable after an edit

3 Answers 514 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Scott Manning
Top achievements
Rank 1
Scott Manning asked on 26 Nov 2014, 09:47 PM
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;
}
}
}

3 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 27 Nov 2014, 01:05 PM
Hello,

You are setting RadGridView's ItemsSource to a DataTable. Would you please try setting it to DataTable.DefaultView instead? Does this help?

Regards,
Dimitrina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Scott Manning
Top achievements
Rank 1
answered on 27 Nov 2014, 06:00 PM
Thank you.  That worked perfectly for me.

On a different topic and a quick question,  can GridViewCheckBoxColumn have multiple items checked?  I added this column to the grid so the users can select which records to process.  I see that the GridViewSelectColumn column can work, but the GridViewSelectColumn does not seem to be bound to any data.  I was going to have a true/false value in the table to indicate if the user selected the record and I was trying to use GridViewCheckBoxColumn.  Currently, it appears that you can only have one item checked in the GridViewCheckBoxColumn column

0
Dimitrina
Telerik team
answered on 28 Nov 2014, 08:53 AM
Hello,

Using GridViewCheckBoxColumn allows you to select as many checkboxes on it as you wish. Have you bound it to a property from your business object? You can check the CheckBox Column documentation. If you are still not able to select multiple items on it, may I ask you open a new thread where to provide more details on your specific implementation?

Regards,
Dimitrina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
Scott Manning
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Scott Manning
Top achievements
Rank 1
Share this question
or