string connectionString = "Data Source=HP-PC; Initial Catalog=dbMahato; User id=sa; password=softech";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
try
{
string psql = "Select *from tblEmployee";
SqlCommand cmd = new SqlCommand(psql, con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable ds = new DataTable();
da.Fill(ds);
ds.Columns.Add("SNo");
for (int i = 0; i <= ds.Rows.Count - 1; i++)
{
ds.Rows[i]["SNo"] = i + 1;
}
gvEmployeeDetails.DataSource = ds;
gvEmployeeDetails.DataBind();
}
catch (Exception ex) { throw ex; }
finally
{
con.Close();
con.Dispose();
}
===================================================
<asp:GridView ID="gvEmployeeDetails" runat="server" AutoGenerateColumns="false" DataKeyNames="empID"
Style="margin-left: 0px" CssClass="fullTable zebraStrips" Width="100%"
AllowPaging="True" PageSize="25" GridLines="Horizontal" >
<Columns>
<asp:TemplateField HeaderText="S.No">
<ItemTemplate>
<asp:Label ID="lblSNo" runat="server" Text='<%#Bind("SNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emp No.">
<ItemTemplate>
<asp:Label ID="lblEmpNo" runat="server" Text='<%#Bind("empID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblRegistrationNo" runat="server" Text='<%#Bind("empName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bank A/C">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#Bind("bankAcNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender" Visible="false">
<ItemTemplate>
<asp:Label ID="lblOpdNo" runat="server" Text='<%#Bind("gender") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department">
<ItemTemplate>
<asp:Label ID="lblCitizenshipNo" runat="server" Text='<%#Bind("deptName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<ItemTemplate>
<asp:Label ID="lblDistrict" runat="server" Text='<%#Bind("desigName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DOB(NEP)">
<ItemTemplate>
<asp:Label ID="lblIssueLetter" runat="server" Text='<%#Bind("dob_Nep") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit/Delete" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CommandName="EditThis" Text="Edit" CommandArgument='<%#Bind("empID") %>'></asp:LinkButton>
<asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete" Text="Delete"
CommandArgument='<%#Bind("empID") %>' OnClientClick="return confirm('Are you confirm to Delete?')"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No Records Fetched!
</EmptyDataTemplate>
</asp:GridView>