初见,Devexpress

时间:2024-01-05 22:09:44

【绥江一百】http://www.sj100.net                                                  欢迎,进入绥江一百感谢点击[我的小网站,请大家多多指教相互共勉]

1.连接数据库

 using System;
using System.Collections.Generic;
using System.Web;
using System.Data.OleDb; namespace myBPB
{
public class myDB
{
OleDbConnection conn = null;
public OleDbConnection getConn()
{
string sServer = "192.168.132.10";
string sDatabase = "myBPB";
string sUser = "sa";
string sPassword = "";
//Provider=SQLOLEDB; 指的是连接数据库所用的相关数据库驱动
//Data Source; 指的是服务器地址或服务器名称
//Persist Security Info; 指的是是否保存安全信息,其实可以简单的理解为"在数据库连接成功后是否保存密码信息",True表示保存,False表示不保存
//Initial Catalog; 指的是要连接的数据库名称
//User ID; 登录数据库的名称
//Password; 登录数据库的密码
String sSQL = String.Format("Provider=SQLOLEDB;Data Source={0};" +"Persist Security Info=True;User ID={2};" +"Initial Catalog={1};Password={3};",sServer, sDatabase, sUser, sPassword);//数据库连接语句
try
{
conn = new OleDbConnection(sSQL);
conn.Open();
return conn;
}
catch (System.Exception ex)
{
return null;
}
}
}
}

连接数据库

2.填充ASPxGridView

 public void BindGridView()
{
OleDbConnection conn = new myDB().getConn();
string str = "select * from 区域";
OleDbDataAdapter da = new OleDbDataAdapter(str, conn);
DataSet ds = new DataSet();
da.Fill(ds);
this.ASPxGridView1.DataSource = ds.Tables[].DefaultView;
this.ASPxGridView1.DataBind();
conn.Close();
}

填充ASPxGridView

3.布局

控件:ASPxSplitter(分离器)

作用:布局时多用于分割页面

属性:Orientation(方向)= Vertical(垂直)或 Herizontal(水平)

ASPxSplitter1.Panes[0].PaneStyle.Paddings.Padding = 0;         //第一个面板的内边距为0

ASPxSplitter1.Panes[1].PaneStyle.Paddings.Padding = 0;         //第二个面板的内边距为0

ASPxSplitter1.Panes[0].Size = 63;                                        //第一个面板的高度(大小)为63

ASPxSplitter1.Panes[1].ScrollBars = ScrollBars.Auto;              //自动出现滚动条

ASPxSplitter1.AllowResize = false;                                       //不允许调整ASPxSplitter1

ASPxSplitter1.FullscreenMode = true;                                   //全屏模式为true


控件:ASPxMenu1(菜单)

作用:制作菜单

属性:width(宽度)=100%

Items(菜单的选项)

ItemAutoWidth(菜单选项宽度自动调整)=True(真)或False(假)

AutoSeparators="RootOnly"(菜单选项之间用竖线隔开)

ClientInstanceName="ASPxMenu1"(控件的前端名称)


控件:表格

作用:排版

属性:Style—>表格—>table-layout=fixed(表格宽度为固定值)


控件:ASPxImage

作用:显示图标、图片

属性:ASPxImage1.ImageUrl = "~/Images/man.gif";


控件:ASPxGridView

作用:显示数据

属性:ASPxGridView1.Width = 2000;

ASPxGridView1.SettingsBehavior.AllowFocusedRow = true;//每行都可以选择

Columns(列)— Caption(显示名称)— FieldName(数据库连接字段)

AutoGenerateColumns="False"//数据源自动绑定为假

KeyFieldName="bh"//关键字为bh

ClientInstanceName="ASPxGridView1"(控件的前端名称)

前端运用:

1.在每行前面添加一个ASPxCheckBox

         <dx:ASPxGridView ID="ASPxGridView1" runat="server">
<Columns> <dx:GridViewCommandColumn Caption="操作" ShowSelectCheckbox="true" VisibleIndex="" Width="20px">
<HeaderTemplate>
<dx:ASPxCheckBox ID="ASPxCheckBox1" runat="server" ToolTip="Select/Unselect all rows on the page"
ClientSideEvents-CheckedChanged="function(s, e) { ASPxGridView1.SelectAllRowsOnPage(s.GetChecked()); }" />
</HeaderTemplate>
</dx:GridViewCommandColumn> </Columns>
</dx:ASPxGridView>

自动添加ASPxCheck.xml

2.在第一列显示行号

         <dx:ASPxGridView ID="ASPxGridView1" runat="server">
