Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, hello to all that help. I'm learning WPF and making small testing, I am changing the value of a cell at the moment when I am editing another cell of the same row. The change in the cell is made, but the change in the datagrid is not observed.

What am I omitting so that the value that I am modifying is updated?

Thanks for the help.
I know that is about something textbox cell.
Please, help me to identify my mistake. Thanks a lot,

What I have tried:

This is my code(reduced):
C#
public class clEmployee
{
   public int employeeID { get; set; }
   public string employeeNam { get; set; }
   public Boolean isChanged { get; set; }
}

XML
<datagrid
    x:name="dgTesting"
	margin="5,5,10,5"
	autogeneratecolumns="False" 
	horizontalalignment="Stretch"
	itemssource="{Binding myEmployees}"
	verticalalignment="Stretch"
	currentcellchanged="dgTesting_CurrentCellChanged"
	celleditending="dgTesting_CellEditEnding"
	previewkeydown="dgTesting_PreviewKeyDown"
	height="500"
	grid.column="0"
	grid.row="1">
          <datagrid.columns>
                <datagridtextcolumn binding="{Binding Path=employeeID, Mode=TwoWay}" header="ID">
                <datagridtextcolumn binding="{Binding Path=employeeName, Mode=TwoWay}" header="Names" width="700">
                <datagridtextcolumn binding="{Binding Path=isChanged, Mode=TwoWay}" header="Changes">

C#
public partial class wCatEmployees : Window
{
	ObservableCollection<clemployee> myEmployees= new ObservableCollection<clemployee>();

	public wCatEmployees()
	{
		InitializeComponent();
		loadEployees();
	}

	private void loadEployees()
	{
		myEmployees = a.LoadEmployees();
		dgTesting.ItemsSource = myEmployees;
	}

	private void dgTesting_PreviewKeyDown(object sender, KeyEventArgs e)
	{
		if (e.Key == Key.Enter || e.Key == Key.Return)
		{
			e.Handled = true;
			dgTesting.CommitEdit();
		}
	}
	
	private void dgTesting_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
	{
		if (e.EditAction != DataGridEditAction.Commit) { return; }
			if (e.Column.SortMemberPath.Equals("employeeName"))
		{
			
			int Fi = dgTesting.Items.IndexOf(dgTesting.CurrentItem);
			((clEmployee)dgTesting.Items[Fi]).isChanged = true; // <====Here, I don't see the change in the datagrid

			this.dgTesting.Items.Refresh(); // <=====  If I do that an exception ocurrs
		}
	}
}

I don't know what more else to do.
Posted
Updated 28-Dec-22 18:44pm
v2

1 solution

 
Share this answer
 
Comments
Member 15639943 29-Dec-22 10:07am    
I had already tried the code example shown in link number 2:
if (!e.Row.IsEditing)
{
this.dgTesting.Items.Refresh();
}

What I do not understand is that if I have finished editing the cell, the if condition (!e.Row.IsEditing)
is never true, so this.dgTesting.Items.Refresh(); it doesn't run either.

The example in link 1 is similar and I had already tried them too, but without success.
Thank you very much for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900