簡體   English   中英

如何使用DataGrid C#WPF更新數據庫?

[英]How to update a database using DataGrid C# WPF?

我想用dataGrid更新數據庫(SQLite)。 我可以更改dataGrid中的值,但更改不適用於數據庫,為什么我要添加按鈕“save”以保存dataGrid中應用的所有更改,

我搜索了這個問題,但我找不到答案

這是我填充DataGrid的C#代碼:

SQLiteConnection sQLiteConnection = new SQLiteConnection("Data source = dataSet.db ; version = 3 ;");
        SQLiteCommand sQLiteCommand; 
        SQLiteDataAdapter sQLiteDataAdapter; 
        System.Data.DataTable dataTable = new System.Data.DataTable("Naamaa");
        SQLiteCommandBuilder cmd;
        DataSet ds;
        public ManipulateData(string wilaya, int theme)
        {
            InitializeComponent();

            try
            {
                sQLiteConnection.Open();
                sQLiteCommand = new SQLiteCommand(sQLiteConnection);
                sQLiteCommand.CommandText = "SELECT * FROM " + wilaya + " ;";
                sQLiteCommand.ExecuteNonQuery();
                sQLiteDataAdapter = new SQLiteDataAdapter(sQLiteCommand);
                sQLiteDataAdapter.Fill(dataTable);

                McDataGrid.ItemsSource = dataTable.DefaultView;


            }catch
            {
                MessageBox.Show("Error");
            }
        }

這是xaml代碼:

<Grid Background="White">
        <DataGrid x:Name="McDataGrid"  />
</Grid>

我正在研究一些非常類似的東西,只是我正在使用Postgres。

一種簡單的方法是將DataGridView.ReadOnly屬性設置為true,然后當用戶在DataGridView中選擇一行時,您可以啟動一個包含所選行中所有數據的Windows窗體和一個“保存”按鈕來更新數據數據庫。 然后,從該按鈕,您只需要使用從表單收集的數據啟動對數據庫的更新查詢。

如果你想修改DataGridView本身的數據,事情會變得更加復雜,因為你需要跟蹤DataGridView上已編輯的行(除非你不關心優化,只能對每一行進行更新,否則如果它已被編輯,則無關緊要)。 您可以為所有已修改的行使用DataGridViewRow列表。

那可以理解嗎? 不習慣寫英文,特別是在非常復雜的問題上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM