01.private void button1_Click(object sender, EventArgs e)
02.{
03. dataGridView2.Columns.Add("Col1", "Column1");
04. dataGridView2.Columns.Add("Col2", "Column2");
05. System.Collections.ArrayList col1 = new System.Collections.ArrayList();
06. System.Collections.ArrayList col2 = new System.Collections.ArrayList();
07. OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/mdb1.mdb");
08. OleDbDataReader dr;
09. try
10. {
11. Conn.Open();
12. string str = "SELECT Name FROM Student";
13. OleDbCommand cmd = new OleDbCommand(str, Conn);
14. dr = cmd.ExecuteReader();
15. while (dr.Read())
16. {
17. //Here It Will Store Names In "col1" Arraylist...
18. col1.Add(dr.GetValue(0).ToString());
19. }
20. Conn.Close();
21. }
22. catch (Exception ex)
23. {
24. Conn.Close();
25. MessageBox.Show(ex.ToString());
26. }
27.
28. try
29. {
30. Conn.Open();
31. string str = "SELECT age FROM Student";
32. OleDbCommand cmd = new OleDbCommand(str, Conn);
33. dr = cmd.ExecuteReader();
34. while (dr.Read())
35. {
36. //Here It Will Store Ages In "col2" Arraylist...
37. col2.Add(dr.GetValue(0).ToString());
38. }
39. Conn.Close();
40. }
41. catch (Exception ex)
42. {
43. Conn.Close();
44. MessageBox.Show(ex.ToString());
45. }
46.
47.
48. //Here It Will Add Data To The DataGridView...
49. int i = 0;
50. while (i < col1.Count)
51. {
52. //Add Names...
53. dataGridView2.Rows[i].Cells[0].Value = col1[i].ToString();
54.
55. //Add Ages...
56. dataGridView2.Rows[i].Cells[1].Value = col2[i].ToString();
57. i++;
58. }
59.}