1. 程式人生 > >Dev的datagirdview中combobox多級聯動

Dev的datagirdview中combobox多級聯動

datagridview中的column為combobox時的資料繫結和聯動,就是同一行的後面的combobox根據前面的列的combobox變化而變化
下面是用dev的asp.net控制元件做的combobox3級聯動的一個小demo:
aspx檔案:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
  2. <%@ Register assembly="DevExpress.Web.ASPxGridView.v8.1, Version=8.1.3.0, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1"namespace="DevExpress.Web.ASPxGridView"
     tagprefix="dxwgv" %>
  3. <%@ Register assembly="DevExpress.Web.ASPxEditors.v8.1, Version=8.1.3.0, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1"namespace="DevExpress.Web.ASPxEditors" tagprefix="dxe" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    >
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7.     <title>MutilCombobox</title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.     <div>
  12.         <dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" Width="427px"
  13.             KeyFieldName=
    "id"
  14.             oncelleditorinitialize="ASPxGridView1_CellEditorInitialize"
  15.             oncustomcallback="ASPxGridView1_CustomCallback"
  16.             oncancelrowediting="ASPxGridView1_CancelRowEditing">
  17.             <SettingsEditing Mode="Inline" />
  18.             <Columns>
  19.                 <dxwgv:GridViewCommandColumn Caption="編輯">
  20.                     <EditButton Visible="true"></EditButton>
  21.                 </dxwgv:GridViewCommandColumn>
  22.                 <dxwgv:GridViewDataComboBoxColumn Caption="年份" FieldName="year" Width="100px">
  23.                     <PropertiesComboBox ValueType="System.String">
  24.                         <%--設定客戶端回傳事件--%>
  25.                         <ClientSideEvents SelectedIndexChanged="function(s, e){ASPxGridView1.PerformCallback('year');}" />
  26.                     </PropertiesComboBox>
  27.                 </dxwgv:GridViewDataComboBoxColumn>
  28.                 <dxwgv:GridViewDataComboBoxColumn Caption="部門" FieldName="dept" Width="200px">
  29.                     <PropertiesComboBox ValueType="System.String">
  30.                         <%--設定客戶端回傳事件--%>
  31.                         <ClientSideEvents SelectedIndexChanged="function(s, e){ASPxGridView1.PerformCallback('dept');}" />
  32.                     </PropertiesComboBox>
  33.                 </dxwgv:GridViewDataComboBoxColumn>
  34.                 <dxwgv:GridViewDataComboBoxColumn Caption="人員" FieldName="person" Width="300px">
  35. <PropertiesComboBox ValueType="System.String"></PropertiesComboBox>
  36.                 </dxwgv:GridViewDataComboBoxColumn>
  37.             </Columns>
  38.         </dxwgv:ASPxGridView>
  39.     </div>
  40.     </form>
  41. </body>
  42. </html>
