I am going to write a blog on "Gridview Code". This is my first blog on Simple Gridview Data Binding using c#.

Source Code

  1. <div>
  2. <asp:GridView ID="grdCompany" runat="server">
  3. </asp:GridView>
  4. </div>
C# Code

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. bindGridview();
  5. }
  6. public DataTable getDataTable()
  7. {
  8. DataTable dt = new DataTable();
  9. try
  10. {
  11. dt = new DataTable();
  12. dt.Columns.Add("id", typeof(int));
  13. dt.Columns.Add("company", typeof(string));
  14. dt.Columns.Add("address", typeof(string));
  15. dt.Columns.Add("pincode", typeof(string));
  16. dt.Rows.Add(1, "Capgemini", "Vikhroli East, Mumbai", "400 079");
  17. dt.Rows.Add(2, "TCS", "Tech Park, Bangalore", "560066");
  18. dt.Rows.Add(3, "Wipro IT", "Guindy, Chennai", "600032");
  19. }
  20. catch { throw; }
  21. return dt;
  22. }
  23. public void bindGridview()
  24. {
  25. grdCompany.DataSource = getDataTable();
  26. grdCompany.DataBind();
  27. }
Code Output