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.  
  3. <%@ Register assembly="DevExpress.Web.ASPxGridView.v8.1, Version=8.1.3.0, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1" namespace="DevExpress.Web.ASPxGridView" tagprefix="dxwgv" %>
  4. <%@ Register assembly="DevExpress.Web.ASPxEditors.v8.1, Version=8.1.3.0, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1" namespace="DevExpress.Web.ASPxEditors" tagprefix="dxe" %>
  5.  
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  7.  
  8. <html xmlns="http://www.w3.org/1999/xhtml">
  9. <head runat="server">
  10.     <title>MutilCombobox</title>
  11. </head>
  12. <body>
  13.     <form id="form1" runat="server">
  14.     <div>
  15.         <dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" Width="427px" 
  16.             KeyFieldName="id" 
  17.             oncelleditorinitialize="ASPxGridView1_CellEditorInitialize" 
  18.             oncustomcallback="ASPxGridView1_CustomCallback" 
  19.             oncancelrowediting="ASPxGridView1_CancelRowEditing">
  20.             <SettingsEditing Mode="Inline" />
  21.             <Columns>
  22.                 <dxwgv:GridViewCommandColumn Caption="编辑">
  23.                     <EditButton Visible="true"></EditButton>
  24.                 </dxwgv:GridViewCommandColumn>
  25.                 <dxwgv:GridViewDataComboBoxColumn Caption="年份" FieldName="year" Width="100px">
  26.                     <PropertiesComboBox ValueType="System.String">
  27.                         <%--设定客户端回传事件--%>
  28.                         <ClientSideEvents SelectedIndexChanged="function(s, e){ASPxGridView1.PerformCallback('year');}" />
  29.                     </PropertiesComboBox>
  30.                 </dxwgv:GridViewDataComboBoxColumn>
  31.                 <dxwgv:GridViewDataComboBoxColumn Caption="部门" FieldName="dept" Width="200px">
  32.                     <PropertiesComboBox ValueType="System.String">
  33.                         <%--设定客户端回传事件--%>
  34.                         <ClientSideEvents SelectedIndexChanged="function(s, e){ASPxGridView1.PerformCallback('dept');}" />
  35.                     </PropertiesComboBox>
  36.                 </dxwgv:GridViewDataComboBoxColumn>
  37.                 <dxwgv:GridViewDataComboBoxColumn Caption="人员" FieldName="person" Width="300px">
  38. <PropertiesComboBox ValueType="System.String"></PropertiesComboBox>
  39.                 </dxwgv:GridViewDataComboBoxColumn>
  40.             </Columns>
  41.         </dxwgv:ASPxGridView>
  42.     </div>
  43.     </form>
  44. </body>
  45. </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.  
  18. public partial class Default2 : System.Web.UI.Page
  19. {
  20.     //整个数据源
  21.     private DataTable main = null;
  22.     //绑定gridview的数据源
  23.     private DataTable init = null;
  24.     //是否部门selectindexchanged
  25.     private bool isDept = false;
  26.     //
  27.     private int yearSelectindex = 0;
  28.     //是否年份selectindexchanged
  29.     private bool isYear = false;
  30.     
  31.     //不连接数据库,创建临时表
  32.     private DataTable CreateMainData()
  33.     {
  34.         DataTable table = new DataTable();
  35.         table.Columns.Add("id", typeof(int));
  36.         table.Columns.Add("rowid", typeof(int));
  37.         table.Columns.Add("year", typeof(string));
  38.         table.Columns.Add("dept", typeof(string));
  39.         table.Columns.Add("person", typeof(string));
  40.  
  41.         int m = 0;
  42.         for (int i = 0; i < 3; i++)
  43.         {
  44.             for (int j = 2007; j < 2010; j++)
  45.             {
  46.                 for (int k = 0; k < 5; k++)
  47.                 {
  48.                     for (int l = 0; l < 3; l++)
  49.                     {
  50.                         DataRow row = table.NewRow();
  51.                         row[0] = m;
  52.                         row[1] = i;
  53.                         row[2] = j.ToString();
  54.                         row[3] = j + "_dept_" + k;
  55.                         row[4] = row[3] + "_person_" + l;
  56.                         table.Rows.Add(row);
  57.                         m++;
  58.                     }
  59.                 }
  60.             }
  61.         }
  62.         return table;
  63.     }
  64.     //从总数据源中获取girdview的绑定表
  65.     private DataTable GetInit()
  66.     {
  67.         if (Session["main"] == null)
  68.             return null;
  69.         else
  70.         {
  71.             DataTable mainTable = (DataTable)Session["main"];
  72.             DataTable table = new DataTable();
  73.             table.Columns.Add("id", typeof(int));
  74.             table.Columns.Add("rowid", typeof(int));
  75.             table.Columns.Add("year", typeof(string));
  76.             table.Columns.Add("dept", typeof(string));
  77.             table.Columns.Add("person", typeof(string));
  78.  
  79.             DataRow[] rows = mainTable.Select("person='2007_dept_0_person_0' and rowid=0");
  80.             table.Rows.Add(rows[0][0], rows[0][1], rows[0][2], rows[0][3], rows[0][4]);
  81.             rows = mainTable.Select("person='2007_dept_0_person_0' and rowid=1");
  82.             table.Rows.Add(rows[0][0], rows[0][1], rows[0][2], rows[0][3], rows[0][4]);
  83.             rows = mainTable.Select("person='2007_dept_0_person_0' and rowid=2");
  84.             table.Rows.Add(rows[0][0], rows[0][1], rows[0][2], rows[0][3], rows[0][4]);
  85.             return table;
  86.         }
  87.     }
  88.     protected void Page_Load(object sender, EventArgs e)
  89.     {
  90.         
  91.         if (!IsPostBack)
  92.         {
  93.             //创建数据源和绑定gridview
  94.             this.main = CreateMainData();
  95.             Session["main"] = main;
  96.             this.init = GetInit();
  97.             this.ASPxGridView1.DataSource = this.init.DefaultView;
  98.             this.ASPxGridView1.DataBind();
  99.         }
  100.         
  101.     }
  102.     //进入编辑状态的回调的初始化
  103.     protected void ASPxGridView1_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e)
  104.     {
  105.         if (Session["main"] == null)
  106.             return;
  107.         DataTable table = (DataTable)Session["main"];
  108.     //设置年的combobox的items
  109.         if (e.Column.FieldName == "year")
  110.         {
  111.             ASPxComboBox cboYear = e.Editor as ASPxComboBox;
  112.             cboYear.Items.Clear();
  113.             DataRow[] rows = null;
  114.             if (Session["year"] == null)
  115.             {
  116.                 rows = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex);
  117.                 Session["year"] = rows;
  118.             }
  119.             else
  120.                 rows = (DataRow[])Session["year"];
  121.             if (rows.Length < 1)
  122.             {
  123.                 cboYear.Text = "";
  124.                 return;
  125.             }
  126.             else
  127.             {
  128.         //是否是初始化
  129.                 if(!IsCallback)
  130.                     cboYear.Text = rows[0][2].ToString();
  131.                 
  132.             }
  133.             foreach (DataRow row in rows)
  134.             {
  135.                 if(cboYear.Items.IndexOfText(row[2].ToString()) < 0)
  136.                     cboYear.Items.Add(row[2].ToString());
  137.             }
  138.             //cboYear.SelectedIndex = cboYear.Items.IndexOfText(cboYear.Text);
  139.             cboYear.SelectedIndexChanged += new EventHandler(cboYear_SelectedIndexChanged);
  140.             cboYear.SelectedIndexChanged += new EventHandler(cboYear_SelectedIndexChanged);
  141.         }
  142.         //设置部门的combobox的items
  143.         else if (e.Column.FieldName == "dept")
  144.         {
  145.             ASPxComboBox cboDept = e.Editor as ASPxComboBox;
  146.             cboDept.Items.Clear();
  147.             DataRow[] rows = null;
  148.             if (Session["dept"] == null)
  149.             {
  150.                 rows = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + this.ASPxGridView1.GetRowValues(this.ASPxGridView1.EditingRowVisibleIndex, "year").ToString() + "'");
  151.                 Session["dept"] = rows;
  152.             }
  153.             else
  154.                 rows = (DataRow[])Session["dept"];
  155.             if (rows.Length < 1)
  156.             {
  157.                 cboDept.Text = "";
  158.                 return;
  159.             }
  160.             else
  161.             {
  162.         //如果不是“部门”的回传,则默认选择第一项
  163.                 if (!this.isDept)
  164.                     cboDept.Text = rows[0][3].ToString();
  165.             }
  166.             foreach (DataRow row in rows)
  167.             {
  168.                 if (cboDept.Items.IndexOfText(row[3].ToString()) < 0)
  169.                     cboDept.Items.Add(row[3].ToString());
  170.             }
  171.             //cboDept.SelectedIndex = cboDept.Items.IndexOfText(cboDept.Text);
  172.             cboDept.SelectedIndexChanged -= new EventHandler(cboDept_SelectedIndexChanged);
  173.             cboDept.SelectedIndexChanged += new EventHandler(cboDept_SelectedIndexChanged);
  174.             
  175.         }
  176.     //设置人员的combobox的items
  177.         else if (e.Column.FieldName == "person")
  178.         {
  179.             ASPxComboBox cboPerson = e.Editor as ASPxComboBox;
  180.             cboPerson.Items.Clear();
  181.             DataRow[] rows = null;
  182.             if (Session["person"] == null)
  183.             {
  184.                 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() + "'");
  185.                 Session["person"] = rows;
  186.             }
  187.             else
  188.                 rows = (DataRow[])Session["person"];
  189.             if (rows.Length < 1)
  190.             {
  191.                 cboPerson.Text = "";
  192.                 return;
  193.             }
  194.             else
  195.             {
  196.                 if (cboPerson.SelectedIndex >= 0)
  197.                     cboPerson.Text = cboPerson.SelectedItem.Text;
  198.                 else
  199.                     cboPerson.Text = rows[0][4].ToString();
  200.             }
  201.             foreach (DataRow row in rows)
  202.             {
  203.                 if (cboPerson.Items.IndexOfText(row[4].ToString()) < 0)
  204.                     cboPerson.Items.Add(row[4].ToString());
  205.             }
  206.             
  207.         }
  208.     }
  209.     //部门selectindexchanged事件
  210.     void cboDept_SelectedIndexChanged(object sender, EventArgs e)
  211.     {
  212.         this.isDept = true;
  213.         if (Session["main"] == null)
  214.             return;
  215.         DataTable table = (DataTable)Session["main"];
  216.         ASPxComboBox cboDept = (ASPxComboBox)sender;
  217.         DataRow[] deptrows = (DataRow[])Session["dept"];
  218.     //更新人员联动信息
  219.         Session["person"] = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + deptrows[0][2].ToString() + "' and dept='" + cboDept.SelectedItem.Text + "'");
  220.     }
  221.     //年份selectindexchanged事件
  222.     void cboYear_SelectedIndexChanged(object sender, EventArgs e)
  223.     {
  224.         this.isYear = true;
  225.         this.isDept = false;
  226.         if (Session["main"] == null)
  227.             return;
  228.         DataTable table = (DataTable)Session["main"];
  229.         ASPxComboBox cboYear = (ASPxComboBox)sender;
  230.         this.yearSelectindex = cboYear.SelectedIndex;
  231.     //更新部门联动信息
  232.         DataRow[] deptrows = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + cboYear.SelectedItem.Text + "'");
  233.         Session["dept"] = deptrows;
  234.     //更新人员联动信息
  235.         if (deptrows.Length > 0)
  236.             Session["person"] = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + cboYear.SelectedItem.Text + "' and dept='" + deptrows[0][3].ToString() + "'");
  237.         else
  238.             Session["person"] = new DataRow[] { };
  239.     }
  240.     //客户端回调事件
  241.     protected void ASPxGridView1_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
  242.     {
  243.     //如果是“年份”的回调,则修改相应的联动信息
  244.         if (e.Parameters.Equals("year"))
  245.         {
  246.             this.isYear = true;
  247.             this.isDept = false;
  248.             if (Session["main"] == null)
  249.                 return;
  250.             DataTable table = (DataTable)Session["main"];
  251.             DataRow[] yearrows = (DataRow[])Session["year"];
  252.             List<string> strings = new List<string>();
  253.             foreach (DataRow row in yearrows)
  254.             {
  255.                 if (strings.IndexOf(row[2].ToString()) < 0)
  256.                     strings.Add(row[2].ToString());
  257.             }
  258.             DataRow[] deptrows = new DataRow[] { };
  259.             if (yearrows.Length > 0)
  260.                 deptrows = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + strings[this.yearSelectindex] + "'");
  261.             Session["dept"] = deptrows;
  262.             if (deptrows.Length > 0)
  263.                 Session["person"] = table.Select("rowid=" + this.ASPxGridView1.EditingRowVisibleIndex + " and year='" + strings[this.yearSelectindex] + "' and dept='" + deptrows[0][3].ToString() + "'");
  264.             else
  265.                 Session["person"] = new DataRow[] { };
  266.         }
  267.  
  268.     }
  269.     //编辑状态切换
  270.     protected void ASPxGridView1_CancelRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
  271.     {
  272.         Session["year"] = null;
  273.         Session["dept"] = null;
  274.         Session["person"] = null;
  275.     }
  276. }

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

 

转载于:https://my.oschina.net/u/1267931/blog/718627

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值