In this article i show you how to use gridview with templatefields in asp.net
step1:-Create table
CREATE TABLE tblEmp
(
Id int primary key identity,
FirstName varchar(50),
LastName varchar(50),
Designation varchar(50),
Salary int,
City varchar(50),
)
Step2:-Insert some data in table
INSERT INTO tblEmp VALUES('SIVA','Garika','UiDeveloper',15000,'Hydarabad')
INSERT INTO tblEmp VALUES('Narayana','Garika','Developer',25000,'Hydarabad')
INSERT INTO tblEmp VALUES('Kiran','Thota','Developer',35000,'Hydarabad')
INSERT INTO tblEmp VALUES('Madhu','Thota','Developer',19000,'Hydarabad')
INSERT INTO tblEmp VALUES('Manasa','Javisetty','Testar',15000,'Hydarabad')
INSERT INTO tblEmp VALUES('Malavika','Dongari','Developer',15000,'Hydarabad')
INSERT INTO tblEmp VALUES('Mounika','Javisetty','Testar',15000,'Hydarabad')
INSERT INTO tblEmp VALUES('Anji','Dongari','Developer',25000,'Hydarabad')
INSERT INTO tblEmp VALUES('Aravind','Choodi','Developer',32000,'Hydarabad')
INSERT INTO tblEmp VALUES('Avinash','Choodi','Developer',26000,'Hydarabad')
Step3:-Create stored procedure
Create Procedure USP_GetEmployeeData
AS
BEGIN
select *from tblEmp
END
Step4:-Execute procedure
EXEC USP_GetEmployeeData
Output:-
Step6:-Take one class as Emp
public class Emp
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get; set; }
public string Designation { get; set; }
public int Salary { get; set; }
public string City { get; set; }
}
Step7:- Source code
<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal">
<Columns>
<asp:TemplateField HeaderText="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FullName">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("FullName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("FullName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Designation") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Designation") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Salary") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Salary") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="White" HorizontalAlign="Center" BackColor="#336666" />
<RowStyle BackColor="White" ForeColor="#333333" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#487575" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#275353" />
</asp:GridView>
Step8:-Code behind
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
GetEmpyoyeeData();
}
}
private void GetEmpyoyeeData()
{
List<Emp> list = new List<Emp>();
string str = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("USP_GetEmployeeData", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Emp obj = new Emp();
obj.ID = Convert.ToInt32(dr["Id"]);
obj.FirstName = dr["FirstName"].ToString();
obj.LastName = dr["LastName"].ToString();
obj.FullName=dr["FirstName"].ToString()+ " "+dr["LastName"].ToString();
obj.Designation = dr["Designation"].ToString();
obj.Salary = Convert.ToInt32(dr["Salary"]);
obj.City = dr["City"].ToString();
list.Add(obj);
}
GridView1.DataSource = list;
GridView1.DataBind();
con.Close();
}
Output:-
step1:-Create table
CREATE TABLE tblEmp
(
Id int primary key identity,
FirstName varchar(50),
LastName varchar(50),
Designation varchar(50),
Salary int,
City varchar(50),
)
Step2:-Insert some data in table
INSERT INTO tblEmp VALUES('SIVA','Garika','UiDeveloper',15000,'Hydarabad')
INSERT INTO tblEmp VALUES('Narayana','Garika','Developer',25000,'Hydarabad')
INSERT INTO tblEmp VALUES('Kiran','Thota','Developer',35000,'Hydarabad')
INSERT INTO tblEmp VALUES('Madhu','Thota','Developer',19000,'Hydarabad')
INSERT INTO tblEmp VALUES('Manasa','Javisetty','Testar',15000,'Hydarabad')
INSERT INTO tblEmp VALUES('Malavika','Dongari','Developer',15000,'Hydarabad')
INSERT INTO tblEmp VALUES('Mounika','Javisetty','Testar',15000,'Hydarabad')
INSERT INTO tblEmp VALUES('Anji','Dongari','Developer',25000,'Hydarabad')
INSERT INTO tblEmp VALUES('Aravind','Choodi','Developer',32000,'Hydarabad')
INSERT INTO tblEmp VALUES('Avinash','Choodi','Developer',26000,'Hydarabad')
Step3:-Create stored procedure
Create Procedure USP_GetEmployeeData
AS
BEGIN
select *from tblEmp
END
Step4:-Execute procedure
EXEC USP_GetEmployeeData
Output:-
Step5:-Go to visuavl studio
put connections in web.config fileStep6:-Take one class as Emp
public class Emp
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get; set; }
public string Designation { get; set; }
public int Salary { get; set; }
public string City { get; set; }
}
Step7:- Source code
<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal">
<Columns>
<asp:TemplateField HeaderText="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FullName">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("FullName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("FullName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Designation") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Designation") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Salary") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Salary") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="White" HorizontalAlign="Center" BackColor="#336666" />
<RowStyle BackColor="White" ForeColor="#333333" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#487575" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#275353" />
</asp:GridView>
Step8:-Code behind
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
GetEmpyoyeeData();
}
}
private void GetEmpyoyeeData()
{
List<Emp> list = new List<Emp>();
string str = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("USP_GetEmployeeData", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Emp obj = new Emp();
obj.ID = Convert.ToInt32(dr["Id"]);
obj.FirstName = dr["FirstName"].ToString();
obj.LastName = dr["LastName"].ToString();
obj.FullName=dr["FirstName"].ToString()+ " "+dr["LastName"].ToString();
obj.Designation = dr["Designation"].ToString();
obj.Salary = Convert.ToInt32(dr["Salary"]);
obj.City = dr["City"].ToString();
list.Add(obj);
}
GridView1.DataSource = list;
GridView1.DataBind();
con.Close();
}
Output:-
No comments:
Post a Comment