GridView Control In Asp.net
This Article explain the GridView Control and its use in Asp.net program
What is GridView Control?
Grid View- Composite data bound control
-
To display data in rows and columns
(T-form)
-
We can manipulate data
-
It have default template
-
Paging is pre-defined
# Templates applied on Grid view – 8
1. ItemTemplate
2. AlternatingItemTemplate
3. HeaderTemplate
4. FooterTemplate
5. EditItemTemplate
6. SelectedItemTemplate
7. PagerTemplate
8. EmptyDataTemplate
How to work with GridView Control in Asp.net
# Open new project in Asp.net
* Add Webform And make connection string
<connectionStrings>
<add name="ps" connectionString="server=DESKTOP-I3R20OL\SQLEXPRESS;
database=database1; integrated security=true"/>
</connectionStrings>
|
# Webform > Design> Toolbox> Data>
GridView
*Now bind data with Gridview
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack==false)
{
Grid_Bind();
}
}
private void Grid_Bind()
{
SqlDataAdapter adp = new SqlDataAdapter("select * from tbemp", ConfigurationManager.ConnectionStrings["ps"].ConnectionString);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
|
# Customize GridView
* GridView properties > Auto Generate column =
False
* GridView properties> Columns>option
*In Templatefield
Header text = Empno
Header text = Employees Details
# Html Source
For Column Empno
<asp:TemplateField HeaderText="Empno">
<ItemTemplate>
<%#Eval("empno")%>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lb1" Text='<%#Eval("empno")%>' runat="server" />
</EditItemTemplate>
</asp:TemplateField>
|
For Column Employees Details
<asp:TemplateField HeaderText="Employees Details">
<ItemTemplate>
<b>Name : </b><%#Eval("ename")%><br />
<b>Address : </b><%#Eval("eadd")%><br />
<b>Salary : </b><%#Eval("esal")%><br />
<asp:LinkButton ID="lk" Text="Edit" CommandName="edit" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<b>Name : </b><asp:TextBox ID="t1" Text='<%#Eval("ename")%>' runat="server" /><br />
<b>Address : </b><asp:TextBox ID="t2" Text='<%#Eval("eadd")%>' runat="server" /><br />
<b>Salary : </b><asp:TextBox ID="t3" Text='<%#Eval("esal")%>' runat="server" /><br />
<asp:LinkButton ID="lk1" Text="Update" CommandName="update" runat="server" />
<asp:LinkButton ID="lk2" Text="Cancel" CommandName="cancel" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
|
#For Edit Command
Gridview>Event>RowEditing
protected void GridView1_RowEditing(object
sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Grid_Bind();
}
|
#For Cancel Command
Gridview>Event>RowCancelingEdit
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Grid_Bind();
}
|
#For Updating Command
Gridview>Event>RowUpdating
protected void GridView1_RowUpdating(object
sender, GridViewUpdateEventArgs e)
{
Int32 empno, esal;
string ename, eadd;
empno =
Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lb1")).Text);
ename = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("t1")).Text;
eadd= ((TextBox)GridView1.Rows[e.RowIndex].FindControl("t2")).Text;
esal =
Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].FindControl("t3")).Text);
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ps"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand("update tbemp set ename=@en,eadd=@ed,esal=@es where
empno=@eno");
cmd.Connection = con;
cmd.Parameters.Add("@eno", SqlDbType.Int).Value = empno;
cmd.Parameters.Add("@en", SqlDbType.VarChar,50).Value = ename;
cmd.Parameters.Add("@ed", SqlDbType.VarChar, 50).Value = eadd;
cmd.Parameters.Add("@es", SqlDbType.Int).Value = esal;
cmd.ExecuteNonQuery();
cmd.Dispose();
GridView1.EditIndex = -1;
Grid_Bind();
}
|
No comments:
Post a Comment