<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="BrainTree._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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%----%>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_OnRowDataBound"
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField HeaderText="Name" DataField="Location" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:DropDownList ID="ddlCountries" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCountries_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lbBuildingUpdate" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lbBuildingUpdateSave" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="lbBuildingUpdateCancel" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnAddNewCompany" runat="server" Text="Add New Company" OnClick="btnAddNewCompany_Click" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using System.Configuration;
namespace BrainTree
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//bindGrid();
GridView1.DataSource = GetData();
GridView1.DataBind();
}
}
protected void grdCompany_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
//protected void grdCompany_RowEditing(object sender, GridViewEditEventArgs e)
//{
// grdCompany.EditIndex = e.NewEditIndex;
// bindGrid();
//}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = GetData();
GridView1.DataBind();
}
//protected void grdCompany_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
//{
// grdCompany.EditIndex = -1;
// bindGrid();
//}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataSource = GetData();
GridView1.DataBind();
}
protected void grdCompany_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dtOriginalCompany = new DataTable();
DataRow drRowtoAddBRC = dtOriginalCompany.NewRow();
drRowtoAddBRC["CompanyName"] = "Barclays";
drRowtoAddBRC["CompanyID"] = "BRC";
drRowtoAddBRC["CompanyDesc"] = "Barclays Pvt Ltd";
dtOriginalCompany.Rows.Add(drRowtoAddBRC);
DataRow drRowtoAddACC = dtOriginalCompany.NewRow();
drRowtoAddACC["CompanyName"] = "Accenture";
drRowtoAddACC["CompanyID"] = "ACC";
drRowtoAddACC["CompanyDesc"] = "Accenture Pvt Ltd.";
dtOriginalCompany.Rows.Add(drRowtoAddACC);
DataRow drRowtoAddCRSUI = dtOriginalCompany.NewRow();
drRowtoAddCRSUI["CompanyName"] = "Credit Suisse";
drRowtoAddCRSUI["CompanyID"] = "CRSUI";
drRowtoAddCRSUI["CompanyDesc"] = "United Kingdom";
dtOriginalCompany.Rows.Add(drRowtoAddCRSUI);
DataRow drRowtoAddDLL = dtOriginalCompany.NewRow();
drRowtoAddDLL["CompanyName"] = "Delloite";
drRowtoAddDLL["CompanyID"] = "DLL";
drRowtoAddDLL["CompanyDesc"] = "Delloite";
dtOriginalCompany.Rows.Add(drRowtoAddDLL);
DataRow drRowtoAddGLT = dtOriginalCompany.NewRow();
drRowtoAddGLT["CompanyName"] = "HSBC";
drRowtoAddGLT["CompanyID"] = "GLT";
drRowtoAddGLT["CompanyDesc"] = "HSBC Pvt Ltd.";
dtOriginalCompany.Rows.Add(drRowtoAddGLT);
ViewState["dtOriginalCompany"] = dtOriginalCompany;
DataTable dtCurrentCompany = new DataTable();
DataTable dtOldCompany = new DataTable();
dtCurrentCompany = ViewState["dtOriginalCompany"] as DataTable;
dtOldCompany = ViewState["dtOriginalCompany"] as DataTable;
dtOldCompany.Rows[1].Delete();
ViewState["dtOriginalCompany"] = dtOldCompany;
}
protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dtCompany = new DataTable("Location");
dtCompany.Columns.Add(new DataColumn("Location", System.Type.GetType("System.String")));
DataRow drRowtoAddBRC = dtCompany.NewRow();
drRowtoAddBRC["Location"] = "India";
dtCompany.Rows.Add(drRowtoAddBRC);
DataRow drRowtoAddACC = dtCompany.NewRow();
drRowtoAddACC["Location"] = "United Kingdom";
dtCompany.Rows.Add(drRowtoAddACC);
DataRow drRowtoAddCRSUI = dtCompany.NewRow();
drRowtoAddCRSUI["Location"] = "United States";
dtCompany.Rows.Add(drRowtoAddCRSUI);
DataRow drRowtoAddDLL = dtCompany.NewRow();
drRowtoAddDLL["Location"] = "Australia";
dtCompany.Rows.Add(drRowtoAddDLL);
DataRow drRowtoAddGLT = dtCompany.NewRow();
drRowtoAddGLT["Location"] = "Canada";
dtCompany.Rows.Add(drRowtoAddGLT);
ViewState["dtOriginalCompany"] = dtCompany;
DataTable dtCurrentCompany = new DataTable();
DataTable dtOldCompany = new DataTable();
dtCurrentCompany = ViewState["dtOriginalCompany"] as DataTable;
dtOldCompany = ViewState["dtOriginalCompany"] as DataTable;
dtOldCompany.Rows[1].Delete();
ViewState["dtOriginalCompany"] = dtOldCompany;
}
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlCompany = (e.Row.FindControl("ddlCountries") as DropDownList);
if (ddlCompany != null)
{
ddlCompany.Items.Add(new ListItem("--Select Location--", ""));
ddlCompany.Items.Add(new ListItem("Pune", "PUNE"));
ddlCompany.Items.Add(new ListItem("Bangalore", "BGL"));
ddlCompany.Items.Add(new ListItem("Chennai", "CHN"));
ddlCompany.Items.Add(new ListItem("Hyderabad", "HYD"));
ddlCompany.Items.Add(new ListItem("Mumbai", "MUM"));
ddlCompany.DataTextField = "CityName";
ddlCompany.DataValueField = "CityId";
ddlCompany.DataBind();
}
}
}
protected void btnAddNewCompany_Click(object sender, EventArgs e)
{
DataTable dtOriginalCompany = new DataTable("CompanyData");
dtOriginalCompany.Columns.Add(new DataColumn("CompanyName", System.Type.GetType("System.String")));
dtOriginalCompany.Columns.Add(new DataColumn("CompanyID", System.Type.GetType("System.String")));
dtOriginalCompany.Columns.Add(new DataColumn("CompanyDesc", System.Type.GetType("System.String")));
DataRow drRowtoAddBRC = dtOriginalCompany.NewRow();
drRowtoAddBRC["CompanyName"] = "Barclays";
drRowtoAddBRC["CompanyID"] = "BRC";
drRowtoAddBRC["CompanyDesc"] = "Barclays Pvt Ltd";
dtOriginalCompany.Rows.Add(drRowtoAddBRC);
DataRow drRowtoAddACC = dtOriginalCompany.NewRow();
drRowtoAddACC["CompanyName"] = "Accenture";
drRowtoAddACC["CompanyID"] = "ACC";
drRowtoAddACC["CompanyDesc"] = "Accenture Pvt Ltd.";
dtOriginalCompany.Rows.Add(drRowtoAddACC);
DataRow drRowtoAddCRSUI = dtOriginalCompany.NewRow();
drRowtoAddCRSUI["CompanyName"] = "Credit Suisse";
drRowtoAddCRSUI["CompanyID"] = "CRSUI";
drRowtoAddCRSUI["CompanyDesc"] = "United Kingdom";
dtOriginalCompany.Rows.Add(drRowtoAddCRSUI);
DataRow drRowtoAddDLL = dtOriginalCompany.NewRow();
drRowtoAddDLL["CompanyName"] = "Delloite";
drRowtoAddDLL["CompanyID"] = "DLL";
drRowtoAddDLL["CompanyDesc"] = "Delloite";
dtOriginalCompany.Rows.Add(drRowtoAddDLL);
DataRow drRowtoAddGLT = dtOriginalCompany.NewRow();
drRowtoAddGLT["CompanyName"] = "HSBC";
drRowtoAddGLT["CompanyID"] = "GLT";
drRowtoAddGLT["CompanyDesc"] = "HSBC Pvt Ltd.";
dtOriginalCompany.Rows.Add(drRowtoAddGLT);
ViewState["dtOriginalCompany"] = dtOriginalCompany;
DataTable dtCurrentCompany = new DataTable();
DataTable dtOldCompany = new DataTable();
dtCurrentCompany = ViewState["dtOriginalCompany"] as DataTable;
dtOldCompany = ViewState["dtOriginalCompany"] as DataTable;
dtOldCompany.Rows[1].Delete();
ViewState["dtOldCompany"] = dtOldCompany;
}
private void bindGrid()
{
DataTable dtCompany = new DataTable("CompanyData");
dtCompany.Columns.Add(new DataColumn("CompanyName", System.Type.GetType("System.String")));
dtCompany.Columns.Add(new DataColumn("CompanyID", System.Type.GetType("System.String")));
dtCompany.Columns.Add(new DataColumn("CompanyDesc", System.Type.GetType("System.String")));
DataRow drRowtoAddBRC = dtCompany.NewRow();
drRowtoAddBRC["CompanyName"] = "Barclays";
drRowtoAddBRC["CompanyID"] = "BRC";
drRowtoAddBRC["CompanyDesc"] = "Barclays Pvt Ltd";
dtCompany.Rows.Add(drRowtoAddBRC);
DataRow drRowtoAddACC = dtCompany.NewRow();
drRowtoAddACC["CompanyName"] = "Accenture";
drRowtoAddACC["CompanyID"] = "ACC";
drRowtoAddACC["CompanyDesc"] = "Accenture Pvt Ltd.";
dtCompany.Rows.Add(drRowtoAddACC);
DataRow drRowtoAddCRSUI = dtCompany.NewRow();
drRowtoAddCRSUI["CompanyName"] = "Credit Suisse";
drRowtoAddCRSUI["CompanyID"] = "CRSUI";
drRowtoAddCRSUI["CompanyDesc"] = "United Kingdom";
dtCompany.Rows.Add(drRowtoAddCRSUI);
DataRow drRowtoAddDLL = dtCompany.NewRow();
drRowtoAddDLL["CompanyName"] = "Delloite";
drRowtoAddDLL["CompanyID"] = "DLL";
drRowtoAddDLL["CompanyDesc"] = "Delloite";
dtCompany.Rows.Add(drRowtoAddDLL);
DataRow drRowtoAddGLT = dtCompany.NewRow();
drRowtoAddGLT["CompanyName"] = "HSBC";
drRowtoAddGLT["CompanyID"] = "GLT";
drRowtoAddGLT["CompanyDesc"] = "HSBC Pvt Ltd.";
dtCompany.Rows.Add(drRowtoAddGLT);
//grdCompany.DataSource = dtCompany;
//grdCompany.DataBind();
}
private DataTable GetData()
{
DataTable dtCompany = new DataTable("Location");
dtCompany.Columns.Add(new DataColumn("Location", System.Type.GetType("System.String")));
DataRow drRowtoAddBRC = dtCompany.NewRow();
drRowtoAddBRC["Location"] = "India";
dtCompany.Rows.Add(drRowtoAddBRC);
DataRow drRowtoAddACC = dtCompany.NewRow();
drRowtoAddACC["Location"] = "United Kingdom";
dtCompany.Rows.Add(drRowtoAddACC);
DataRow drRowtoAddCRSUI = dtCompany.NewRow();
drRowtoAddCRSUI["Location"] = "United States";
dtCompany.Rows.Add(drRowtoAddCRSUI);
DataRow drRowtoAddDLL = dtCompany.NewRow();
drRowtoAddDLL["Location"] = "Australia";
dtCompany.Rows.Add(drRowtoAddDLL);
DataRow drRowtoAddGLT = dtCompany.NewRow();
drRowtoAddGLT["Location"] = "Canada";
dtCompany.Rows.Add(drRowtoAddGLT);
return dtCompany;
}
}
}