<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DatatGridView.aspx.cs" Inherits="DatatGridView" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Insert Update Delete on GridView | code30s.com</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating" BackColor="#CACBE1"
ShowFooter="true" onrowcommand="GridView1_RowCommand"
>
<HeaderStyle BackColor="#006699" Font-Bold="True" Font-Names="cambria" ForeColor="White" />
<RowStyle Font-Names="Calibri" />
<Columns>
<asp:TemplateField HeaderText="MaNhanVien" HeaderStyle-Width="100px">
<ItemTemplate >
<asp:Label ID="lblMaNhanVien" runat="server" Text='<%# Eval("MaNhanVien") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtThemMaNhanVien" runat="server"></asp:TextBox>
</FooterTemplate>
<HeaderStyle Width="100px"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="TenNhanVien" HeaderStyle-Width="100px">
<EditItemTemplate>
<asp:TextBox ID="txtTenNhanVien" runat="server" Text='<%# Eval("TenNhanVien") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate >
<asp:Label ID="lblTenNhanVien" runat="server" Text='<%# Eval("TenNhanVien") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtThemTenNhanVien" runat="server"></asp:TextBox>
</FooterTemplate>
<HeaderStyle Width="100px"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Công việc" HeaderStyle-Width="100px">
<EditItemTemplate>
<asp:TextBox ID="txtCongViec" runat="server" Text='<%# Eval("CongViec") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate >
<asp:Label ID="lblCongViec" runat="server" Text='<%# Eval("CongViec") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtThemCongViec" runat="server"></asp:TextBox>
</FooterTemplate>
<HeaderStyle Width="100px"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mã Quốc gia" HeaderStyle-Width="100px">
<EditItemTemplate>
<asp:TextBox ID="txtMaQuocGia" runat="server" Text='<%# Eval("MaQuocGia") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate >
<asp:Label ID="lblMaQuocGia" runat="server" Text='<%# Eval("MaQuocGia") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtThemMaQuocGia" runat="server"></asp:TextBox>
</FooterTemplate>
<HeaderStyle Width="100px"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" HeaderStyle-Width="150px">
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" CommandName="Update" runat="server" >Update</asp:LinkButton>
<asp:LinkButton ID="btnCancel" CommandName="Cancel" runat="server">Cancel</asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
<asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="btnAdd" runat="server" CommandName="ThemMoi">Add new</asp:LinkButton>
</FooterTemplate>
<HeaderStyle Width="150px"></HeaderStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class DatatGridView : System.Web.UI.Page
{
SqlConnection sqlConn = new SqlConnection(@"Data Source=BOMBAY;Initial Catalog=bombay;Integrated Security=SSPI");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DocDuLieu();
}
}
private void DocDuLieu()
{
sqlConn.Open();
DataTable dataTable = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT MaNhanVien,TenNhanVien, MaQuocGia, CongViec, Convert(varchar,NgaySinh,103) [NgaySinh] FROM NhanVien", sqlConn);
adapter.Fill(dataTable);
GridView1.DataSource = dataTable;
GridView1.DataBind();
sqlConn.Close();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
DocDuLieu();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string maNhanVien = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblMaNhanVien")).Text;
string commandText = "DELETE NhanVien WHERE MaNhanVien=@MaNV";
SqlCommand command = new SqlCommand(commandText);
command.Parameters.AddWithValue("@MaNV", maNhanVien);
sqlConn.Open();
command.Connection = sqlConn;
command.ExecuteNonQuery();
sqlConn.Close();
GridView1.EditIndex = -1;
DocDuLieu();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string maNhanVien = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblMaNhanVien")).Text;
string tenNhanVien = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTenNhanVien")).Text;
string congViec = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCongViec")).Text;
string maQuocGia = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtMaQuocGia")).Text;
string commandText = "UPDATE NhanVien SET TenNhanVien=@TenNV,CongViec=@CongViec,MaQuocGia=@MaQuocGia WHERE MaNhanVien=@MaNV";
SqlCommand command = new SqlCommand(commandText);
command.Parameters.AddWithValue("@TenNV", tenNhanVien);
command.Parameters.AddWithValue("@CongViec", congViec);
command.Parameters.AddWithValue("@MaQuocGia", maQuocGia);
command.Parameters.AddWithValue("@MaNV", maNhanVien);
sqlConn.Open();
command.Connection = sqlConn;
command.ExecuteNonQuery();
sqlConn.Close();
GridView1.EditIndex = -1;
DocDuLieu();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
DocDuLieu();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string commandText = string.Empty;
string maNhanVien = ((TextBox)GridView1.FooterRow.FindControl("txtThemMaNhanVien")).Text;
string tenNhanVien = ((TextBox)GridView1.FooterRow.FindControl("txtThemTenNhanVien")).Text;
string congViec = ((TextBox)GridView1.FooterRow.FindControl("txtThemCongViec")).Text;
string maQuocGia = ((TextBox)GridView1.FooterRow.FindControl("txtThemMaQuocGia")).Text;
if (string.IsNullOrWhiteSpace(maNhanVien)
&& string.IsNullOrWhiteSpace(tenNhanVien)
&& string.IsNullOrWhiteSpace(congViec)
&& string.IsNullOrWhiteSpace(maQuocGia))
{
Response.Write("<h3>Nhập thông tin dòng muốn thêm !</h3>");
return;
}
if (e.CommandName.Equals("ThemMoi"))
{
commandText = "INSERT INTO NhanVien(MaNhanVien,TenNhanVien,CongViec,MaQuocGia) VALUES(@MaNV,@TenNV,@CongViec,@MaQuocGia)";
SqlCommand command = new SqlCommand(commandText);
command.Parameters.AddWithValue("@MaNV", maNhanVien);
command.Parameters.AddWithValue("@TenNV", tenNhanVien);
command.Parameters.AddWithValue("@CongViec", congViec);
command.Parameters.AddWithValue("@MaQuocGia", maQuocGia);
sqlConn.Open();
command.Connection = sqlConn;
command.ExecuteNonQuery();
sqlConn.Close();
GridView1.EditIndex = -1;
DocDuLieu();
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.