如何将更改从“文本框”保存到数据库?

问题描述:

来自"dataGridView1"的当前条目显示在"textBox1"中。

"textBox1"中的用户进行更改。



如何保存来自"textBox"的更改到数据库并在"dataGridView1"中显示?b
$
我附上我的决定代码。

如果表格有几个" textBox"," checkBox1",然后为每个元素制作一个事件,不知何故麻烦......



还有其他更有效的方法吗?

The current entry from "dataGridView1" is displayed in "textBox1".
The user in "textBox1" makes changes.

How to save changes from "textBox" to the database and display in "dataGridView1"?

I enclose the code of my decision.
If the form has several "textBox", "checkBox1", then make an event for each element, somehow troublesome ...

Are there any other more efficient ways to do this?

public partial class Frm1UC : UserControl
    {
        DataTable dt;        
 
        OleDbConnection connection;
        OleDbDataAdapter adapter;
        OleDbCommandBuilder commandBuilder;
        
 
        static string catBD = @"c:\vs\csharp\db_GridVAccess.accdb";
        string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}", catBD);
 
        string sql = "SELECT * FROM TreeFolder_tbl";
 
        // *** Конструктор
        public Frm1UC()
        {
            InitializeComponent();
        }
 
        // *** Событие. Загрузка формы
        private void Frm1UC_Load(object sender, EventArgs e)
        {
            using (OleDbConnection cn = new OleDbConnection())
            {
                connection = new OleDbConnection(connectionString);
                connection.Open();
                                
                adapter = new OleDbDataAdapter(sql, connection);
 
                commandBuilder = new OleDbCommandBuilder(adapter);
 
                
                dt = new DataTable();
                adapter.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }
 
 
        // Выбор ячейки
        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
        }
 
        // Событие. Изменение текста
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.CurrentRow.Cells[1].Value = textBox1.Text;
        }
 
        // Сохранить
        public void Save()
        {
            adapter.Update(dt);
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Save();
        }
 
      
    }

一种简单的方法将数据绑定到TextBox到相关字段,这里dt是DataGridView的DataTable。

A simple method would be to data bind to the TextBox to the field in question, here dt is the DataTable for the DataGridView.

textBox1.DataBindings.Add("Text", dt, "ProductName");

然后订阅TextBox的Leave事件 

Then subscribe to the Leave event of the TextBox 

textBox1.Leave += TextBox1_Leave;

离开事件

private void TextBox1_Leave(object sender, EventArgs e)
{
    var dt = (DataTable) dataGridView1.DataSource;
    dt.Rows[dataGridView1.CurrentRow.Index].EndEdit();
}

以上提供的内容可以让您朝着正确的方向前进。

The above provides should get you in the right direction.

最后,只要您的数据源设置完毕对于DataTable,访问数据应该像Leave事件中所示类似地完成。另外我建议考虑使用BindingSource组件,因为这个组件提供了更多选项来处理数据
设置为DataGridView以及其他控件但是是可选的。

Lastly, any time your data source is set to a DataTable, accessing data should be done similarly as shown in the Leave event. Also I would recommend considering using a BindingSource component as this component provides more options to working with data set to a DataGridView along with other controls but is optional.