简体   繁体   English

C#DataGridView和oledb

[英]c# DataGridView & oledb

I've a database in MS Access. 我在MS Access中有一个数据库。
And i'm placing the data in the database to datagridview on form load. 而且我将表单中的数据放置在数据库中的datagridview中。

        string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + Application.StartupPath + "/Db.mdb";
        OleDbConnection conObj = new OleDbConnection(connectionString);
        conObj.Open();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        ds.Tables.Add(dt);
        OleDbDataAdapter da = new OleDbDataAdapter();
        da = new OleDbDataAdapter("SELECT * FROM StopMaster", conObj);
        da.Fill(dt);
        dataGridView1.DataSource = dt.DefaultView;
        conObj.Close();

on execution, it'll show datagridview with the items in the database, 执行后,它将显示datagridview及其数据库中的项目,
along with an empty row where we can add new data and if we click on the existing row's items with data we can update it and even remove it 以及一个空行,我们可以在其中添加新数据,如果单击包含数据的现有行的项目,我们可以对其进行更新甚至删除

how to code for that update delete and add and reflect to the database is there any event or what 如何为该更新编写代码,删除并添加并反映到数据库中,是否有任何事件或事件
also how can i make data grid view columns not resizable. 还如何使数据网格视图列不可调整大小。

also say if i have two columns "name" and "pin code" in the datagridview when i click on column header say for now "pin code"it must sort the datagridview rows in order 还说如果我单击列标题时在datagridview中是否有两列“名称”和“ pin码”,现在说“ pin码”,则必须按顺序对datagridview行进行排序
is there any event and how to for that too 是否有任何事件以及如何做到这一点

The first thing to mention, is that you will need to Bind your data table to the Datagridview. 首先要提到的是,您将需要将数据表绑定到Datagridview。 This is so that any changes you make whilst editing the DataGridView control reflect directly through to the underlying DataTable. 这样一来,您在编辑DataGridView控件时所做的任何更改都会直接反映到基础DataTable中。

I'll assume that you have already populated the DataTable object. 我假设您已经填充了DataTable对象。

You start by creating BindingSource object :- 首先创建BindingSource对象:

BindingSource bs = new BindingSource();

bs.Datasource = dt;

Now we need to make this Binding Object the datasource for your DataGridView Control :- 现在,我们需要将此绑定对象作为DataGridView控件的数据源:

dataGridView1.DataSource = bs;

In a method, you can use the following code to commit changes :- 在一种方法中,您可以使用以下代码来提交更改:

using (OleDbConnection con = new OleDbConnection(connectionString))
                {
                    var adaptor = new OleDbDataAdapter();
                    adaptor.SelectCommand = new OleDbCommand(""SELECT * FROM [StopMaster]", con);

                var cbr = new OleDbCommandBuilder(adaptor);

                cbr.GetDeleteCommand();
                cbr.GetInsertCommand();
                cbr.GetUpdateCommand();

                try
                {
                    con.Open();
                    adaptor.Update(dt);
                }
                catch (OleDbException ex)
                {
                    MessageBox.Show(ex.Message, "OledbException Error");
                }
                catch (Exception x)
                {
                    MessageBox.Show(x.Message, "Exception Error");
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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