<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebProductsDemo.aspx.cs"
Inherits="TestDemo.WebProductsDemo" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v12.1, Version=12.1.8.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v12.1, Version=12.1.8.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dx" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div style="margin: 50px;">
<dx:ASPxGridView ID="ASPxGridView1" runat="server" OnRowUpdating="ASPxGridView1_RowUpdating"
KeyFieldName="ID" OnRowDeleting="ASPxGridView1_RowDeleting" OnRowInserting="ASPxGridView1_RowInserting"
OnRowValidating="ASPxGridView1_RowValidating" OnCustomUnboundColumnData="ASPxGridView1_CustomUnboundColumnData"
Width="900px">
<Columns>
<dx:GridViewCommandColumn>
<EditButton Visible="true" />
<DeleteButton Visible="true" />
<HeaderTemplate>
<dx:ASPxButton ID="AddProducts" runat="server" Text="New" OnClick="AddProducts_Click">
</dx:ASPxButton>
</HeaderTemplate>
</dx:GridViewCommandColumn>
<dx:GridViewDataColumn FieldName="ID" Visible="false" />
<dx:GridViewDataComboBoxColumn FieldName="HOTELID">
<PropertiesComboBox>
</PropertiesComboBox>
</dx:GridViewDataComboBoxColumn>
<dx:GridViewDataComboBoxColumn FieldName="PRODUCTTYPEID">
<PropertiesComboBox>
</PropertiesComboBox>
</dx:GridViewDataComboBoxColumn>
<dx:GridViewDataColumn FieldName="PRODUCTCODE" />
<dx:GridViewDataColumn FieldName="DESCRIPTION" />
<dx:GridViewDataColumn FieldName="PRICE" />
<dx:GridViewDataColumn FieldName="PRICEISFIXED" />
<dx:GridViewDataColumn FieldName="CREATED" />
<dx:GridViewDataColumn FieldName="CREATED_BY" />
<dx:GridViewDataColumn FieldName="UPDATED" />
<dx:GridViewDataColumn FieldName="UPDATED_BY" />
<dx:GridViewDataTextColumn FieldName="Total" UnboundType="Decimal">
<FooterCellStyle ForeColor="Brown" />
<PropertiesTextEdit DisplayFormatString="c" />
</dx:GridViewDataTextColumn>
</Columns>
<Settings ShowFooter="true" />
<TotalSummary>
<dx:ASPxSummaryItem FieldName="Total" SummaryType="Sum" />
</TotalSummary>
</dx:ASPxGridView>
</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 DevExpress.Web.ASPxGridView;
using System.Data;
namespace TestDemo
{
public partial class WebProductsDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindAll();
}
private DBHelper db = new DBHelper();
protected void AddProducts_Click(object sender, EventArgs e)
{
ASPxGridView1.AddNewRow();
}
public void BindAll()
{
BindHotel();
BindProductType();
Bind();
}
public void Bind()
{
string sql = @"select a.id,a.hotelid,a.producttypeid,
a.productcode,
a.description,
a.price,
a.priceisfixed,
a.created,
a.created_by,
a.updated,
a.updated_by,b.name as HotelName,c.description
from web_products a inner join hotels b on a.hotelid=b.id inner join
web_producttypes c on a.producttypeid=c.id";
this.ASPxGridView1.DataSource = db.GetDataTable(sql);
this.ASPxGridView1.DataBind();
}
public void BindHotel()
{
GridViewDataComboBoxColumn col = ASPxGridView1.Columns["HOTELID"] as GridViewDataComboBoxColumn;
string sql = "select id,name from hotels";
col.PropertiesComboBox.DataSource = db.GetDataTable(sql);
col.PropertiesComboBox.ValueField = "ID";
col.PropertiesComboBox.TextField = "NAME";
}
public void BindProductType()
{
GridViewDataComboBoxColumn col = ASPxGridView1.Columns["PRODUCTTYPEID"] as GridViewDataComboBoxColumn;
string sql = "select id,description from web_producttypes";
col.PropertiesComboBox.DataSource = db.GetDataTable(sql);
col.PropertiesComboBox.ValueField = "ID";
col.PropertiesComboBox.TextField = "DESCRIPTION";
}
protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
{
int selectID = Convert.ToInt32(e.Keys[0].ToString());
int hotleId = Convert.ToInt32(e.NewValues[0].ToString());
int productTypeId = Convert.ToInt32(e.NewValues[1].ToString());
string productCode = e.NewValues[2].ToString();
string description = e.NewValues[3].ToString();
string price = e.NewValues[4].ToString();
string pricefixed = e.NewValues[5].ToString();
string created = e.NewValues[6] == null ? DateTime.Now.ToString() : e.NewValues[6].ToString();
string createdBy = e.NewValues[7] == null ? "" : e.NewValues[7].ToString();
string update = e.NewValues[8] == null ? DateTime.Now.ToString() : e.NewValues[8].ToString();
string updatedBy = e.NewValues[9] == null ? "" : e.NewValues[9].ToString();
string sqlUpdate = string.Format(@"update web_products set
hotelid = {0},
producttypeid = {1},
productcode = '{2}',
description = '{3}',
price = '{4}',
priceisfixed = {5},
created = to_date('{6}','yyyy-MM-dd HH24:mi:ss'),
created_by = '{7}',
updated = to_date('{8}','yyyy-MM-dd HH24:mi:ss'),
updated_by = '{9}'
where id = {10}", hotleId, productTypeId, productCode, description, price, pricefixed, created, createdBy, update, updatedBy, selectID);
if (db.ExecuteSql(sqlUpdate))
{
this.ASPxGridView1.CancelEdit();
e.Cancel = true;
BindAll();
}
}
protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
{
int hotleId = Convert.ToInt32(e.NewValues[0]);
int productTypeId = Convert.ToInt32(e.NewValues[1]);
string productCode = e.NewValues[2].ToString();
string description = e.NewValues[3].ToString();
string price = e.NewValues[4].ToString();
string pricefixed = e.NewValues[5].ToString();
string created = e.NewValues[6] == null ? DateTime.Now.ToString() : e.NewValues[6].ToString();
string createdBy = e.NewValues[7] == null ? "" : e.NewValues[7].ToString();
string update = e.NewValues[8] == null ? DateTime.Now.ToString() : e.NewValues[8].ToString();
string updatedBy = e.NewValues[9] == null ? "" : e.NewValues[9].ToString();
string sqlInsert = string.Format(@"insert into web_products
(
id,
hotelid,
producttypeid,
productcode,
description,
price,
priceisfixed,
created,
created_by,
updated,
updated_by)
values
(SEQWEB_PRODUCTSID.NEXTVAL,{0},{1},'{2}','{3}','{4}',{5},to_date('{6}','yyyy-MM-dd HH24:mi:ss'),'{7}',to_date('{8}','yyyy-MM-dd HH24:mi:ss'),'{9}')", hotleId, productTypeId, productCode, description, price, pricefixed, created, createdBy, update, updatedBy);
if (db.ExecuteSql(sqlInsert))
{
this.ASPxGridView1.CancelEdit();
e.Cancel = true;
BindAll();
}
}
protected void ASPxGridView1_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
{
int selected = Convert.ToInt32(e.Keys[0].ToString());
string sqlDelete = string.Format("delete web_products where id={0}", selected);
if (db.ExecuteSql(sqlDelete))
{
e.Cancel = true;
BindAll();
}
}
protected void ASPxGridView1_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
{
foreach (GridViewColumn column in this.ASPxGridView1.Columns)
{
GridViewDataColumn dataColumn = column as GridViewDataColumn;
if (dataColumn == null)
continue;
if (e.NewValues["PRODUCTCODE"] == null)
{
AddError(e.Errors, this.ASPxGridView1.Columns["PRODUCTCODE"], "PRODUCTCODE should not be null");
}
}
}
public void AddError(Dictionary<GridViewColumn, string> errors, GridViewColumn column, string errorText)
{
if (errors.ContainsKey(column))
return;
errors[column] = errorText;
}
protected void ASPxGridView1_CustomUnboundColumnData(object sender, ASPxGridViewColumnDataEventArgs e)
{
if (e.Column.FieldName == "Total")
{
decimal price = (decimal)e.GetListSourceFieldValue("PRICE");
e.Value = price * 10;
}
}
}
}