简体   繁体   中英

SQLite Datagrid Validation

I have a DataGrid on a WPF form that has the ItemsSource property set to a DataTable . This DataTable is populated from an SQLite table, and I'm displaying one of the columns by using a DataGridTemplateColumn that is filled with a ComboBox . The ComboBox is correctly showing the DisplayMember (string) and SelectedValue (int) from class DummyClass . However, when I select a value from the ComboBox the DataGrid is displaying a Validation error (red exclamation mark) next to the row.

I added an event handler for when the selection is changed for the ComboBox so that I could view the the validation error. The validation error reads:

Value '1' could not be converted.

where the '1' is whatever index was just selected in the ComboBox .

What is causing this validation error?

        InitializeComponent();

        DummyClassCollection = new ObservableCollection<DummyClass>();
        DummyClassCollection.Add(new DummyClass() { DisplayValue = "Item1", SelectedValue = 0 });
        DummyClassCollection.Add(new DummyClass() { DisplayValue = "Item2", SelectedValue = 1 });

        table = new DataTable();
        dgData.ItemsSource = table.DefaultView;

        DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
        DataTemplate dataTemplate = new DataTemplate();
        FrameworkElementFactory control = new FrameworkElementFactory(typeof(ComboBox));

        control.SetValue(ComboBox.ItemsSourceProperty, DummyClassCollection);
        control.SetValue(ComboBox.DisplayMemberPathProperty, "DisplayValue");
        control.SetValue(ComboBox.SelectedValuePathProperty, "SelectedValue");

        control.AddHandler(ComboBox.SelectionChangedEvent, new SelectionChangedEventHandler(ComboBox_SelectionChanged));

        Binding b = new Binding();
        b.Path = new PropertyPath("tableID");
        b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        b.Mode = BindingMode.TwoWay;

        control.SetBinding(ComboBox.SelectedValueProperty, b);

        dataTemplate.VisualTree = control;
        templateColumn.CellTemplate = dataTemplate;

        //dgData.AutoGenerateColumns = false;
        dgData.Columns.Add(templateColumn);

        string connection = @"Data Source = " + database + "; Version=3; foreign keys=true;";
        sqlc = new SQLiteConnection(connection);
        sqlcmd = new SQLiteCommand("SELECT * FROM tblExample", sqlc);
        adapter = new SQLiteDataAdapter(sqlcmd);
        sqlcmdb = new SQLiteCommandBuilder(adapter);

        adapter.InsertCommand = sqlcmdb.GetInsertCommand();
        adapter.UpdateCommand = sqlcmdb.GetUpdateCommand();
        adapter.DeleteCommand = sqlcmdb.GetDeleteCommand();
        adapter.AcceptChangesDuringUpdate = true;
        adapter.Fill(table);

        templateColumn.Header = table.Columns[1].ColumnName;

I just worked out a similar problem but in a Windows form DataGrid object. I'm using SQLite tables as the data sources. I had assigned a DataGridViewComboBoxColumn with a DisplayMember and ValueMember from a specific SQLite table. But the DataGrid did not display the DisplayMember value when I ran the code, all I could see was the integer value stored in the table bound to the DataGrid.

Turns out the SQLite datatype for the ValueMember table did not match the datatype of the field assigned to a combo box in the DataGrid. The ValueMember field in the lookup table was set to a SQLite data type 'Integer'. The SQLite data type in the combo box field of the SQLite table bound to the DataGrid table is SQLite type 'int'.

SQLite type 'Integer' maps to .Net System.Int64 which in code is typeof(Long).

SQLite type 'int' maps to to .Net System.Int32 which in code is typeof(int).

So I modified the SQLite tables to be sure all integer type fields are SQLite type 'int'.

I then modified my C# DataSet code so that all integer type fields are set to typeof(int).

And now the DataGridViewComboBoxColumn is behaving as expected.

The column in the SQLite table is of type Long where my observable collection property was of type int . This was causing the data validation error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM