如何在按钮单击时从访问数据库添加datagridview中的行 [英] How to add row in datagridview from access database on per button click

查看:82
本文介绍了如何在按钮单击时从访问数据库添加datagridview中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个访问数据库,我在按钮点击事件中填充datagridview。但是当我点击按钮时,我想要在datagridview中添加访问数据库的记录。第一次点击1记录的手段应该在datagridview中添加,第二次点击另一条记录应该添加等等...



我需要做什么修改我的代码,请指导我

I have a access database from where I am populating the datagridview on button click event. but what I exactly I want to add record of access database in datagridview when button is clicked. Means on 1st click 1 record should be add in datagridview, on 2nd click another record should be add and so on...

What modification I need to make in my code, please guide me

private void button1_Click(object sender, EventArgs e)
        {
            aCommand3 = new OleDbCommand("select * from batch_tbl", main_connection);
            aAdapter3 = new OleDbDataAdapter(aCommand3);

            ds3 = new DataSet();
            aAdapter3.Fill(ds3, "app_info");


            ds3.Tables[0].Constraints.Add("pk_bno", ds3.Tables[0].Columns[0], true);
            int batch_count = ds3.Tables[0].Rows.Count;

            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.DataSource = ds3.Tables[0];
        }

推荐答案

您可以通过两种方式完成:

1)创建查询返回第n条记录: http://www.techrepublic。 com / blog / msoffice / an-access-query-that-returns-every-nth-record / 3617 [ ^ ]

2)将所有数据提取到数据表中然后循环遍历行集合。有关 DataTable类(System.Data) [ ^ ]。

例如:

You can do it in 2 ways:
1) creating query that returns nth record: http://www.techrepublic.com/blog/msoffice/an-access-query-that-returns-every-nth-record/3617[^]
2) fetching all data into datatable and then looping through the rows collection. More about DataTable class (System.Data)[^].
For example:
counter +=1;
currentrow = DataTable.Rows[counter];


这篇关于如何在按钮单击时从访问数据库添加datagridview中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