您的位置:首页 > 其它

Dev的datagirdview中combobox多级联动

2008-09-19 08:53 405 查看
datagridview中的column为combobox时的数据绑定和联动,就是同一行的后面的combobox根据前面的列的combobox变化而变化
下面是用dev的asp.net控件做的combobox3级联动的一个小demo:
aspx文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<%@ Register assembly="DevExpress.Web.ASPxGridView.v8.1, Version=8.1.3.0, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1" namespace="DevExpress.Web.ASPxGridView" tagprefix="dxwgv" %>
<%@ Register assembly="DevExpress.Web.ASPxEditors.v8.1, Version=8.1.3.0, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1" namespace="DevExpress.Web.ASPxEditors" tagprefix="dxe" %>

<!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>MutilCombobox</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" Width="427px"
KeyFieldName="id"
oncelleditorinitialize="ASPxGridView1_CellEditorInitialize"
oncustomcallback="ASPxGridView1_CustomCallback"
oncancelrowediting="ASPxGridView1_CancelRowEditing">
<SettingsEditing Mode="Inline" />
<Columns>
<dxwgv:GridViewCommandColumn Caption="编辑">
<EditButton Visible="true"></EditButton>
</dxwgv:GridViewCommandColumn>
<dxwgv:GridViewDataComboBoxColumn Caption="年份" FieldName="year" Width="100px">
<PropertiesComboBox ValueType="System.String">
<%--设定客户端回传事件--%>
<ClientSideEvents SelectedIndexChanged="function(s, e){ASPxGridView1.PerformCallback('year');}" />
</PropertiesComboBox>
</dxwgv:GridViewDataComboBoxColumn>
<dxwgv:GridViewDataComboBoxColumn Caption="部门" FieldName="dept" Width="200px">
<PropertiesComboBox ValueType="System.String">
<%--设定客户端回传事件--%>
<ClientSideEvents SelectedIndexChanged="function(s, e){ASPxGridView1.PerformCallback('dept');}" />
</PropertiesComboBox>
</dxwgv:GridViewDataComboBoxColumn>
<dxwgv:GridViewDataComboBoxColumn Caption="人员" FieldName="person" Width="300px">
<PropertiesComboBox ValueType="System.String"></PropertiesComboBox>
</dxwgv:GridViewDataComboBoxColumn>
</Columns>
</dxwgv:ASPxGridView>
</div>
</form>
</body>
</html>

aspx.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DevExpress.Web.ASPxEditors;
using System.Web.UI.MobileControls;
using System.Collections.Generic;
using DevExpress.Web.ASPxGridView;

