Bind Gridview with DataTable in C# Dynamically

To bind gridview with datatable we need to write the code like as shown below

<html xmlns="">
<title>Create temparory table in using c#</title>
<form id="form1" runat="server">
<asp:GridView ID="gvDetails" runat="server">
<HeaderStyle BackColor="#DC5807" Font-Bold="true" ForeColor="White" />

protected void Page_Load(object sender, EventArgs e)
/// <summary>
/// Dynamically create & bind data to gridview
/// </summary>
protected void BindGridviewData()
DataTable dt = new DataTable();
dt.Columns.Add("UserId", typeof(Int32));
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("Education", typeof(string));
dt.Rows.Add(1, "Ashish Singh", "M.Tech");
dt.Rows.Add(2, "Ankur Srivatavai", "Msc");
dt.Rows.Add(3, "Madhav Sai", "MS");
dt.Rows.Add(4, "Praveen", "B.Tech");
dt.Rows.Add(6, "Sateesh", "MD");
dt.Rows.Add(7, "Raju Singh", "M.Tech");
dt.Rows.Add(8, "Rajesh", "MCA");
gvDetails.DataSource = dt;

Post a Comment