首页 > 代码库 > DataGirdView绑定多列combox联动

DataGirdView绑定多列combox联动

  1. /两个combox多行联动
  2.         private ComboBox cb1 = new ComboBox();
  3.         private ComboBox cb2 = new ComboBox();
  4.         //设置combox高度,解决默认修改combox.Height属性不能改变高度的问题
  5.         [System.Runtime.InteropServices.DllImport("user32.dll")]
  6.         private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
  7.         private const int CB_SETITEMHEIGHT = 0x153;
  8.         private bool SetComboBoxHeight(ComboBox cb, int height)
  9.         {
  10.             int rtn = SendMessage(cb.Handle, CB_SETITEMHEIGHT, -1, height);
  11.             cb.Refresh();
  12.             return rtn != -1;
  13.         }
  14.         public gvForm()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.         /// <summary>
  19.         /// 不连接数据库,临时创建table
  20.         /// </summary>
  21.         /// <param name="paramsCols">列名</param>
  22.         /// <param name="name">表名</param>
  23.         /// <param name="rowCount">行数</param>
  24.         /// <returns></returns>
  25.         private DataTable CreateDataTable(string[] paramsCols, string name, int rowCount)
  26.         {
  27.             DataTable table = new DataTable(name);
  28.             foreach (string s in paramsCols)
  29.                 table.Columns.Add(s, typeof(string));
  30.             for (int i = 0; i < rowCount; i++)
  31.             {
  32.                 DataRow row = table.NewRow();
  33.                 foreach (string s in paramsCols)
  34.                 {
  35.                     row[s] = s + "_" + i;
  36.                 }
  37.                 table.Rows.Add(row);
  38.             }
  39.             return table;
  40.         }
  41.         private void gvForm_Load(object sender, EventArgs e)
  42.         {
  43.             //创建整个datagridview的datasource
  44.             this.dataGridView1.DataSource = CreateDataTable(new[] { "ID", "Name", "CBOne", "CBTwo" }, "Main", 5);
  45.             this.cb1.Visible = false;
  46.             this.cb2.Visible = false;
  47.             //为combox添加selectindex消息响应
  48.             this.cb1.SelectedIndexChanged += new EventHandler(cb1_SelectedIndexChanged);
  49.             this.cb2.SelectedIndexChanged += new EventHandler(cb1_SelectedIndexChanged);
  50.             this.dataGridView1.Controls.Add(this.cb1);
  51.             this.dataGridView1.Controls.Add(this.cb2);
  52.         }
  53.         void cb1_SelectedIndexChanged(object sender, EventArgs e)
  54.         {
  55.             this.dataGridView1.CurrentCell.Value = ((ComboBox)sender).Text;
  56.         }
  57.         
  58.         /// <summary>
  59.         /// 即时设定combox的动态数据源
  60.         /// </summary>
  61.         /// <param name="sender"></param>
  62.         /// <param name="e"></param>
  63.         private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
  64.         {
  65.             if (this.dataGridView1.CurrentCell == null)
  66.             { 
  67.             }
  68.             //第一个combox,根据ID
  69.             else if (this.dataGridView1.CurrentCell.ColumnIndex == 2)
  70.             {
  71.                 this.cb1.Visible = false;
  72.                 this.cb2.Visible = false;
  73.                 string s = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "_Text";
  74.                 
  75.                 this.cb1.Items.Clear();
  76.                 DataTable cbt1 =  CreateDataTable(new[] { s }, "cb1", 5);
  77.                 foreach (DataRow row in cbt1.Rows)
  78.                 {
  79.                     this.cb1.Items.Add(row[s]);
  80.                     
  81.                 }
  82.                 this.cb1.DropDownStyle = ComboBoxStyle.DropDownList;
  83.                 this.cb1.SelectedIndex = 0;
  84.                 Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
  85.                 this.cb1.Left = rect.Left;
  86.                 this.cb1.Top = rect.Top;
  87.                 this.cb1.Width = rect.Width;
  88.                 //设定高度,这个-6不知什么原因,不过减6后才是正确的值
  89.                 this.SetComboBoxHeight(this.cb1, rect.Height - 6);
  90.                 this.cb1.Visible = true;
  91.             }
  92.             //第二个combox,根据第一个combox绑定相应的数据源
  93.             else if (this.dataGridView1.CurrentCell.ColumnIndex == 3)
  94.             {
  95.                 this.cb1.Visible = false;
  96.                 this.cb2.Visible = false;
  97.                 string s = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[2].Value.ToString() + "_L_Text";
  98.                 this.cb2.Items.Clear();
  99.                 DataTable cbt2 = CreateDataTable(new[] { s }, "cb2", 5);
  100.                 foreach (DataRow row in cbt2.Rows)
  101.                 {
  102.                     this.cb2.Items.Add(row[s]);
  103.                 }
  104.                 this.cb2.DropDownStyle = ComboBoxStyle.DropDownList;
  105.                 this.cb2.SelectedIndex = 0;
  106.                 Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
  107.                 this.cb2.Left = rect.Left;
  108.                 this.cb2.Top = rect.Top;
  109.                 this.cb2.Width = rect.Width;
  110.                 this.SetComboBoxHeight(this.cb2, rect.Height - 6);
  111.                 this.cb2.Visible = true;
  112.             }
  113.             else
  114.             {
  115.                 this.cb1.Visible = false;
  116.                 this.cb2.Visible = false;
  117.             }
  118.         }
  119.         //其他消息响应隐藏combox
  120.         private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
  121.         {
  122.             this.cb1.Visible = false;
  123.             this.cb2.Visible = false;
  124.         }
  125.         //改变列宽也改变combox的宽度
  126.         private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
  127.         {
  128.             if(e.Column.Name.Equals("CBOne"))
  129.             {
  130.                 Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
  131.                 this.cb1.Left = rect.Left;
  132.                 this.cb1.Top = rect.Top;
  133.                 this.cb1.Width = rect.Width;
  134.                 this.SetComboBoxHeight(this.cb1, rect.Height);
  135.             }
  136.             else if(e.Column.Name.Equals("CBTwo"))
  137.             {
  138.                 Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
  139.                 this.cb2.Left = rect.Left;
  140.                 this.cb2.Top = rect.Top;
  141.                 this.cb2.Width = rect.Width;
  142.                 this.SetComboBoxHeight(this.cb2, rect.Height - 6);
  143.             }
  144.         }
  145.         //改变行高也改变combox的高度
  146.         private void dataGridView1_RowHeightChanged(object sender, DataGridViewRowEventArgs e)
  147.         {
  148.             if (e.Row.Index == this.dataGridView1.CurrentCell.RowIndex)
  149.             {
  150.                 Rectangle rect1 = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
  151.                 this.cb1.Left = rect1.Left;
  152.                 this.cb1.Top = rect1.Top;
  153.                 this.cb1.Width = rect1.Width;
  154.                 this.SetComboBoxHeight(this.cb1, rect1.Height);
  155.                 Rectangle rect2 = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
  156.                 this.cb2.Left = rect2.Left;
  157.                 this.cb2.Top = rect2.Top;
  158.                 this.cb2.Width = rect2.Width;
  159.                 this.SetComboBoxHeight(this.cb2, rect2.Height - 6);
  160.             }
  161.         }
  162.     }

DataGirdView绑定多列combox联动