Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
C#
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];
        }
Posted
Comments
[no name] 12-May-13 15:11pm    
You need to write an INSERT statement to add records to a database.
vishal deb 12-May-13 15:20pm    
Thank you for suggestion but... I don't want to insert record in database, I want to show record of database, as 1 record on 1 button click in datagridview
[no name] 12-May-13 15:28pm    
That was not the question that you asked. If you want to show record 1, then record 2, etc you have to query for it and keep track of which record you are displaying.
vishal deb 12-May-13 15:36pm    
Sorry for confusion I actually mean by this "I want to add record of access database in datagridview when button is clicked" is " I want to show record of database in datagridview" I am able to view all records at once on button click. But this is not my purpose... Can you give some more lights on the idea
[no name] 12-May-13 19:10pm    
you have to query for it and keep track of which record you are displaying.

1 solution

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:
C#
counter +=1;
currentrow = DataTable.Rows[counter];
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900