public partial class Default2 : System.Web.UI.Page
{
//整个数据源
private DataTable main = null;
//绑定gridview的数据源
private DataTable init = null;
//是否部门selectindexchanged
private bool isDept = false;
//
private int yearSelectindex = 0;
//是否年份selectindexchanged
private bool isYear = false;

//不连接数据库,创建临时表
private DataTable CreateMainData()
{
DataTable table = new DataTable();
table.Columns.Add("id", typeof(int));
table.Columns.Add("rowid", typeof(int));
table.Columns.Add("year", typeof(string));
table.Columns.Add("dept", typeof(string));
table.Columns.Add("person", typeof(string));

int m = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 2007; j < 2010; j++)
{
for (int k = 0; k < 5; k++)
{
for (int l = 0; l < 3; l++)
{
DataRow row = table.NewRow();
row[0] = m;
row[1] = i;
row[2] = j.ToString();
row[3] = j + "_dept_" + k;
row[4] = row[3] + "_person_" + l;
table.Rows.Add(row);
m++;
}
}
}
}
return table;
}
//从总数据源中获取girdview的绑定表
private DataTable GetInit()
{
if (Session["main"] == null)
return null;
else
{
DataTable mainTable = (DataTable)Session["main"];
DataTable table = new DataTable();
table.Columns.Add("id", typeof(int));
table.Columns.Add("rowid", typeof(int));
table.Columns.Add("year", typeof(string));
table.Columns.Add("dept", typeof(string));
table.Columns.Add("person", typeof(string));

DataRow[] rows = mainTable.Select("person='2007_dept_0_person_0' and rowid=0");
table.Rows.Add(rows[0][0], rows[0][1], rows[0][2], rows[0][3], rows[0][4]);
rows = mainTable.Select("person='2007_dept_0_person_0' and rowid=1");
table.Rows.Add(rows[0][0], rows[0][1], rows[0][2], rows[0][3], rows[0][4]);
rows = mainTable.Select("person='2007_dept_0_person_0' and rowid=2");
table.Rows.Add(rows[0][0], rows[0][1], rows[0][2], rows[0][3], rows[0][4]);
return table;
}
}
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
//创建数据源和绑定gridview
this.main = CreateMainData();
Session["main"] = main;
this.init = GetInit();
this.ASPxGridView1.DataSource = this.init.DefaultView;
this.ASPxGridView1.DataBind();
}

}
//进入编辑状态的回调的初始化
protected void ASPxGridView1_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e)
{
if (Session["main"] == null)
return;
DataTable table = (DataTable)Session["main"];
//设置年的combobox的items
if (e.Column.FieldName == "year")
{
ASPxComboBox cboYear = e.Editor as ASPxComboBox;
cboYear.Items.Clear();
DataRow[] rows = null;
if (Session["year"] == null)
{
rows = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex);
Session["year"] = rows;
}
else
rows = (DataRow[])Session["year"];
if (rows.Length < 1)
{
cboYear.Text = "";
return;
}
else
{
//是否是初始化
if(!IsCallback)
cboYear.Text = rows[0][2].ToString();

}
foreach (DataRow row in rows)
{
if(cboYear.Items.IndexOfText(row[2].ToString()) < 0)
cboYear.Items.Add(row[2].ToString());
}
//cboYear.SelectedIndex = cboYear.Items.IndexOfText(cboYear.Text);
cboYear.SelectedIndexChanged += new EventHandler(cboYear_SelectedIndexChanged);
cboYear.SelectedIndexChanged += new EventHandler(cboYear_SelectedIndexChanged);
}
//设置部门的combobox的items
else if (e.Column.FieldName == "dept")
{
ASPxComboBox cboDept = e.Editor as ASPxComboBox;
cboDept.Items.Clear();
DataRow[] rows = null;
if (Session["dept"] == null)
{
rows = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + this.ASPxGridView1.GetRowValues(this.ASPxGridView1.EditingRowVisibleIndex, "year").ToString() + "'");
Session["dept"] = rows;
}
else
rows = (DataRow[])Session["dept"];
if (rows.Length < 1)
{
cboDept.Text = "";
return;
}
else
{
//如果不是“部门”的回传,则默认选择第一项
if (!this.isDept)
cboDept.Text = rows[0][3].ToString();
}
foreach (DataRow row in rows)
{
if (cboDept.Items.IndexOfText(row[3].ToString()) < 0)
cboDept.Items.Add(row[3].ToString());
}
//cboDept.SelectedIndex = cboDept.Items.IndexOfText(cboDept.Text);
cboDept.SelectedIndexChanged -= new EventHandler(cboDept_SelectedIndexChanged);
cboDept.SelectedIndexChanged += new EventHandler(cboDept_SelectedIndexChanged);

}
//设置人员的combobox的items
else if (e.Column.FieldName == "person")
{
ASPxComboBox cboPerson = e.Editor as ASPxComboBox;
cboPerson.Items.Clear();
DataRow[] rows = null;
if (Session["person"] == null)
{
rows = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + this.ASPxGridView1.GetRowValues(this.ASPxGridView1.EditingRowVisibleIndex, "year").ToString() + "' and dept='" + this.ASPxGridView1.GetRowValues(this.ASPxGridView1.EditingRowVisibleIndex, "dept").ToString() + "'");
Session["person"] = rows;
}
else
rows = (DataRow[])Session["person"];
if (rows.Length < 1)
{
cboPerson.Text = "";
return;
}
else
{
if (cboPerson.SelectedIndex >= 0)
cboPerson.Text = cboPerson.SelectedItem.Text;
else
cboPerson.Text = rows[0][4].ToString();
}
foreach (DataRow row in rows)
{
if (cboPerson.Items.IndexOfText(row[4].ToString()) < 0)
cboPerson.Items.Add(row[4].ToString());
}

}
}
//部门selectindexchanged事件
void cboDept_SelectedIndexChanged(object sender, EventArgs e)
{
this.isDept = true;
if (Session["main"] == null)
return;
DataTable table = (DataTable)Session["main"];
ASPxComboBox cboDept = (ASPxComboBox)sender;
DataRow[] deptrows = (DataRow[])Session["dept"];
//更新人员联动信息
Session["person"] = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + deptrows[0][2].ToString() + "' and dept='" + cboDept.SelectedItem.Text + "'");
}
//年份selectindexchanged事件
void cboYear_SelectedIndexChanged(object sender, EventArgs e)
{
this.isYear = true;
this.isDept = false;
if (Session["main"] == null)
return;
DataTable table = (DataTable)Session["main"];
ASPxComboBox cboYear = (ASPxComboBox)sender;
this.yearSelectindex = cboYear.SelectedIndex;
//更新部门联动信息
DataRow[] deptrows = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + cboYear.SelectedItem.Text + "'");
Session["dept"] = deptrows;
//更新人员联动信息
if (deptrows.Length > 0)
Session["person"] = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + cboYear.SelectedItem.Text + "' and dept='" + deptrows[0][3].ToString() + "'");
else
Session["person"] = new DataRow[] { };
}
//客户端回调事件
protected void ASPxGridView1_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
{
//如果是“年份”的回调,则修改相应的联动信息
if (e.Parameters.Equals("year"))
{
this.isYear = true;
this.isDept = false;
if (Session["main"] == null)
return;
DataTable table = (DataTable)Session["main"];
DataRow[] yearrows = (DataRow[])Session["year"];
List<string> strings = new List<string>();
foreach (DataRow row in yearrows)
{
if (strings.IndexOf(row[2].ToString()) < 0)
strings.Add(row[2].ToString());
}
DataRow[] deptrows = new DataRow[] { };
if (yearrows.Length > 0)
deptrows = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + strings[this.yearSelectindex] + "'");
Session["dept"] = deptrows;
if (deptrows.Length > 0)
Session["person"] = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + strings[this.yearSelectindex] + "' and dept='" + deptrows[0][3].ToString() + "'");
else
Session["person"] = new DataRow[] { };
}

}
//编辑状态切换
protected void ASPxGridView1_CancelRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
{
Session["year"] = null;
Session["dept"] = null;
Session["person"] = null;
}
}

没有写girdview的update事件。运行效果截图,

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: