Bind Gridview fron Database in ASP.Net C#
Gridview control is the one of the main control of ASP.Net.
Gridview control is used to display data from Database to user webform in specific format.
STEP – 1 First Bind data to gridview we have to make connection between Database and our ASP.Net website.
learn for connectivity visit – connectivity with Database with ASP.Net
STEP – 2 After making Connection take Gridview control on webform.
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<table style=”z-index: 100; left: 312px; position: absolute; top: 120px”>
<tr>
<td style=”width: 100px”>
<asp:GridView ID=”GridView1″ runat=”server” BackColor=”White” BorderColor=”#CC9966″ BorderStyle=”None” BorderWidth=”1px” CellPadding=”4″>
<RowStyle BackColor=”White” ForeColor=”#330099″ />
<FooterStyle BackColor=”#FFFFCC” ForeColor=”#330099″ />
<PagerStyle BackColor=”#FFFFCC” ForeColor=”#330099″ HorizontalAlign=”Center” />
<SelectedRowStyle BackColor=”#FFCC66″ Font-Bold=”True” ForeColor=”#663399″ />
<HeaderStyle BackColor=”#990000″ Font-Bold=”True” ForeColor=”#FFFFCC” />
</asp:GridView>
</td>
</tr>
</table></div>
</form>
</body>
</html>
STEP – 3 Now write the below code on page load
public partial class _Default : System.Web.UI.Page
{
// Declare DataTable and TableAdapter Object here..
User_DS.USERMST_SELECTDataTable USerDT = new User_DS.USERMST_SELECTDataTable();
User_DSTableAdapters.USERMST_SELECTTableAdapter UserAdapter = new User_DSTableAdapters.USERMST_SELECTTableAdapter();protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
USerDT = UserAdapter.Select();GridView1.DataSource = USerDT;
GridView1.DataBind();}
}
}