Display Single DataTable Data In Different Columns Of DataGridView

This blog is based on the DataTable and DataGridView operations in C# WinForms application.
 
Here, my table name is Users which contains the data of the users of a company stored into the DataTable named as dt3.
 
no Emp_name
 1 Ramesh
 2 Satish
 3 Anant
 
I want to display this data table in GridView like in the following image.
 
Output
 
 no company_name user1 user2 user3
 1 TCS Ramesh Satish Anant
 
Note
Before writing the code, add the "usingsystem.data.sqlclient" namespace to your file.
 
Step 1
 
Make a database with a table in SQL Server.
 
Display Single DataTable Data In Different Column Of Datagridview

Step 2

Create a Windows application and add DataGridView on the form. Now, add a DataGridView control to the form by selecting it from the Toolbox and set properties according to your need.
 
Display Single DataTable Data In Different Column Of Datagridview

Step 3 - Adding Source Code for GridView

Now, you can add these few lines of code anywhere in your file to load the data from the database. Using the below-mentioned code, you can add a column to the DataGridView manually. Here, I have created a function named as display() to add a column manually and call it at the form load event.

  1. public void display() {  
  2.     try {  
  3.         DataTable dt2 = new DataTable(); //create datatable to add column to datagridview manually  
  4.         dt2.Columns.Add("Id", Type.GetType("System.String"));  
  5.         dt2.Columns.Add("company_name", Type.GetType("System.String"));  
  6.         dt2.Columns.Add("user1", Type.GetType("System.String"));  
  7.         dt2.Columns.Add("user2", Type.GetType("System.String"));  
  8.         dt2.Columns.Add("user3", Type.GetType("System.String"));  
  9.     } catch (Exception e) {  
  10.         MessageBox.Show(e.Message);  
  11.     }  
  12. }  
  13. private void Form2_Load(object sender, EventArgs e) {  
  14.     display();  
  15. }  

Step 4

Here, I am filling the dt3 data table with the employee names.
  1. string str = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\audit\employees\employees\App_data\Database1.mdf;Integrated Security=True";   
  2. SqlConnection sc = new SqlConnection(str);  
  3. sc.Open();  
  4. SqlDataAdapter sda = new SqlDataAdapter("select top 3 Id,Emp_name from Users", sc);  
  5. DataTable dt3 = new DataTable();  
  6. sda.Fill(dt3); //fill datatable by top3 employee name  
  7. sc.Close();  
Step 5
 
After step 4, I am adding the following code to display the datatable data in different columns of DataGridView.
  1. if (dt3.Rows.Count != 0)  
  2. {  
  3.     DataRow dr = dt2.NewRow();  
  4.     dr["Id"] = dt3.Rows[0]["Id"].ToString();  
  5.     dr["company_name"] = "TCS";  
  6.     int tbl_cnt = dt3.Rows.Count;  
  7.     int cnt = 0;  
  8.     dr["user1"] = dr["user1"] + dt3.Rows[cnt]["Emp_name"].ToString();  
  9.     cnt++;  
  10.     if (cnt < tbl_cnt) {  
  11.         dr["user2"] = dr["user2"] + dt3.Rows[cnt]["Emp_name"].ToString();  
  12.     }  
  13.     cnt++;  
  14.     if (cnt < tbl_cnt) {  
  15.         dr["user3"] = dr["user3"] + dt3.Rows[cnt]["Emp_name"].ToString();  
  16.     }  
  17.     dt2.Rows.Add(dr);  
  18. }  
  19. DataTable result = dt2;  
  20. result.AcceptChanges();  
  21. dataGridView1.DataSource = dt2;  
Output
 
Display Single DataTable Data In Different Column Of Datagridview