Grid Binding and Update in same row
Grid Binding and Update in same row
Write
the below code in aspx page
<h3>Employee Details</h3>
<div>
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#15044e" HeaderStyle-ForeColor="White" runat="server" AutoGenerateColumns="false"
ShowFooter="true" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_Edit" OnRowUpdating="Gridview1_RowUpdating" OnRowDeleting="GridView1_RowDeleting"
HeaderStyle-Width="20%" OnRowCancelingEdit="GridView1_Cancel">
<Columns>
<asp:TemplateField ControlStyle-Height="40%" HeaderText="Id">
<EditItemTemplate>
<asp:Label ID="lbleditid" runat="server" Text='<%# Bind("EmployeeId") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="txtname" runat="server" Text='<%# Bind("Employee_Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtname1" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%# Bind("Employee_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="contact">
<EditItemTemplate>
<asp:TextBox ID="txtcontact" runat="server" Text='<%# Bind("Employee_ContactNo") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtcontact1" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblcontact" runat="server" Text='<%# Bind("Employee_ContactNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<EditItemTemplate>
<asp:TextBox ID="txtcity" runat="server" Text='<%# Bind("Employee_City") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtcity1" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblcity" runat="server" Text='<%# Bind("Employee_City") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<EditItemTemplate>
<asp:TextBox ID="txtsalary" runat="server" Text='<%# Bind("Employee_Salary") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtsalary1" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblsalary" runat="server" Text='<%# Bind("Employee_Salary") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender">
<EditItemTemplate>
<asp:TextBox ID="txtgender" runat="server" Text='<%# Bind("Employee_Gender") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtgender1" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblgender" runat="server" Text='<%# Bind("Employee_Gender") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Nationality">
<EditItemTemplate>
<asp:TextBox ID="txtnat" runat="server" Text='<%# Bind("Employee_Nationality") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtnat1" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblnat" runat="server" Text='<%# Bind("Employee_Nationality") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image" >
<ItemTemplate >
<asp:Image ID="img" runat="server" Width="150px" Height="150" ImageUrl='<%# Eval("image") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="AddNew" Text="Add New"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />
</Columns>
</asp:GridView>
</div>
Write
the below code in aspx.cs paGE
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
namespace CURD_Demo
{
public partial class Details : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter sda = new SqlDataAdapter();
DataTable dt = new DataTable();
DataSet ds1 = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
try
{
con.ConnectionString = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=C:\\Users\\puja.s\\Desktop\\Asp
.net Practice\\CURD_Demo\\CURD_Demo\\App_Data\\Database1.mdf;";
con.Open();
if (!this.IsPostBack)
{
BindGridView1();//showing details of employee
BindDataList1();
}
}
catch (Exception)
{
Response.Write("SERVER PROBLEM");
}
}
protected void BindGridView1()
{
cmd.CommandText = "SELECT Employee.EmployeeId,Employee.Employee_Name,
Employee.Employee_ContactNo,
Employee.Employee_Salary,Employee.Employee_Gender,Employee.Employee_City,Employee.Employee_Nationality,Image.Image
FROM Employee INNER JOIN Image ON Employee.Image_Id = Image.Image_Id ";
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_Edit(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex =
e.NewEditIndex;
BindGridView1();
}
protected void Gridview1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtname");
TextBox txtContact = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtcontact");
TextBox txtCity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtcity");
TextBox txtSalary = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtsalary");
TextBox txtGender = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtgender");
TextBox txtNat = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtnat");
Label lblid = (Label)GridView1.Rows[e.RowIndex].FindControl("lbleditid");
sda = new SqlDataAdapter("UPDATE EMPLOYEE SET Employee_Name='" + txtName.Text + "',Employee_ContactNo
='" + txtContact.Text + "',Employee_Salary='" + txtSalary.Text + "',Employee_Gender='" + txtGender.Text + "',Employee_City='" + txtCity.Text + "',Employee_Nationality='" + txtNat.Text + "'
WHERE EmployeeId='" + Convert.ToInt32(lblid.Text)
+ "'", con);
dt = new DataTable();
sda.Fill(dt);
GridView1.EditIndex = -1;
BindGridView1();
}
protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
if(e.CommandName.Equals("AddNew"))
{
TextBox txtName = (TextBox)GridView1.FooterRow.FindControl("txtname1");
TextBox txtContact = (TextBox)GridView1.FooterRow.FindControl("txtcontact1");
TextBox txtCity = (TextBox)GridView1.FooterRow.FindControl("txtcity1");
TextBox txtSalary = (TextBox)GridView1.FooterRow.FindControl("txtsalary1");
TextBox txtGender = (TextBox)GridView1.FooterRow.FindControl("txtgender1");
TextBox txtNat = (TextBox)GridView1.FooterRow.FindControl("txtnat1");
sda = new SqlDataAdapter("INSERT INTO
EMPLOYEE(Employee_Name,Employee_ContactNo,Employee_Salary,Employee_Gender,Employee_City,Employee_Nationality)
VALUES('" + txtName.Text + "'," +
txtContact.Text + "," + txtSalary.Text + ",'" + txtGender.Text + "','" + txtCity.Text + "','" + txtNat.Text + "')",con);
dt = new DataTable();
sda.Fill(dt);
BindGridView1();
}
}
protected void GridView1_RowDeleting(object sender,GridViewDeleteEventArgs e)
{
sda=new SqlDataAdapter("Delete from Employee
where EmployeeId="+Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lblid")).Text)+"",con);
dt = new DataTable();
sda.Fill(dt);
BindGridView1();
}
protected void GridView1_Cancel(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGridView1();
}
}}
Comments
Post a Comment