Show Database Table in DataGridView

DataGridView is a type of toolbox that can show data from database table. Here I am going to discuss about dataGridView. 
In my previous post, I had already discussed about insert,upadate and delete data in a database table.In that post I uesd a window.Now I am just adding a dataGridView toolbox in that window and going to discuss about showing tables in that dataGridView .
After adding dataGridView,  the window will becomes like this,
Now add the following code in Gateway.cs Class ,

public static List<StudentMark> GetStudent()
string connectionString = @”
Here write our own Data Source“;
SqlConnection connection = new SqlConnection(connectionString);

string query = @”select * from t_Mark”;
SqlCommand command = new SqlCommand(query, connection);
SqlDataReader reader = command.ExecuteReader();

List<StudentMark> studentList = new List<StudentMark>();

while (reader.Read())
StudentMark studentObj = new StudentMark()
StudentRoll = reader[0].ToString(),
Bangla = reader[1].ToString(),
English = reader[2].ToString(),
Math = reader[3].ToString()

return studentList;

Now double click on SHOW BUTTON :

private void showButton_Click(object sender, EventArgs e)
List<StudentMark> studentList = Gateway.GetStudent();

dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = studentList;

Now if we insert,update or delete any information in a database table and click on SHOW BUTTON then we can see the information in dataGridView box from database.I give an example here,

Leave a Reply