In Default.aspx Page
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
<!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>Editable Grid view</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<table>
<tr><td>
<asp:GridView ID=”grdContact” runat=”server” AutoGenerateColumns=”False” DataKeyNames=”Id, Type” OnRowCancelingEdit=”grdContact_RowCancelingEdit” OnRowDataBound=”grdContact_RowDataBound” OnRowEditing=”grdContact_RowEditing” OnRowUpdating=”grdContact_RowUpdating” OnRowCommand=”grdContact_RowCommand” ShowFooter=”True” OnRowDeleting=”grdContact_RowDeleting”>
<Columns>
<asp:TemplateField HeaderText=”ID” HeaderStyle-HorizontalAlign=”Left”>
<EditItemTemplate>
<asp:Label ID=”lblId” runat=”server” Text=’<%# Bind(“Id”) %>’></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”lblId” runat=”server” Text=’<%# Bind(“Id”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Name” HeaderStyle-HorizontalAlign=”Left”>
<EditItemTemplate>
<asp:TextBox ID=”txtName” runat=”server” Text=’<%# Bind(“Name”) %>’></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID=”txtNewName” runat=”server” ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID=”lblName” runat=”server” Text=’<%# Bind(“Name”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Sex” HeaderStyle-HorizontalAlign=”Left”>
<EditItemTemplate>
<asp:DropDownList ID=”ddlSex” runat=”server” SelectedValue=’<%# Eval(“Sex”) %>’>
<asp:ListItem Value=”Male” Text=”Male”></asp:ListItem>
<asp:ListItem Value=”Female” Text=”Female”></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”lblSex” runat=”server” Text=’<%# Eval(“Sex”) %>’></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID=”ddlNewSex” runat=”server” >
<asp:ListItem Text=”Male” Value=”Male” Selected=”True”></asp:ListItem>
<asp:ListItem Text=”Female” Value=”Female”></asp:ListItem> </asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Type” HeaderStyle-HorizontalAlign=”Left”>
<EditItemTemplate>
<asp:DropDownList ID=”cmbType” runat=”server” DataTextField=”Typename” DataValueField=”Id”> </asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”lblType” runat=”server” Text=’<%# Eval(“Type”) %>’></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID=”cmbNewType” runat=”server” DataTextField=”Typename” DataValueField=”Id”> </asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Ative”>
<EditItemTemplate>
<asp:CheckBox ID=”chkActive” runat=”server” />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”lblActive” runat=”server” Text=’<%# Eval(“IsActive”) %>’></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:CheckBox ID=”chkNewActive” runat=”server” />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Edit” ShowHeader=”False” HeaderStyle-HorizontalAlign=”Left”>
<EditItemTemplate>
<asp:LinkButton ID=”lbkUpdate” runat=”server” CausesValidation=”True” CommandName=”Update” Text=”Update”></asp:LinkButton>
<asp:LinkButton ID=”lnkCancel” runat=”server” CausesValidation=”False” CommandName=”Cancel” Text=”Cancel”></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID=”lnkAdd” runat=”server” CausesValidation=”False” CommandName=”Insert” Text=”Insert”></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID=”lnkEdit” runat=”server” CausesValidation=”False” CommandName=”Edit” Text=”Edit”></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText=”Delete” ShowDeleteButton=”True” ShowHeader=”True” />
</Columns>
</asp:GridView>
</td></tr>
</table>
</div>
</form>
</body>
</html>
—————————————————————–
In Default.aspx.cs File
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TestDatabaseTableAdapters;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillGrid();
}
}
public void FillGrid()
{
ContactTableAdapter contact = new ContactTableAdapter();
DataTable contacts = contact.GetData();
if (contacts.Rows.Count > 0)
{
grdContact.DataSource = contacts;
grdContact.DataBind();
}
else
{
contacts.Rows.Add(contacts.NewRow());
grdContact.DataSource = contacts;
grdContact.DataBind();
int TotalColumns = grdContact.Rows[0].Cells.Count;
grdContact.Rows[0].Cells.Clear();
grdContact.Rows[0].Cells.Add(new TableCell());
grdContact.Rows[0].Cells[0].ColumnSpan = TotalColumns;
grdContact.Rows[0].Cells[0].Text = “No Record Found”;
}
}
protected void grdContact_RowDataBound(object sender, GridViewRowEventArgs e)
{
ContactTypeTableAdapter contactType = new ContactTypeTableAdapter();
DataTable contactTypes = contactType.GetData();
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblType = (Label)e.Row.FindControl(“lblType”);
if (lblType != null)
{
int typeId = Convert.ToInt32(lblType.Text);
lblType.Text = (string)contactType.GetTypeById(typeId);
}
DropDownList cmbType = (DropDownList)e.Row.FindControl(“cmbType”);
if (cmbType != null)
{
cmbType.DataSource = contactTypes;
cmbType.DataTextField = “TypeName”;
cmbType.DataValueField = “Id”;
cmbType.DataBind();
cmbType.SelectedValue = grdContact.DataKeys[e.Row.RowIndex].Values[1].ToString();
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList cmbNewType = (DropDownList)e.Row.FindControl(“cmbNewType”);
cmbNewType.DataSource = contactTypes;
cmbNewType.DataBind();
}
}
protected void grdContact_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdContact.EditIndex = -1;
FillGrid();
}
protected void grdContact_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
ContactTableAdapter contact = new ContactTableAdapter();
bool flag = false;
Label lblId = (Label)grdContact.Rows[e.RowIndex].FindControl(“lblId”);
TextBox txtName = (TextBox)grdContact.Rows[e.RowIndex].FindControl(“txtName”);
CheckBox chkActive = (CheckBox)grdContact.Rows[e.RowIndex].FindControl(“chkActive”);
DropDownList cmbType = (DropDownList)grdContact.Rows[e.RowIndex].FindControl(“cmbType”);
DropDownList ddlSex = (DropDownList)grdContact.Rows[e.RowIndex].FindControl(“ddlSex”);
if (chkActive.Checked) flag = true; else flag = false;
contact.Update(txtName.Text, ddlSex.SelectedValue, cmbType.SelectedValue, flag,Convert.ToInt32(lblId.Text));
grdContact.EditIndex = -1;
FillGrid();
}
protected void grdContact_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
ContactTableAdapter contact = new ContactTableAdapter();
int id = Convert.ToInt32(grdContact.DataKeys[e.RowIndex].Values[0].ToString());
contact.Delete(id);
FillGrid();
}
protected void grdContact_RowCommand(object sender, GridViewCommandEventArgs e)
{
ContactTableAdapter contact = new ContactTableAdapter();
bool flag= false;
if (e.CommandName.Equals(“Insert”))
{
TextBox txtNewName = (TextBox)grdContact.FooterRow.FindControl(“txtNewName”);
CheckBox chkNewActive = (CheckBox)grdContact.FooterRow.FindControl(“chkNewActive”);
DropDownList cmbNewType = (DropDownList)grdContact.FooterRow.FindControl(“cmbNewType”);
DropDownList ddlNewSex = (DropDownList)grdContact.FooterRow.FindControl(“ddlNewSex”);
if (chkNewActive.Checked) flag = true; else flag = false;
contact.Insert(txtNewName.Text, ddlNewSex.SelectedValue, cmbNewType.SelectedValue, flag);
FillGrid();
}
}
protected void grdContact_RowEditing(object sender, GridViewEditEventArgs e)
{
grdContact.EditIndex = e.NewEditIndex;
FillGrid();
}
}
<Columns>
<asp:TemplateField HeaderText=”ID” HeaderStyle-HorizontalAlign=”Left”>
<EditItemTemplate>
<asp:Label ID=”lblId” runat=”server” Text=’<%# Bind(“Id”) %>’></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”lblId” runat=”server” Text=’<%# Bind(“Id”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Name” HeaderStyle-HorizontalAlign=”Left”>
<EditItemTemplate>
<asp:TextBox ID=”txtName” runat=”server” Text=’<%# Bind(“Name”) %>’></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID=”txtNewName” runat=”server” ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID=”lblName” runat=”server” Text=’<%# Bind(“Name”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Sex” HeaderStyle-HorizontalAlign=”Left”>
<EditItemTemplate>
<asp:DropDownList ID=”ddlSex” runat=”server” SelectedValue=’<%# Eval(“Sex”) %>’>
<asp:ListItem Value=”Male” Text=”Male”></asp:ListItem>
<asp:ListItem Value=”Female” Text=”Female”></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”lblSex” runat=”server” Text=’<%# Eval(“Sex”) %>’></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID=”ddlNewSex” runat=”server” >
<asp:ListItem Text=”Male” Value=”Male” Selected=”True”></asp:ListItem>
<asp:ListItem Text=”Female” Value=”Female”></asp:ListItem> </asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Type” HeaderStyle-HorizontalAlign=”Left”>
<EditItemTemplate>
<asp:DropDownList ID=”cmbType” runat=”server” DataTextField=”Typename” DataValueField=”Id”> </asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”lblType” runat=”server” Text=’<%# Eval(“Type”) %>’></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID=”cmbNewType” runat=”server” DataTextField=”Typename” DataValueField=”Id”> </asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Ative”>
<EditItemTemplate>
<asp:CheckBox ID=”chkActive” runat=”server” />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”lblActive” runat=”server” Text=’<%# Eval(“IsActive”) %>’></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:CheckBox ID=”chkNewActive” runat=”server” />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Edit” ShowHeader=”False” HeaderStyle-HorizontalAlign=”Left”>
<EditItemTemplate>
<asp:LinkButton ID=”lbkUpdate” runat=”server” CausesValidation=”True” CommandName=”Update” Text=”Update”></asp:LinkButton>
<asp:LinkButton ID=”lnkCancel” runat=”server” CausesValidation=”False” CommandName=”Cancel” Text=”Cancel”></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID=”lnkAdd” runat=”server” CausesValidation=”False” CommandName=”Insert” Text=”Insert”></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID=”lnkEdit” runat=”server” CausesValidation=”False” CommandName=”Edit” Text=”Edit”></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText=”Delete” ShowDeleteButton=”True” ShowHeader=”True” />
</Columns>
</asp:GridView>
</td></tr>
</table>
</div>
</form>
</body>
</html>
—————————————————————–
In Default.aspx.cs File
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TestDatabaseTableAdapters;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillGrid();
}
}
public void FillGrid()
{
ContactTableAdapter contact = new ContactTableAdapter();
DataTable contacts = contact.GetData();
if (contacts.Rows.Count > 0)
{
grdContact.DataSource = contacts;
grdContact.DataBind();
}
else
{
contacts.Rows.Add(contacts.NewRow());
grdContact.DataSource = contacts;
grdContact.DataBind();
int TotalColumns = grdContact.Rows[0].Cells.Count;
grdContact.Rows[0].Cells.Clear();
grdContact.Rows[0].Cells.Add(new TableCell());
grdContact.Rows[0].Cells[0].ColumnSpan = TotalColumns;
grdContact.Rows[0].Cells[0].Text = “No Record Found”;
}
}
protected void grdContact_RowDataBound(object sender, GridViewRowEventArgs e)
{
ContactTypeTableAdapter contactType = new ContactTypeTableAdapter();
DataTable contactTypes = contactType.GetData();
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblType = (Label)e.Row.FindControl(“lblType”);
if (lblType != null)
{
int typeId = Convert.ToInt32(lblType.Text);
lblType.Text = (string)contactType.GetTypeById(typeId);
}
DropDownList cmbType = (DropDownList)e.Row.FindControl(“cmbType”);
if (cmbType != null)
{
cmbType.DataSource = contactTypes;
cmbType.DataTextField = “TypeName”;
cmbType.DataValueField = “Id”;
cmbType.DataBind();
cmbType.SelectedValue = grdContact.DataKeys[e.Row.RowIndex].Values[1].ToString();
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList cmbNewType = (DropDownList)e.Row.FindControl(“cmbNewType”);
cmbNewType.DataSource = contactTypes;
cmbNewType.DataBind();
}
}
protected void grdContact_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdContact.EditIndex = -1;
FillGrid();
}
protected void grdContact_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
ContactTableAdapter contact = new ContactTableAdapter();
bool flag = false;
Label lblId = (Label)grdContact.Rows[e.RowIndex].FindControl(“lblId”);
TextBox txtName = (TextBox)grdContact.Rows[e.RowIndex].FindControl(“txtName”);
CheckBox chkActive = (CheckBox)grdContact.Rows[e.RowIndex].FindControl(“chkActive”);
DropDownList cmbType = (DropDownList)grdContact.Rows[e.RowIndex].FindControl(“cmbType”);
DropDownList ddlSex = (DropDownList)grdContact.Rows[e.RowIndex].FindControl(“ddlSex”);
if (chkActive.Checked) flag = true; else flag = false;
contact.Update(txtName.Text, ddlSex.SelectedValue, cmbType.SelectedValue, flag,Convert.ToInt32(lblId.Text));
grdContact.EditIndex = -1;
FillGrid();
}
protected void grdContact_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
ContactTableAdapter contact = new ContactTableAdapter();
int id = Convert.ToInt32(grdContact.DataKeys[e.RowIndex].Values[0].ToString());
contact.Delete(id);
FillGrid();
}
protected void grdContact_RowCommand(object sender, GridViewCommandEventArgs e)
{
ContactTableAdapter contact = new ContactTableAdapter();
bool flag= false;
if (e.CommandName.Equals(“Insert”))
{
TextBox txtNewName = (TextBox)grdContact.FooterRow.FindControl(“txtNewName”);
CheckBox chkNewActive = (CheckBox)grdContact.FooterRow.FindControl(“chkNewActive”);
DropDownList cmbNewType = (DropDownList)grdContact.FooterRow.FindControl(“cmbNewType”);
DropDownList ddlNewSex = (DropDownList)grdContact.FooterRow.FindControl(“ddlNewSex”);
if (chkNewActive.Checked) flag = true; else flag = false;
contact.Insert(txtNewName.Text, ddlNewSex.SelectedValue, cmbNewType.SelectedValue, flag);
FillGrid();
}
}
protected void grdContact_RowEditing(object sender, GridViewEditEventArgs e)
{
grdContact.EditIndex = e.NewEditIndex;
FillGrid();
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TestDatabaseTableAdapters;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillGrid();
}
}
public void FillGrid()
{
ContactTableAdapter contact = new ContactTableAdapter();
DataTable contacts = contact.GetData();
if (contacts.Rows.Count > 0)
{
grdContact.DataSource = contacts;
grdContact.DataBind();
}
else
{
contacts.Rows.Add(contacts.NewRow());
grdContact.DataSource = contacts;
grdContact.DataBind();
int TotalColumns = grdContact.Rows[0].Cells.Count;
grdContact.Rows[0].Cells.Clear();
grdContact.Rows[0].Cells.Add(new TableCell());
grdContact.Rows[0].Cells[0].ColumnSpan = TotalColumns;
grdContact.Rows[0].Cells[0].Text = “No Record Found”;
}
}
protected void grdContact_RowDataBound(object sender, GridViewRowEventArgs e)
{
ContactTypeTableAdapter contactType = new ContactTypeTableAdapter();
DataTable contactTypes = contactType.GetData();
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblType = (Label)e.Row.FindControl(“lblType”);
if (lblType != null)
{
int typeId = Convert.ToInt32(lblType.Text);
lblType.Text = (string)contactType.GetTypeById(typeId);
}
DropDownList cmbType = (DropDownList)e.Row.FindControl(“cmbType”);
if (cmbType != null)
{
cmbType.DataSource = contactTypes;
cmbType.DataTextField = “TypeName”;
cmbType.DataValueField = “Id”;
cmbType.DataBind();
cmbType.SelectedValue = grdContact.DataKeys[e.Row.RowIndex].Values[1].ToString();
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList cmbNewType = (DropDownList)e.Row.FindControl(“cmbNewType”);
cmbNewType.DataSource = contactTypes;
cmbNewType.DataBind();
}
}
protected void grdContact_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdContact.EditIndex = -1;
FillGrid();
}
protected void grdContact_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
ContactTableAdapter contact = new ContactTableAdapter();
bool flag = false;
Label lblId = (Label)grdContact.Rows[e.RowIndex].FindControl(“lblId”);
TextBox txtName = (TextBox)grdContact.Rows[e.RowIndex].FindControl(“txtName”);
CheckBox chkActive = (CheckBox)grdContact.Rows[e.RowIndex].FindControl(“chkActive”);
DropDownList cmbType = (DropDownList)grdContact.Rows[e.RowIndex].FindControl(“cmbType”);
DropDownList ddlSex = (DropDownList)grdContact.Rows[e.RowIndex].FindControl(“ddlSex”);
if (chkActive.Checked) flag = true; else flag = false;
contact.Update(txtName.Text, ddlSex.SelectedValue, cmbType.SelectedValue, flag,Convert.ToInt32(lblId.Text));
grdContact.EditIndex = -1;
FillGrid();
}
protected void grdContact_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
ContactTableAdapter contact = new ContactTableAdapter();
int id = Convert.ToInt32(grdContact.DataKeys[e.RowIndex].Values[0].ToString());
contact.Delete(id);
FillGrid();
}
protected void grdContact_RowCommand(object sender, GridViewCommandEventArgs e)
{
ContactTableAdapter contact = new ContactTableAdapter();
bool flag= false;
if (e.CommandName.Equals(“Insert”))
{
TextBox txtNewName = (TextBox)grdContact.FooterRow.FindControl(“txtNewName”);
CheckBox chkNewActive = (CheckBox)grdContact.FooterRow.FindControl(“chkNewActive”);
DropDownList cmbNewType = (DropDownList)grdContact.FooterRow.FindControl(“cmbNewType”);
DropDownList ddlNewSex = (DropDownList)grdContact.FooterRow.FindControl(“ddlNewSex”);
if (chkNewActive.Checked) flag = true; else flag = false;
contact.Insert(txtNewName.Text, ddlNewSex.SelectedValue, cmbNewType.SelectedValue, flag);
FillGrid();
}
}
protected void grdContact_RowEditing(object sender, GridViewEditEventArgs e)
{
grdContact.EditIndex = e.NewEditIndex;
FillGrid();
}
}
No comments:
Post a Comment