Page.aspx
<asp:GridView ID="GridView1" runat="server" style="height:100px; overflow:auto" AutoGenerateColumns="False"
DataSourceID="sqldsEmpDetails" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
DataKeyNames="id" onrowdeleting="GridView1_RowDeleting"
onrowcommand="GridView1_RowCommand" onrowcreated="GridView1_RowCreated"
ShowHeader="False">
<Columns>
<asp:TemplateField HeaderText="Edit">
<EditItemTemplate>
<asp:LinkButton ID="lkbtnUpdate" runat="server" CommandName="Update"
CommandArgument='<%# Eval("Id") %>'>Update</asp:LinkButton>
<asp:LinkButton ID="lkbtnCancal" runat="server" CommandName="Cancel">Cancal</asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lkbtninsert" runat="server" CommandName="AddNew"
ValidationGroup="Insert">Insert</asp:LinkButton>
<asp:LinkButton ID="lkbtncancel" runat="server" onclick="lkbtncancel_Click">Cancel</asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="lkbtnEdit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
<asp:LinkButton ID="lkbtnDelete" runat="server"
CommandArgument='<%# Eval("id") %>' CommandName="Delete"
onclientclick="javascript:return DeleteItem();">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="txtname" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="*" ForeColor="Red"
ValidationGroup="Insert"></asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Addresse">
<EditItemTemplate>
<asp:TextBox ID="txtAddresse" runat="server" Text='<%# Eval("Addresse") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" ErrorMessage="*" ForeColor="Red"
ValidationGroup="Insert"></asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lbladresse" runat="server" Text='<%# Eval("Addresse") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cell">
<EditItemTemplate>
<asp:TextBox ID="txtcell" runat="server" Text='<%# Eval("Cell") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox3" ErrorMessage="*" ForeColor="Red"
ValidationGroup="Insert"></asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblcell" runat="server" Text='<%# Eval("Cell") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id" Visible="False">
<EditItemTemplate>
<asp:TextBox ID="txtid" runat="server" Text='<%# Eval("ID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="FixedHeader" />
</asp:GridView>
<asp:Button ID="btnnewrow" runat="server" onclick="btnnewrow_Click"
Text="Add New" />
<script type="text/javascript">
//conformation box for Delete
function DeleteItem() {
if (confirm("Are you sure you want to delete ...?")) {
return true;
}
return false;
}
</script>
Page.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Common;
using System.Data;
using MySql.Data.MySqlClient;
public partial class _Default : System.Web.UI.Page
{
MySqlConnection conn = new MySqlConnection();
MySqlCommand cmd = new MySqlCommand();
MySqlDataAdapter adp = new MySqlDataAdapter();
sendmail objmail = new sendmail();
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = string.Empty;
conn = new MySqlConnection("Server=localhost ;Database=emp ;Uid=root;Pwd=send;");
conn.Open();
Bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
LinkButton lk = (LinkButton)GridView1.Rows[e.RowIndex].FindControl("lkbtnUpdate");
string id = lk.CommandArgument.ToString();
// string iidd = Gvempdetails.DataKeys[e.RowIndex].Value["id"];
// int id = Convert.ToInt32(Gvempdetails.DataKeys[e.RowIndex].Item["youfield"].tostring());
// TextBox id = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtid");
TextBox name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtname");
TextBox Address = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAddresse");
TextBox cell = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtcell");
string SaveQuery = "UPDATE empdetails SET NAME='" + name.Text + "',addresse='" + Address.Text + "',Cell=" + cell.Text + " WHERE id=" + id + "";
//cmd = new MySqlCommand(SaveQuery, conn);
//cmd.ExecuteNonQuery();
sqldsEmpDetails.UpdateCommand = SaveQuery;
sqldsEmpDetails.Update();
GridView1.EditIndex = -1;
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string test = GridView1.DataKeys[e.RowIndex].Value.ToString();
LinkButton lk = (LinkButton)GridView1.Rows[e.RowIndex].FindControl("lkbtnDelete");
string id = lk.CommandArgument.ToString();
sqldsEmpDetails.DeleteCommand = "Delete From empdetails where id="+id+"";
sqldsEmpDetails.DataBind();
GridView1.DataBind();
}
protected void btnnewrow_Click(object sender, EventArgs e)
{
GridView1.ShowFooter = true;
}
protected void lkbtncancel_Click(object sender, EventArgs e)
{
GridView1.ShowFooter = false;
}
public void Bind()
{
DataSet ds= new DataSet();
adp = new MySqlDataAdapter("SELECT id,NAME,Addresse,Cell FROM empdetails ORDER BY NAME ASC", conn);
adp.Fill(ds, "c");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
No comments:
Post a Comment