Elad770
Member
- Joined
- Oct 4, 2021
- Messages
- 20
- Programming Experience
- 1-3
hello
The datagrid does binidng with a DataTable that holds within it a db table with required fields. The problem is that when I try to get out of focus in the new row from any cell from the same row without really filling the same cell, the datagrid does not allow me to access other rows that are already filled with content and requires me to fill the last row.
I use with cellEditing event because I want to update a specific cell in the table on the other hand I want to add a new row to the table.
I tried disabling this row by CancelEdit and then running BeginEdit but it does not take it out of normal focus. And still obliges me to complete the row.
How can this be done without adding more settings in XAML?
The datagrid does binidng with a DataTable that holds within it a db table with required fields. The problem is that when I try to get out of focus in the new row from any cell from the same row without really filling the same cell, the datagrid does not allow me to access other rows that are already filled with content and requires me to fill the last row.
I use with cellEditing event because I want to update a specific cell in the table on the other hand I want to add a new row to the table.
I tried disabling this row by CancelEdit and then running BeginEdit but it does not take it out of normal focus. And still obliges me to complete the row.
C#:
<!--file xaml-->
<Grid Background="#2e3137" Grid.Row="0" Grid.ColumnSpan="2">
<TextBox Margin="0,0,20,0" Style="{StaticResource resTextBox}" />
</Grid>
<Grid Name="SecondGrid" Margin="0,20,20,0" Grid.Row="1" Grid.Column="1">
<DataGrid Name="Dg" SelectionUnit="FullRow" VerticalAlignment="Top" VerticalScrollBarVisibility="Auto" FlowDirection="RightToLeft" HorizontalContentAlignment="Center" HorizontalAlignment="Right" FontSize="18" HorizontalScrollBarVisibility="Auto" Foreground="{Binding ElementName=GridMain, Path=Background}" Background="#FFE3E3E3" RowStyle="{StaticResource RowStyleWithAlternation}"
AutoGenerateColumns="False" CellEditEnding="Dg_CellEditEnding" />
</Grid>
</Grid>
//in code behind
private void FillDataGrid()
{
//schemaTable is dataTable of schema of table of db
foreach (DataRow dr in pro.schemaTable.Rows)
{
DataGridTextColumn title = new DataGridTextColumn();
title.Header = dr[0];
title.Binding = new Binding(title.Header.ToString())
{ Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged };
if (bool.Parse(dr["IsAutoIncrement"].ToString()))
{
title.Visibility = Visibility.Hidden;
string uniqe = dr[0].ToString();
pro.currentTable.Columns[uniqe].DefaultValue = 0;
}
Dg.Columns.Add(title);
}
Dg.ItemsSource = pro.currentTable.DefaultView;
}
private void Dg_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DataRowView row_current = (e.Row.Item as DataRowView);
row_current.Row.CancelEdit();
row_current.Row.BeginEdit();
if (row_current.Row[pro.ColUniqe].ToString() == "0")
{
//.........
}
else
{
//....
}
}