aspx.cs:
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using DevExpress.Web.ASPxEditors;
  14. using System.Web.UI.MobileControls;
  15. using System.Collections.Generic;
  16. using DevExpress.Web.ASPxGridView;
  17. public partial class Default2 : System.Web.UI.Page
  18. {
  19. //整個資料來源
  20. private DataTable main = null;
  21. //繫結gridview的資料來源
  22. private DataTable init = null;
  23. //是否部門selectindexchanged
  24. privatebool isDept = false;
  25. //
  26. privateint yearSelectindex = 0;
  27. //是否年份selectindexchanged
  28. privatebool isYear = false;
  29. //不連線資料庫,建立臨時表
  30. private DataTable CreateMainData()
  31.     {
  32.         DataTable table = new DataTable();
  33.         table.Columns.Add("id"typeof(int));
  34.         table.Columns.Add("rowid"typeof(int));
  35.         table.Columns.Add("year"typeof(string));
  36.         table.Columns.Add("dept"typeof(string));
  37.         table.Columns.Add("person"typeof(string));
  38. int m = 0;
  39. for (int i = 0; i < 3; i++)
  40.         {
  41. for (int j = 2007; j < 2010; j++)
  42.             {
  43. for (int k = 0; k < 5; k++)
  44.                 {
  45. for (int l = 0; l < 3; l++)
  46.                     {
  47.                         DataRow row = table.NewRow();
  48.                         row[0] = m;
  49.                         row[1] = i;
  50.                         row[2] = j.ToString();
  51.                         row[3] = j + "_dept_" + k;
  52.                         row[4] = row[3] + "_person_" + l;
  53.                         table.Rows.Add(row);
  54.                         m++;
  55.                     }
  56.                 }
  57.             }
  58.         }
  59. return table;
  60.     }
  61. //從總資料來源中獲取girdview的繫結表
  62. private DataTable GetInit()
  63.     {
  64. if (Session["main"] == null)
  65. returnnull;
  66. else
  67.         {
  68.             DataTable mainTable = (DataTable)Session["main"];
  69.             DataTable table = new DataTable();
  70.             table.Columns.Add("id"typeof(int));
  71.             table.Columns.Add("rowid"typeof(int));
  72.             table.Columns.Add("year"typeof(string));
  73.             table.Columns.Add("dept"typeof(string));
  74.             table.Columns.Add("person"typeof(string));
  75.             DataRow[] rows = mainTable.Select("person='2007_dept_0_person_0' and rowid=0");
  76.             table.Rows.Add(rows[0][0], rows[0][1], rows[0][2], rows[0][3], rows[0][4]);
  77.             rows = mainTable.Select("person='2007_dept_0_person_0' and rowid=1");
  78.             table.Rows.Add(rows[0][0], rows[0][1], rows[0][2], rows[0][3], rows[0][4]);
  79.             rows = mainTable.Select("person='2007_dept_0_person_0' and rowid=2");
  80.             table.Rows.Add(rows[0][0], rows[0][1], rows[0][2], rows[0][3], rows[0][4]);
  81. return table;
  82.         }
  83.     }
  84. protectedvoid Page_Load(object sender, EventArgs e)
  85.     {
  86. if (!IsPostBack)
  87.         {
  88. //建立資料來源和繫結gridview
  89. this.main = CreateMainData();
  90.             Session["main"] = main;
  91. this.init = GetInit();
  92. this.ASPxGridView1.DataSource = this.init.DefaultView;
  93. this.ASPxGridView1.DataBind();
  94.         }
  95.     }
  96. //進入編輯狀態的回撥的初始化
  97. protectedvoid ASPxGridView1_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e)
  98.     {
  99. if (Session["main"] == null)
  100. return;
  101.         DataTable table = (DataTable)Session["main"];
  102. //設定年的combobox的items
  103. if (e.Column.FieldName == "year")
  104.         {
  105.             ASPxComboBox cboYear = e.Editor as ASPxComboBox;
  106.             cboYear.Items.Clear();
  107.             DataRow[] rows = null;
  108. if (Session["year"] == null)
  109.             {
  110.                 rows = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex);
  111.                 Session["year"] = rows;
  112.             }
  113. else
  114.                 rows = (DataRow[])Session["year"];
  115. if (rows.Length < 1)
  116.             {
  117.                 cboYear.Text = "";
  118. return;
  119.             }
  120. else
  121.             {
  122. //是否是初始化
  123. if(!IsCallback)
  124.                     cboYear.Text = rows[0][2].ToString();
  125.             }
  126. foreach (DataRow row in rows)
  127.             {
  128. if(cboYear.Items.IndexOfText(row[2].ToString()) < 0)
  129.                     cboYear.Items.Add(row[2].ToString());
  130.             }
  131. //cboYear.SelectedIndex = cboYear.Items.IndexOfText(cboYear.Text);
  132.             cboYear.SelectedIndexChanged += new EventHandler(cboYear_SelectedIndexChanged);
  133.             cboYear.SelectedIndexChanged += new EventHandler(cboYear_SelectedIndexChanged);
  134.         }
  135. //設定部門的combobox的items
  136. elseif (e.Column.FieldName == "dept")
  137.         {
  138.             ASPxComboBox cboDept = e.Editor as ASPxComboBox;
  139.             cboDept.Items.Clear();
  140.             DataRow[] rows = null;
  141. if (Session["dept"] == null)
  142.             {
  143.                 rows = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + this.ASPxGridView1.GetRowValues(this.ASPxGridView1.EditingRowVisibleIndex, "year").ToString() + "'");
  144.                 Session["dept"] = rows;
  145.             }
  146. else
  147.                 rows = (DataRow[])Session["dept"];
  148. if (rows.Length < 1)
  149.             {
  150.                 cboDept.Text = "";
  151. return;
  152.             }
  153. else
  154.             {
  155. //如果不是“部門”的回傳,則預設選擇第一項
  156. if (!this.isDept)
  157.                     cboDept.Text = rows[0][3].ToString();
  158.             }
  159. foreach (DataRow row in rows)
  160.             {
  161. if (cboDept.Items.IndexOfText(row[3].ToString()) < 0)
  162.                     cboDept.Items.Add(row[3].ToString());
  163.             }
  164. //cboDept.SelectedIndex = cboDept.Items.IndexOfText(cboDept.Text);
  165.             cboDept.SelectedIndexChanged -= new EventHandler(cboDept_SelectedIndexChanged);
  166.             cboDept.SelectedIndexChanged += new EventHandler(cboDept_SelectedIndexChanged);
  167.         }
  168. //設定人員的combobox的items
  169. elseif (e.Column.FieldName == "person")
  170.         {
  171.             ASPxComboBox cboPerson = e.Editor as ASPxComboBox;
  172.             cboPerson.Items.Clear();
  173.             DataRow[] rows = null;
  174. if (Session["person"] == null)
  175.             {
  176.                 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() + "'");
  177.                 Session["person"] = rows;
  178.             }
  179. else
  180.                 rows = (DataRow[])Session["person"];
  181. if (rows.Length < 1)
  182.             {
  183.                 cboPerson.Text = "";
  184. return;
  185.             }
  186. else
  187.             {
  188. if (cboPerson.SelectedIndex >= 0)
  189.                     cboPerson.Text = cboPerson.SelectedItem.Text;
  190. else
  191.                     cboPerson.Text = rows[0][4].ToString();
  192.             }
  193. foreach (DataRow row in rows)
  194.             {
  195. if (cboPerson.Items.IndexOfText(row[4].ToString()) < 0)
  196.                     cboPerson.Items.Add(row[4].ToString());
  197.             }
  198.         }
  199.     }
  200. //部門selectindexchanged事件
  201. void cboDept_SelectedIndexChanged(object sender, EventArgs e)
  202.     {
  203. this.isDept = true;
  204. if (Session["main"] == null)
  205. return;
  206.         DataTable table = (DataTable)Session["main"];
  207.         ASPxComboBox cboDept = (ASPxComboBox)sender;
  208.         DataRow[] deptrows = (DataRow[])Session["dept"];
  209. //更新人員聯動資訊
  210.         Session["person"] = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + deptrows[0][2].ToString() + "' and dept='" + cboDept.SelectedItem.Text + "'");
  211.     }
  212. //年份selectindexchanged事件
  213. void cboYear_SelectedIndexChanged(object sender, EventArgs e)
  214.     {
  215. this.isYear = true;
  216. this.isDept = false;
  217. if (Session["main"] == null)
  218. return;
  219.         DataTable table = (DataTable)Session["main"];
  220.         ASPxComboBox cboYear = (ASPxComboBox)sender;
  221. this.yearSelectindex = cboYear.SelectedIndex;
  222. //更新部門聯動資訊
  223.         DataRow[] deptrows = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + cboYear.SelectedItem.Text + "'");
  224.         Session["dept"] = deptrows;
  225. //更新人員聯動資訊
  226. if (deptrows.Length > 0)
  227.             Session["person"] = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + cboYear.SelectedItem.Text + "' and dept='" + deptrows[0][3].ToString() + "'");
  228. else
  229.             Session["person"] = new DataRow[] { };
  230.     }
  231. //客戶端回撥事件
  232. protectedvoid ASPxGridView1_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
  233.     {
  234. //如果是“年份”的回撥,則修改相應的聯動資訊
  235. if (e.Parameters.Equals("year"))
  236.         {
  237. this.isYear = true;
  238. this.isDept = false;
  239. if (Session["main"] == null)
  240. return;
  241.             DataTable table = (DataTable)Session["main"];
  242.             DataRow[] yearrows = (DataRow[])Session["year"];
  243.             List<string> strings = new List<string>();
  244. foreach (DataRow row in yearrows)
  245.             {
  246. if (strings.IndexOf(row[2].ToString()) < 0)
  247.                     strings.Add(row[2].ToString());
  248.             }
  249.             DataRow[] deptrows = new DataRow[] { };
  250. if (yearrows.Length > 0)
  251.                 deptrows = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + strings[this.yearSelectindex] + "'");
  252.             Session["dept"] = deptrows;
  253. if (deptrows.Length > 0)
  254.                 Session["person"] = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + strings[this.yearSelectindex] + "' and dept='" + deptrows[0][3].ToString() + "'");
  255. else
  256.                 Session["person"] = new DataRow[] { };
  257.         }
  258.     }
  259. //編輯狀態切換
  260. protectedvoid ASPxGridView1_CancelRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
  261.     {
  262.         Session["year"] = null;
  263.         Session["dept"] = null;
  264.         Session["person"] = null;
  265.     }
  266. }
沒有寫girdview的update事件。執行效果截圖,