3
until you specify the column names in GridView explicitly, the GridView is dynamic anyway. So, just call your SP and bind the result to GridView.
- public void BindGrid()
- {
- using (SqlConnection con = new SqlConnection("Data Source=SACHIN-PC\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True"))
- {
- SqlDataAdapter da = new SqlDataAdapter("spGetEmployee", con);
- da.SelectCommand.CommandType = CommandType.StoredProcedure;
- DataSet ds = new DataSet();
- dataAdapter.Fill(ds);
- GridView1.DataSource = ds;
- GridView1.DataBind();
- }
Note:- As a best practise, Always stored connection string in config file and retrieve from there
-
- string cs=ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
3
Hi, If you have the datatable, simply bind it with gridview.
GridView1.DataSource = datatable;
GridView1.DataBind();
2
i have datatable ,how to bind it in dynamic gridview
2
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=*******Initial Catalog=*******;Persist Security Info=True;User ID=*******;Password=*******" ></asp:SqlDataSource>
<div>
<asp:TextBox ID="SqlQueryTextBox" runat="server" ></asp:TextBox>
<asp:Button ID="ExecuteButton" OnClick="ExecuteButton_Click" runat="server" Text="Execute"/>
</div>
<div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="true" />
</div>
protected void ExecuteButton_Click(object sender, EventArgs e)
{
SqlDataSource1.SelectCommand = SqlQueryTextBox.Text;
GridView1.DataBind();
}