- /两个combox多行联动
- private ComboBox cb1 = new ComboBox();
- private ComboBox cb2 = new ComboBox();
- //设置combox高度,解决默认修改combox.Height属性不能改变高度的问题
- [System.Runtime.InteropServices.DllImport("user32.dll")]
- private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
- private const int CB_SETITEMHEIGHT = 0x153;
- private bool SetComboBoxHeight(ComboBox cb, int height)
- {
- int rtn = SendMessage(cb.Handle, CB_SETITEMHEIGHT, -1, height);
- cb.Refresh();
- return rtn != -1;
- }
- public gvForm()
- {
- InitializeComponent();
- }
- /// <summary>
- /// 不连接数据库,临时创建table
- /// </summary>
- /// <param name="paramsCols">列名</param>
- /// <param name="name">表名</param>
- /// <param name="rowCount">行数</param>
- /// <returns></returns>
- private DataTable CreateDataTable(string[] paramsCols, string name, int rowCount)
- {
- DataTable table = new DataTable(name);
- foreach (string s in paramsCols)
- table.Columns.Add(s, typeof(string));
- for (int i = 0; i < rowCount; i++)
- {
- DataRow row = table.NewRow();
- foreach (string s in paramsCols)
- {
- row[s] = s + "_" + i;
- }
- table.Rows.Add(row);
- }
- return table;
- }
- private void gvForm_Load(object sender, EventArgs e)
- {
- //创建整个datagridview的datasource
- this.dataGridView1.DataSource = CreateDataTable(new[] { "ID", "Name", "CBOne", "CBTwo" }, "Main", 5);
- this.cb1.Visible = false;
- this.cb2.Visible = false;
- //为combox添加selectindex消息响应
- this.cb1.SelectedIndexChanged += new EventHandler(cb1_SelectedIndexChanged);
- this.cb2.SelectedIndexChanged += new EventHandler(cb1_SelectedIndexChanged);
- this.dataGridView1.Controls.Add(this.cb1);
- this.dataGridView1.Controls.Add(this.cb2);
- }
- void cb1_SelectedIndexChanged(object sender, EventArgs e)
- {
- this.dataGridView1.CurrentCell.Value = ((ComboBox)sender).Text;
- }
- /// <summary>
- /// 即时设定combox的动态数据源
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
- {
- if (this.dataGridView1.CurrentCell == null)
- {
- }
- //第一个combox,根据ID
- else if (this.dataGridView1.CurrentCell.ColumnIndex == 2)
- {
- this.cb1.Visible = false;
- this.cb2.Visible = false;
- string s = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "_Text";
- this.cb1.Items.Clear();
- DataTable cbt1 = CreateDataTable(new[] { s }, "cb1", 5);
- foreach (DataRow row in cbt1.Rows)
- {
- this.cb1.Items.Add(row[s]);
- }
- this.cb1.DropDownStyle = ComboBoxStyle.DropDownList;
- this.cb1.SelectedIndex = 0;
- Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
- this.cb1.Left = rect.Left;
- this.cb1.Top = rect.Top;
- this.cb1.Width = rect.Width;
- //设定高度,这个-6不知什么原因,不过减6后才是正确的值
- this.SetComboBoxHeight(this.cb1, rect.Height - 6);
- this.cb1.Visible = true;
- }
- //第二个combox,根据第一个combox绑定相应的数据源
- else if (this.dataGridView1.CurrentCell.ColumnIndex == 3)
- {
- this.cb1.Visible = false;
- this.cb2.Visible = false;
- string s = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[2].Value.ToString() + "_L_Text";
- this.cb2.Items.Clear();
- DataTable cbt2 = CreateDataTable(new[] { s }, "cb2", 5);
- foreach (DataRow row in cbt2.Rows)
- {
- this.cb2.Items.Add(row[s]);
- }
- this.cb2.DropDownStyle = ComboBoxStyle.DropDownList;
- this.cb2.SelectedIndex = 0;
- Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
- this.cb2.Left = rect.Left;
- this.cb2.Top = rect.Top;
- this.cb2.Width = rect.Width;
- this.SetComboBoxHeight(this.cb2, rect.Height - 6);
- this.cb2.Visible = true;
- }
- else
- {
- this.cb1.Visible = false;
- this.cb2.Visible = false;
- }
- }
- //其他消息响应隐藏combox
- private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
- {
- this.cb1.Visible = false;
- this.cb2.Visible = false;
- }
- //改变列宽也改变combox的宽度
- private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
- {
- if(e.Column.Name.Equals("CBOne"))
- {
- Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
- this.cb1.Left = rect.Left;
- this.cb1.Top = rect.Top;
- this.cb1.Width = rect.Width;
- this.SetComboBoxHeight(this.cb1, rect.Height);
- }
- else if(e.Column.Name.Equals("CBTwo"))
- {
- Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
- this.cb2.Left = rect.Left;
- this.cb2.Top = rect.Top;
- this.cb2.Width = rect.Width;
- this.SetComboBoxHeight(this.cb2, rect.Height - 6);
- }
- }
- //改变行高也改变combox的高度
- private void dataGridView1_RowHeightChanged(object sender, DataGridViewRowEventArgs e)
- {
- if (e.Row.Index == this.dataGridView1.CurrentCell.RowIndex)
- {
- Rectangle rect1 = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
- this.cb1.Left = rect1.Left;
- this.cb1.Top = rect1.Top;
- this.cb1.Width = rect1.Width;
- this.SetComboBoxHeight(this.cb1, rect1.Height);
- Rectangle rect2 = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
- this.cb2.Left = rect2.Left;
- this.cb2.Top = rect2.Top;
- this.cb2.Width = rect2.Width;
- this.SetComboBoxHeight(this.cb2, rect2.Height - 6);
- }
- }
- }
时间: 2024-12-29 12:41:29