<Columns> <dx:GridViewDataTextColumn Caption="序号" FieldName="XH" UnboundType="Integer" VisibleIndex=""Width="36px">
<Settings AutoFilterCondition="Contains" />
<CellStyle HorizontalAlign="Center">
</CellStyle>
</dx:GridViewDataTextColumn> </Columns>
</dx:ASPxGridView>

自动添加行号.xml

         protected void ASPxGridView1_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
{
if (e.Column.FieldName == "XH" && e.IsGetData)
e.Value = (e.ListSourceRowIndex + ).ToString();
}

自动添加行号.后台

3.将Columns分组

             <dx:GridViewBandColumn Caption="使用于">
<Columns>
<dx:GridViewDataTextColumn Caption="部门" FieldName="部门"
ShowInCustomizationForm="True" VisibleIndex="" Width="150px">
<Settings AutoFilterCondition="Contains" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="何工序或何设备" FieldName="何工序或何设备"
ShowInCustomizationForm="True" VisibleIndex="" Width="200px">
<Settings AutoFilterCondition="Contains" />
</dx:GridViewDataTextColumn>
</Columns>
</dx:GridViewBandColumn>

Columns.xml

4.ASPxComboBox配置数据源

 <asp:SqlDataSource ID="SqlDataSource1_BuMen" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT DISTINCT [部门名称], [部门编码] FROM [部门设置]"></asp:SqlDataSource>

数据源—部门.xml

 <dx:ASPxComboBox ID="Com_BuMen" runat="server"
DataSourceID="SqlDataSource1_BuMen" ValueField="部门编码" DropDownStyle="DropDownList"
ValueType="System.String" TextFormatString="{1}" EnableCallbackMode="true"
IncrementalFilteringMode="Contains" TextField="部门编码"
ClientInstanceName="Com_BuMen" >
<Columns>
<dx:ListBoxColumn FieldName="部门编码" Width="45px" />
<dx:ListBoxColumn FieldName="部门名称" Width="100%" />
</Columns>
</dx:ASPxComboBox>

部门文本框.前台

5.增加

 public void Add()
{
String str_add= "insert into 区域(区域号,区域,备注) values('" + QuYuHao + "','" + QuYu + "','" + BeiZhu + "')";
OleDbConnection conn = new myDB().getConn();
OleDbCommand com = new OleDbCommand(str_add, conn);
if(Convert.ToInt32(com.ExecuteNonQuery())>)
{
Page.ClientScript.RegisterClientScriptBlock(typeof(QuYuEdit), "CustomEdit", "alert('添加成功!!');", true);
}
else
{
Page.ClientScript.RegisterClientScriptBlock(typeof(QuYuEdit), "CustomEdit", "alert('添加失败!!');", true);
}
conn.Close();
}

增加.后台

6.删除

 public void Del()
{
String str_Del = "delete from 区域 where bh=" + ;
OleDbConnection conn = new myDB().getConn();
OleDbCommand com = new OleDbCommand(str_Del, conn);
int counts = com.ExecuteNonQuery();
if (counts > )
{
Page.ClientScript.RegisterClientScriptBlock(typeof(WuLiaoBianMa), "CustomEdit", "alert('删除成功!');", true);
}
else
{
Page.ClientScript.RegisterClientScriptBlock(typeof(WuLiaoBianMa), "CustomEdit", "alert('删除失败!');", true);
}
conn.Close();
}

删除.后台

7.修改

 public void Update()
{
String str_Update = "update 区域 set 区域号='" + QuYuHao + "',区域='" + QuYu + "',备注='" + BeiZhu + "' where bh=" + Convert.ToInt32(ASPxHiddenField1.Get("SBH1"));
OleDbConnection conn = new myDB().getConn();
OleDbCommand com = new OleDbCommand(str_Del, conn);
int counts = com.ExecuteNonQuery();
if (counts > )
{
Page.ClientScript.RegisterClientScriptBlock(typeof(WuLiaoBianMa), "CustomEdit", "alert('修改成功!');", true);
}
else
{
Page.ClientScript.RegisterClientScriptBlock(typeof(WuLiaoBianMa), "CustomEdit", "alert('修改失败!');", true);
}
conn.Close();
}

修改.后台

8.查询

 public String Old_Name()
{
OleDbConnection conn = new myDB().getConn();
string str = "select * from 物料编码 where bh='" + Convert.ToInt32(ASPxHiddenField1.Get("sBH")) + "'";
OleDbDataAdapter da = new OleDbDataAdapter(str, conn);
DataSet ds = new DataSet();
da.Fill(ds);
String m = ds.Tables[].Rows[]["物料编码"].ToString();
return m;
}

查询(DataAdapter、DataSet).后台

 public void Lei_Xing()
{
OleDbConnection conn = new myDB().getConn();
string str = "select * from 基础数据 where 目录='供应商类型'";
OleDbCommand com = new OleDbCommand(str, conn);
OleDbDataReader read = com.ExecuteReader();
while (read.Read())
{
Txt_LeiXing.Items.Add(read["内容"].ToString());
}
}

