private void Page_Load(object sender, System.EventArgs e)
{
cnnDB = new SqlConnection("server=HOME;user id=sa;password=;database=Contacts");
if (!IsPostBack)
bindGrid();
}
private void bindGrid()
{
SqlDataAdapter da = new SqlDataAdapter("select * from Anz_Contacts",cnnDB);
DataSet ds = new DataSet();
da.Fill(ds, "Employees");
myDataGrid.DataSource=ds.Tables["Employees"].DefaultView;
myDataGrid.DataBind();
}
<form id="Form1" name="Form1" method="post" runat="server">
<ASP:DATAGRID id="myDataGrid"
runat="server"
AutoGenerateColumns="false"
HeaderStyle-BackColor="#aaaadd"
Font-Size="8pt"
Font-Name="Verdana"
CellSpacing="0"
CellPadding="3"
ShowFooter="true"
BorderColor="Black"
BackColor="AntiqueWhite"
Width="600">
<HeaderStyle BackColor="NavajoWhite"></HeaderStyle>
<FooterStyle BackColor="NavajoWhite"></FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="contract">
<HeaderTemplate>
<input type="checkbox" id="checkAll"
onclick="CheckAll(this);" runat="server" name="checkAll">
</HeaderTemplate>
<ItemTemplate>
<input type="checkbox" runat="server" id="EmpId"
onclick="CheckChanged();" checked='false' name="EmpId" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Id">
<ItemTemplate>
<asp:Label id="Id" Text='<%#
DataBinder.Eval(Container.DataItem, "Id") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="FirstName"
HeaderText="FirstName"></asp:BoundColumn>
<asp:BoundColumn DataField="LastName"
HeaderText="LastName"></asp:BoundColumn>
<asp:BoundColumn DataField="Designation"
HeaderText="Designation"></asp:BoundColumn>
</Columns>
</ASP:DATAGRID>
</form >
Script to create table in SQL is as :
CREATE TABLE [dbo].[Anz_Contacts] (
[Id] [int] IDENTITY (1, 1) NOT NULL ,
[FirstName] [char] (10) NULL ,
[LastName] [char] (10) NULL ,
[Designation] [varchar] (50) NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Anz_Contacts] WITH NOCHECK ADD
CONSTRAINT [PK_Anz_Contacts] PRIMARY KEY CLUSTERED
(
[Id]
) ON [PRIMARY]
GO