Grid.aspx==>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" ShowFooter="True" >
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblTestId" runat="server" Text='<%# Eval("TestId")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTestId" runat="server" Text='<%# Eval("TestId")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="1">ABC</asp:ListItem>
<asp:ListItem Value="2">BCD</asp:ListItem>
<asp:ListItem Value="3">CDE</asp:ListItem>
</asp:DropDownList>
<%--<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name")%>'></asp:TextBox>--%>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftTestname" runat="server" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Temp">
<ItemTemplate>
<asp:Label ID="lblTemp" runat="server" Text='<%# Eval("Temp")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTemp" runat="server" Text='<%# Eval("Temp")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftTestTemp" runat="server" Text='<%# Eval("TestId")%>'></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="hlEdit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
<asp:LinkButton ID="hlDelete" runat="server" CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="hlUpdate" runat="server" CommandName="Update">Update</asp:LinkButton>
<asp:LinkButton ID="hlCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="hlInsert" runat="server" CommandName="Insert">Insert</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#FF6600" BorderColor="White" BorderStyle="Inset" />
</asp:GridView>
Grid.aspx.cs==>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using DataModel;
using System.Data;
namespace Grid
{
public partial class GridEx : System.Web.UI.Page
{
BL bl = new BL();
DM dm = new DM();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
public void BindGrid()
{
DataSet ds = new DataSet();
ds = bl.ShowAlldata();
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
TextBox txtName = (TextBox)row.FindControl("txtftTestname");
TextBox ddlTemp = (TextBox)row.FindControl("txtftTestTemp");
dm.Name = txtName.Text;
dm.Tepm = ddlTemp.Text;
int i = bl.Insert(dm);
GridView1.EditIndex = -1;
BindGrid();
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtTestId = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTestId");
DropDownList txtName = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1");
TextBox txtTemp = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTemp");
dm.TestId = Convert.ToInt32(txtTestId.Text);
dm.Name = txtName.SelectedItem.Text;
dm.Tepm = txtTemp.Text;
int i = bl.Update(dm);
GridView1.EditIndex = -1;
BindGrid();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label lblTestId = (Label)GridView1.Rows[e.RowIndex].FindControl("lblTestId");
dm.TestId = Convert.ToInt32(lblTestId.Text);
int i = bl.Delete(dm);
BindGrid();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int ProductPrice = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Temp"));
if (ProductPrice > 400)
{
e.Row.ForeColor = System.Drawing.Color.Crimson;
e.Row.Font.Italic = true;
e.Row.BackColor = System.Drawing.Color.Aqua;
}
if (ProductPrice < 400)
{
e.Row.ForeColor = System.Drawing.Color.Crimson;
e.Row.Font.Italic = true;
e.Row.BackColor = System.Drawing.Color.LightYellow;
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
BindGrid();
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
}
}
BusinessLogic==>
with in the namespace
public class BL
{
DA da = new DA();
public DataSet ShowAlldata(){
DataSet ds = new DataSet();
ds = da.ShowTest();
return ds;
}
public int Insert(DM dm)
{
int i = da.Insert(dm);
return i;
}
public int Update(DM dm)
{
int i = da.Update(dm);
return i;
}
public int Delete(DM dm)
{
int i = da.Delete(dm);
return i;
}
}
Data Access==>
with in the namespace
public class DA
{
DM dm = new DM();
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["LocalConnectionString1"].ToString());
public DataSet ShowTest()
{
SqlCommand cmd = new SqlCommand("spTest",conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] param = { new SqlParameter("@Type",SqlDbType.VarChar,50)
//new SqlParameter("@Type",SqlDbType.VarChar,50)
};
param[0].Value = "select";
cmd.Parameters.Add(param[0]);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "Table1");
return ds;
//conn.Open();
//SqlDataReader dr = new SqlDataReader();
//dr = cmd.ExecuteReader();
}
public int Insert(DM dm)
{
try
{
SqlCommand cmd = new SqlCommand("spTest", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] param ={ new SqlParameter("@Name",SqlDbType.VarChar,50),
new SqlParameter("@Temp",SqlDbType.VarChar,50),
new SqlParameter("@Type",SqlDbType.VarChar,50)
};
param[0].Value = dm.Name;
param[1].Value = dm.Tepm;
param[2].Value = "insert";
cmd.Parameters.Add(param[0]);
cmd.Parameters.Add(param[1]);
cmd.Parameters.Add(param[2]);
conn.Open();
int i = cmd.ExecuteNonQuery();
conn.Close();
return i;
}
catch (Exception ex)
{
throw ex;
}
}
public int Update(DM dm)
{
try
{
SqlCommand cmd = new SqlCommand("spTest", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] param ={ new SqlParameter("@TestId",SqlDbType.Int),
new SqlParameter("@Name",SqlDbType.VarChar,50),
new SqlParameter("@Temp",SqlDbType.VarChar,50),
new SqlParameter("@Type",SqlDbType.VarChar,50)
};
param[0].Value = dm.TestId;
param[1].Value = dm.Name;
param[2].Value = dm.Tepm;
param[3].Value = "update";
cmd.Parameters.Add(param[0]);
cmd.Parameters.Add(param[1]);
cmd.Parameters.Add(param[2]);
cmd.Parameters.Add(param[3]);
conn.Open();
int i = cmd.ExecuteNonQuery();
conn.Close();
return i;
}
catch (Exception ex)
{
throw ex;
}
}
public int Delete(DM dm)
{
try
{
SqlCommand cmd = new SqlCommand("spTest", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] param ={ new SqlParameter("@TestId",SqlDbType.Int),
new SqlParameter("@Type",SqlDbType.VarChar,50)
};
param[0].Value = dm.TestId;
param[1].Value = "delete";
cmd.Parameters.Add(param[0]);
cmd.Parameters.Add(param[1]);
conn.Open();
int i = cmd.ExecuteNonQuery();
conn.Close();
return i;
}
catch (Exception ex)
{
throw ex;
}
}
}
Data model==>
with in the namespace
public class DM
{
int _testId;
public int TestId
{
get { return _testId; }
set { _testId = value; }
}
string _name, _temp;
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Tepm
{
get { return _temp; }
set { _temp = value; }
}
}
No comments:
Post a Comment