Dev的datagirdview中combobox多級聯動
阿新 • • 發佈:2019-01-24
datagridview中的column為combobox時的資料繫結和聯動,就是同一行的後面的combobox根據前面的列的combobox變化而變化
下面是用dev的asp.net控制元件做的combobox3級聯動的一個小demo:
aspx檔案:
下面是用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"
- <%@ 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=
- 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>
- 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
- privatebool isDept = false;
- //
- privateint yearSelectindex = 0;
- //是否年份selectindexchanged
- privatebool 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)
- returnnull;
- 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;
- }
- }
- protectedvoid 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();
- }
- }
- //進入編輯狀態的回撥的初始化
- protectedvoid 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
- elseif (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
- elseif (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[] { };
- }
- //客戶端回撥事件
- protectedvoid 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[] { };
- }
- }
- //編輯狀態切換
- protectedvoid ASPxGridView1_CancelRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
- {
- Session["year"] = null;
- Session["dept"] = null;
- Session["person"] = null;
- }
- }