try this
<asp:gridview id="dgv" runat="server" xmlns:asp="#unknown">
<columns>
<asp:templatefield headertext="Name" itemstyle-width="150">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem,"Name") %>
</itemtemplate>
</asp:templatefield>
<pre lang="xml"><asp:templatefield headertext=""RollNo"" itemstyle-width=""150"">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,'RollNo');
</asp:TemplateField></asp:templatefield>
in aspx.cs
in page load
if(!IsPostBack)
{
BindGrid();
}
then after Page load create
protected void BindGrid()
{
SqlConnection con=new SqlConnection(ConfigurationManager.appSettings["dbConnection"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tablename;
cmd.Connection = con;
con.Open();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dgv.DataSource = dt;
dgv.DataBind();
cmd.Dispose();
con.Close();
}
vote and accept solution
If this will help you
thanks