Search code examples
c#wpfwpfdatagrid

WPF datagridview - geting outdated value when not pressing enter


i have a datagridview named mainEvents_sql with the following columns :

<DataGrid.Columns >
    <DataGridTextColumn Header="Event ID"  Binding="{Binding id}" IsReadOnly="True" />
    <DataGridTextColumn Header="event name" Binding="{Binding name}" Width="*" />
    <DataGridTemplateColumn>
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Content="Update" x:Name="btnUpdate"
                        Click="btnUpdate_Click" CommandParameter="{Binding name}" >
                </Button>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
 </<DataGrid.Columns >

i am trying to do an update button, i get the currect cell but it is the original and not with the new value, this is my btnUpdate event:

private void btnUpdate_Click(object sender, EventArgs e)
{
    DataRowView selectedFile = ((FrameworkElement)sender).DataContext as DataRowView;
    if (mainEvents_sql.SelectedItems.Count > 0){
         String str = Convert.ToString(selectedFile.Row.ItemArray[1]);
         // update the DB with ADO .net
    }
}

str is the original value, how do i get the new value ?

EDIT: I get the current value if the user press enter but not when he exit the cell by clicking on another item , how can i fix it ?


Solution

  • add (my datagrid).CommitEdit(); before i make changes ,another advantage is this way is update my source object only on save event so it supposed to be faster.