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. /// <summary>
  58. /// 即时设定combox的动态数据源
  59. /// </summary>
  60. /// <param name="sender"></param>
  61. /// <param name="e"></param>
  62. private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
  63. {
  64. if (this.dataGridView1.CurrentCell == null)
  65. {
  66. }
  67. //第一个combox,根据ID
  68. else if (this.dataGridView1.CurrentCell.ColumnIndex == 2)
  69. {
  70. this.cb1.Visible = false;
  71. this.cb2.Visible = false;
  72. string s = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString() + "_Text";
  73. this.cb1.Items.Clear();
  74. DataTable cbt1 =  CreateDataTable(new[] { s }, "cb1", 5);
  75. foreach (DataRow row in cbt1.Rows)
  76. {
  77. this.cb1.Items.Add(row[s]);
  78. }
  79. this.cb1.DropDownStyle = ComboBoxStyle.DropDownList;
  80. this.cb1.SelectedIndex = 0;
  81. Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
  82. this.cb1.Left = rect.Left;
  83. this.cb1.Top = rect.Top;
  84. this.cb1.Width = rect.Width;
  85. //设定高度,这个-6不知什么原因,不过减6后才是正确的值
  86. this.SetComboBoxHeight(this.cb1, rect.Height - 6);
  87. this.cb1.Visible = true;
  88. }
  89. //第二个combox,根据第一个combox绑定相应的数据源
  90. else if (this.dataGridView1.CurrentCell.ColumnIndex == 3)
  91. {
  92. this.cb1.Visible = false;
  93. this.cb2.Visible = false;
  94. string s = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[2].Value.ToString() + "_L_Text";
  95. this.cb2.Items.Clear();
  96. DataTable cbt2 = CreateDataTable(new[] { s }, "cb2", 5);
  97. foreach (DataRow row in cbt2.Rows)
  98. {
  99. this.cb2.Items.Add(row[s]);
  100. }
  101. this.cb2.DropDownStyle = ComboBoxStyle.DropDownList;
  102. this.cb2.SelectedIndex = 0;
  103. Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
  104. this.cb2.Left = rect.Left;
  105. this.cb2.Top = rect.Top;
  106. this.cb2.Width = rect.Width;
  107. this.SetComboBoxHeight(this.cb2, rect.Height - 6);
  108. this.cb2.Visible = true;
  109. }
  110. else
  111. {
  112. this.cb1.Visible = false;
  113. this.cb2.Visible = false;
  114. }
  115. }
  116. //其他消息响应隐藏combox
  117. private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
  118. {
  119. this.cb1.Visible = false;
  120. this.cb2.Visible = false;
  121. }
  122. //改变列宽也改变combox的宽度
  123. private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
  124. {
  125. if(e.Column.Name.Equals("CBOne"))
  126. {
  127. Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
  128. this.cb1.Left = rect.Left;
  129. this.cb1.Top = rect.Top;
  130. this.cb1.Width = rect.Width;
  131. this.SetComboBoxHeight(this.cb1, rect.Height);
  132. }
  133. else if(e.Column.Name.Equals("CBTwo"))
  134. {
  135. Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
  136. this.cb2.Left = rect.Left;
  137. this.cb2.Top = rect.Top;
  138. this.cb2.Width = rect.Width;
  139. this.SetComboBoxHeight(this.cb2, rect.Height - 6);
  140. }
  141. }
  142. //改变行高也改变combox的高度
  143. private void dataGridView1_RowHeightChanged(object sender, DataGridViewRowEventArgs e)
  144. {
  145. if (e.Row.Index == this.dataGridView1.CurrentCell.RowIndex)
  146. {
  147. Rectangle rect1 = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
  148. this.cb1.Left = rect1.Left;
  149. this.cb1.Top = rect1.Top;
  150. this.cb1.Width = rect1.Width;
  151. this.SetComboBoxHeight(this.cb1, rect1.Height);
  152. Rectangle rect2 = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
  153. this.cb2.Left = rect2.Left;
  154. this.cb2.Top = rect2.Top;
  155. this.cb2.Width = rect2.Width;
  156. this.SetComboBoxHeight(this.cb2, rect2.Height - 6);
  157. }
  158. }
  159. }