查询后台(DataReader).后台

9.ASPxGridView中勾选ASPxCheckBox删除数据

 List<object> keyValues = this.ASPxGridView1.GetSelectedFieldValues("bh");  //需要在ASPxGridView1的源设置 KeyFieldName="bh"
ASPxGridView1.Selection.UnselectAll(); //清空ASPxGridView中所有选择的语句。
int count = keyValues.Count;
if (str == "删除")
{
if (count <= )
{
Page.ClientScript.RegisterClientScriptBlock(typeof(WuLiaoBianMa), "CustomEdit", "alert('请选择要删除的数据!');", true);
return;
}
foreach (object key in keyValues)
{
do_del(key.ToString()); //删除
}
keyValues.Clear();
count = keyValues.Count;
BindGridView();
}

ASPxGridView中勾选ASPxCheckBox删除数据

10.ASPxGridView过滤

后台:ASPxGridView1.Settings.ShowFilterRow = true;

前台:<Settings AutoFilterCondition="Contains" />//ASPxGridView属性—>Columns—>要实现过滤的列—>Settings—>AutoFilterCondition="Contains"

11.ASPxGridViewExporter导出

                 if (Directory.Exists(Server.MapPath("tmp")) == false)
{
Directory.CreateDirectory(Server.MapPath("tmp"));
}
string path = Server.MapPath("~/tmp/");//获取完整路径
ASPxGridViewExporter1.FileName = "物料编码";
ASPxGridViewExporter1.PageHeader.Font.Size = ;
string message1 = @"物料编码";
ASPxGridViewExporter1.PageHeader.Center = message1;
this.ASPxGridViewExporter1.WriteXlsToResponse();

ASPxGridViewExporter导出.后台

12.ASPxHiddenField隐藏字段

设置前台名称:ClientInstanceName="hf1";
后台赋初值:ASPxHiddenField1.Add("SBH",0);
前台赋值:hf1.Set('sBH', ASPxGridView1.GetRowKey(ASPxGridView1.GetFocusedRowIndex())); //给隐藏字段赋值为ASPxGridView1当前被选中行的bh值
后台取值:ASPxHiddenField1.Get("sBH");
前台取值:hf1.Get('sBH')

13.ASPxPopupControl自定义约会表单

设置前台名称:ClientInstanceName="ASPxPopupControl1";
前台显示ASPxPopupControl自定义表单:ASPxPopupControl1.Show();
前台隐藏ASPxPopupControl自定义表单:ASPxPopupControl1.Hid();

14.ASPxUploadControl上传、导入实例(导入)

点击导入按钮,弹出下图的自定义约会表单,将选择的文件上传至固定的地址并导入ASPxGridView中

初见,Devexpress

 .前台弹出表单:ASPxPopupControl1.Show();

 .上传文件到指定的地址:
String ExcelFileName = "";
protected voidASPxUploadControl1_FileUploadComplete(objectsender,DevExpress.Web.ASPxUploadControl.
FileUploadCompleteEventArgs e)
{
if (Directory.Exists(Server.MapPath("tmp")) == false)
{
Directory.CreateDirectory(Server.MapPath("tmp"));
}
if (e.IsValid)
{
ASPxUploadControl1.PostedFile.SaveAs(Server.MapPath("~/tmp/")+ ASPxUploadControl1.PostedFile.FileName);
ExcelFileName = ASPxUploadControl1.PostedFile.FileName;
}
} .导入ASPxGridView:
#region 从Table导出数据DaoRu()
public void DaoRu(ASPxGridView ASPxGridView1,String ExcelFileName)
{
try //步骤一:先将用户选择的EXCEL文件上传至服务器端。
{
if (Directory.Exists(Server.MapPath("tmp")) == false)
{
Directory.CreateDirectory(Server.MapPath("tmp"));
}
string path = Server.MapPath("~/tmp/") + ExcelFileName;//获取完整路径
GetOneSheet("Sheet", path, ASPxGridView1); }
catch
{
throw;
}
} // 步骤二:将上传的文件导入至DataSet
public static void GetOneSheet(string sheetname, string Path, ASPxGridView ASPxGridView1)
{
try
{
string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path + ";Extended Properties=Excel 8.0;";
OleDbConnection con = new OleDbConnection(connString); //连接excel表
con.Open();
String strOutPutExcel = "select * from [" + sheetname + "$]";
OleDbDataAdapter ad = new OleDbDataAdapter(strOutPutExcel, con);
DataSet table = table = new DataSet();
ad.Fill(table,"table1"); //填充dataset
int count = table.Tables[].Rows.Count;
OleDbConnection conn = new myDB().getConn();
OleDbCommand com = null;
for (int i = ; i < count; i++)
{ String QuYuHao = table.Tables[].Rows[i][].ToString().Trim();
String LeiBeiHao = table.Tables[].Rows[i][].ToString().Trim();
String str_insert = "INSERT INTO [物料编码] ( [区域号], [类别号]) VALUES (?, ?)"; OleDbParameter op1 = new OleDbParameter();
OleDbParameter op2 = new OleDbParameter(); op1.OleDbType = OleDbType.VarChar;
op2.OleDbType = OleDbType.VarChar;
op1.Value = QuYuHao.ToString();
op2.Value = LeiBeiHao.ToString();
com = new OleDbCommand(str_insert,conn);
com.Parameters.Add(op1);
com.Parameters.Add(op2);
try
{
com.ExecuteNonQuery();
}
catch
{
//Debug.WriteLine();
} }
con.Close();
}
catch
{
throw;
}
}
#endregion .确定按钮单击事件中调用DaoRu():
DaoRu(ASPxGridView1, ExcelFileName);
BindGridView(); //重新填充ASPxGridView

