Thursday, 3 July 2014

Binding Data to the GridView


This post says about how to bind the data into the GridView

Firstly add the GridView into the page from tool box.

Check the .aspx code and update if necessary to

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" BorderStyle="Double" OnRowDataBound = "OnRowDataBound" OnSelectedIndexChanged = "GridView1_OnSelectedIndexChanged"> </asp:GridView>

In the .aspx.cs file, update the Page_Load function

if (!Page.IsPostBack)
{
       string oCommand = "SELECT * from table";
       SqlConnection oConnection = new SqlConnection(ConnectionString);
       SqlCommand oCommand = new SqlCommand(oCommand );
       oCommand.Connection = oConnection;
       SqlDataAdapter adpt = new SqlDataAdapter(oCommand);
       DataSet ds = new DataSet();
       adpt.Fill(ds);
       grid.DataSource = ds.Tables[0];
       grid.DataBind();
}




No comments:

Post a Comment