时间: 2024-08-25 15:33:44

DataGirdView绑定多列combox联动的相关文章

c# datagridview与DataSet绑定, 列与数据库表里面的列一一对应

参考代码1: 自己模拟出数据,并分别对dataGridView赋值. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Collections; names

DataGridView固定了列名,怎样将数据内容绑定在列上

留心驿站 原文 其实很简单,在DataGridView上右键选择编辑列,在数据一项中找到DataPropertyName,在里面写上对应的要绑定的数据中的字段名,即可 .比如:从数据库中选择的datatable的第一列的字段名为:"编号",则在DataPropertyName里写上"编号",显示是显示HeaderTest属性里的内容,DataPropertyName只是用于绑定. DataGridView的AutoSizeColumnsMode设置为Displaye

WPF DataGrid绑定及列居中

基本的数据绑定 把集合的字段(属性)绑定在DataGrid的Binding属性就能将数据绑定列表 1 public class CashItem { 2 public int Value { get; set; } 3 public int Count { get; set; } 4 public int Amount { get { return Value * Count; } } 5 } 1 var items = new List<CashItem>() { 2 new CashIte

EasyUI 中 DataGrid 控件 列 如何绑定对象中的属性

EasyUI 中 DataGrid 控件 是我们经常用到的控件之一, 但是 DataGrid 控件 在绑定显示列时却不支持对象属性绑定. 模型如下: public class Manager implements java.io.Serializable { private Integer id; private Role role; private String loginName; private String password; private int status; private Da

EasyUI combox实现联动

多的时间将被用于combox联动效应.一个选择combox的值自己主动出这值有关相应的其他信息,例如省市联动.最近,我刚刚会见了班似要求,随着EasyUI  combobox 控制完成.假设ASP.NET 里面DropDownList的话,那就非常easy了,一个SelectIndexChange事件再加一个AutoPostBack即可了,以下就是我实现的功能,事实上非常easy,可是对于像我这样刚接触EasyUI.而且对JQ不熟悉的人来说还是有点费神. 首先是数据库:为此我特地做了一个測试数据

C# DataGridView下DataGridViewComboBoxColumn二级联动

效果: 代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace DataGridViewDownComboBoxDemo { public partial class Form1 : Form {

C# C1TrueDBGrid控件如何加载图片列

表格中加载图片是很常见的功能,尤其是网页中,图片的展示更是随处可见.这个功能在bs中很容易就实现了: 前台代码: <asp:GridView ID="GridView1" runat="server"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Image ID="img1" ImageUrl='<%#Eval("img

Excel实现二级菜单联动

项目中需要导入一个Excel模板需要实现二级联动,现记录如下: 首先看一下原始数据,原始信息在一张工作表,第一行是省市名称,下面的若干行为对应省市下面的地名和区名.需要在另外一张工作表中A列和B列建立联动的二级下拉菜单. 2 首先,选中原始表的所有数据(包括多余的空白单元格),按F5或者Ctrl+G调出定位对话框.选择左下角的[定位条件]. 3 如下图,选则[常量],并点击[确定]按钮.这样,所有的非空单元格被选中.   选择功能区的[数据]-[有效性]-[根据所选内容创建].   由于标题在第

物联网平台设计心得:DateTimePicker实现选择联动

所谓的选择联动,就是指,当我DateTimePicker1选择2月4号的时候,我DateTimePicker2只能选择2月4号和2月5号两天,当然你可以自行规定要选择的日期.这在一些图表查询条件里面是很常用的一个功能.下面我们就来看看如何设计. DateTimePicker的选取与使用 在这里,我们使用的DateTimePicker是一个开源的组件,他的model名称为:ui.bootstrap.datetimepicker,我们可以去这个网址找到其相关的内容:http://dalelotts.