代码实现

15.根据ASPxGridView值得变化,前端给控件赋值实例(修改)
初见,Devexpress

 .ClientInstanceName="ASPxGridView1"   //要赋值的前端控件也需设置前端名称
.判断是否选择了"修改"
if (str == "修改")
{
Session.Add("Add_Or_Update", );
Response.Redirect("~/WuLiaoBianMaEdit.aspx"); //转到下图的修改页面
} ."修改"页面加载:
if (!IsPostBack)
{
if (Convert.ToInt32(Session["Add_Or_Update"]) == )
{
ASPxImage1.ImageUrl = "~/Images/m5.gif";
ASPxRoundPanel1.HeaderText = "修改";
ASPxHiddenField1.Add("sBH2", );
ASPxHiddenField1.Add("sBH", );
}
} .ASPxGridView单击事件:
<ClientSideEvents FocusedRowChanged="function(s, e) { OnGridFocusedRowChanged(); }" />
<ClientSideEvents FocusedRowChanged="function(s, e) { OnGridFocusedRowChanged(); }"></ClientSideEvents>
<%--ASPxGridView的单击事件--%> .Javascript实现ASPxGridView单击事件:
<script language="javascript" type="text/javascript">
function OnGridFocusedRowChanged()
{
ASPxGridView1.GetRowValues(ASPxGridView1.GetFocusedRowIndex(), '区域号;类别号', OnGetRowValues); //前端访问控件
}
function OnGetRowValues(values)
{
hf1.Set('sBH', ASPxGridView1.GetRowKey(ASPxGridView1.GetFocusedRowIndex())); //给隐藏字段赋值
if (hf1.Get('sBH2') == )
{
Com_QuYuHao.SetText(values[]);
Com_LeiBieHao.SetText(values[]);
}
}
</script>

代码实现

16.选择"区域号","类别号"后,点击"已用流水号"按钮,从数据库中读取数据加载到表单中,并弹出表单(防止刷屏问题)

初见,Devexpress

 .ClientInstanceName="grid2"
.写一个自定义函数,填充表单中的ASPxGridView:
public void LiuShuiHao(String QuYuHao,String LeiBieHao)
{
String str_LiuShuiHao = "";
if (QuYuHao != "" && LeiBieHao != "")
{
str_LiuShuiHao = "select * from 物料编码 where 区域号='" + QuYuHao + "' and 类别号='" + LeiBieHao + "'";
}
else if (QuYuHao != "")
{
str_LiuShuiHao = "select * from 物料编码 where 区域号='" + QuYuHao + "'";
}
else if (LeiBieHao != "")
{
str_LiuShuiHao = "select * from 物料编码 where 类别号='" + LeiBieHao + "'";
}
if(str_LiuShuiHao!="")
{
OleDbConnection conn = new myDB().getConn();
OleDbDataAdapter da = new OleDbDataAdapter(str_LiuShuiHao, conn);
DataSet ds = new DataSet();
da.Fill(ds);
this.ASPxGridView2.DataSource = ds.Tables[].DefaultView;
this.ASPxGridView2.DataBind();
conn.Close();
}
} .在页面的load中调用该方法:
if (Com_QuYuHao.Text != "" || Com_LeiBieHao.Text != "")
{
LiuShuiHao(Com_QuYuHao.Text, Com_LeiBieHao.Text);
} .按钮的前端Click事件:
ASPxPopupControl1.Show();
grid2.Refresh();
e.processOnServer = false; 读取数据库防止—刷屏

读数据库—防止刷屏