Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
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];
        }
Posted 12-May-13 9:51am
Comments
ThePhantomUpvoter at 12-May-13 15:11pm
   
You need to write an INSERT statement to add records to a database.
vishal deb at 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
ThePhantomUpvoter at 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 at 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
ThePhantomUpvoter at 12-May-13 19:10pm
   
you have to query for it and keep track of which record you are displaying.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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];
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 329
2 BillWoodruff 210
3 Afzaal Ahmad Zeeshan 204
4 CPallini 185
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 12 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100