Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

dataGrid row

$
0
0

Hi, I have a datagrid that is bind to a DataTable. I add values in datagrid cells and save-delete-update rows.

If I add a new value in  one cell only of datagrid row, I want, after the msg, the user to be able to add the empty cell and save.

if the user adds the missing value after msg an error appears: Syntax error (missing operator) in query expression 'id='

XAML:

<DataGrid x:Name="dGService" AutoGenerateColumns="False" ItemsSource="{Binding}"RowEditEnding="rowEditEnding"><DataGrid.Columns><DataGridTextColumn Header="vehicle" Binding="{Binding vehicle, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/><DataGridTextColumn Header="dateService" EditingElementStyle="{StaticResource errorStyle}" Binding="{Binding Path=dateService, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/></DataGrid.Columns></DataGrid>

C#:

private void rowEditEnding(object sender, DataGridRowEditEndingEventArgs e) { // connection OleDbCommand cmd = new OleDbCommand(); if (con.State != ConnectionState.Open) con.Open(); cmd.Connection = con; DataRowView row = (DataRowView)dGService.SelectedItems[0]; // if row is selected if (dGService.SelectedItems.Count > 0) { // if one of the cells is empty if (string.IsNullOrWhiteSpace(row["vehicle"].ToString()) || string.IsNullOrWhiteSpace(row["dateService"].ToString()))//----if one cell is empty {

MessageBox.Show("ADD MISSING VALUE...") } else { // if row is new if (row.IsNew) { // save try { cmd.CommandText = "insert into service(vehicle,dateService) Values('" + row["vehicle"] + "','" + row["dateService"] + "')"; cmd.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(": " + ex.Message, "Επισήμανση", MessageBoxButton.OK, MessageBoxImage.Warning); } finally { con.Close(); } } // if row is not new update else { try { cmd.CommandText = "update service set vehicle = '" + row["vehicle"] + "', dateService = '" + row["dateService"] + "' where id = " + row["id"] + ""; cmd.ExecuteNonQuery(); LoadData(); } catch (Exception ex) { MessageBox.Show(": " + ex.Message, "Επισήμανση", MessageBoxButton.OK, MessageBoxImage.Warning); } finally { con.Close(); } } } } //else no selection else { MessageBox.Show(""); } } }

Thanks in advance




Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>