title: Style to display data in gridview
body: <p>I am trying to display data in <code>Gridview</code> in c#. I have been asked to make the gridview look as close as a table in SQL database. I have been able to achieve a bit but my grid is far from a Sql table. Can someone help me out? This is what I tried so far.</p>
  
  <p><strong>Aspx</strong></p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Reports.aspx.cs" Inherits="Reports" %&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &lt;asp:Label ID="Label1" runat="server" Text="Enter Query"&gt;&lt;/asp:Label&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;asp:TextBox ID="TextBox1" runat="server"&gt;&lt;/asp:TextBox&gt;
      &lt;/div&gt;
          &lt;p&gt;
          &lt;asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /&gt;
              &lt;div id="grdCharges" runat="server" style="width: 875px; overflow: auto; height: 600px;"&gt;
                  &lt;asp:GridView ID="grd1" runat="server" AutoGenerateColumns="true" Width="150%"&gt;
                  &lt;/asp:GridView&gt;
  
              &lt;/div&gt;
          &lt;/p&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>C#</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Data.SqlClient;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  
  public partial class Reports : System.Web.UI.Page
  {
  
      protected void Button1_Click(object sender, EventArgs e)
      {
          SqlConnection con = null;
          SqlDataReader reader = null;
  
          try
          {
              string q = TextBox1.Text.ToString();
              string strcon = "Data Source=***********";
              con = new SqlConnection(strcon);
  
              SqlCommand cmd = new SqlCommand();
              cmd.CommandText = q;
              cmd.CommandType = CommandType.Text;
              cmd.Connection = con;
  
              con.Open();
  
              reader = cmd.ExecuteReader();
              // Data is accessible through the DataReader object here.
              var dataTable = new DataTable();
              dataTable.Load(reader);
              grd1.DataSource = dataTable;
              grd1.DataBind();   
          }
          catch (Exception ex)
          {
              //....
          }
          finally
          {
              if(reader != null)
                  reader.Close();
  
              if(con != null &amp;&amp; con.ConnectionState != ConnectionState.Closed)
                  con.Close();
          }
      }
  }
  </code></pre>
  
  <p>This is how my table looks like - 
  Current table -
  <a href="https://i.stack.imgur.com/24Y8T.png" rel="nofollow noreferrer">Current table</a></p>
  
  <p>Target table -
  <a href="https://i.stack.imgur.com/6FCRw.png" rel="nofollow noreferrer">Targettable</a></p>