Form1.cs
上传用户:b2s168
上传日期:2021-04-20
资源大小:45k
文件大小:3k
源码类别:

组合框控件

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace CombEdit
  9. {
  10.     public partial class Form1 : Form
  11.     {
  12.         public Form1()
  13.         {
  14.             InitializeComponent();
  15.         }
  16.         private void Form1_Load(object sender, EventArgs e)
  17.         {
  18.             DataTable dtbl = new DataTable();
  19.             dtbl.Columns.Add("Show");
  20.             dtbl.Columns.Add("Value");
  21.             DataRow drow=dtbl.Rows.Add();
  22.             drow["Show"] = "销售部";
  23.             drow["Value"] = "001";
  24.             drow = dtbl.Rows.Add();
  25.             drow["Show"] = "人事部";
  26.             drow["Value"] = "002";
  27.             DataGridViewComboBoxColumn cmb0 = new DataGridViewComboBoxColumn();
  28.             cmb0.Name = "cmb0";
  29.             cmb0.DisplayMember = "Show";
  30.             cmb0.ValueMember = "Value";
  31.             cmb0.DataSource = dtbl;
  32.             dataGridView1.Columns.Add(cmb0);
  33.             dataGridView1.Columns["cmb0"].HeaderText = "系统";
  34.             DataGridViewComboBoxExColumn cmb1 = new DataGridViewComboBoxExColumn();
  35.             cmb1.Name = "cmb1";
  36.             cmb1.DisplayMember = "Show";
  37.             cmb1.ValueMember = "Value";
  38.             cmb1.DataSource = dtbl;
  39.             dataGridView1.Columns.Add(cmb1);
  40.             dataGridView1.Columns["cmb1"].HeaderText = "自定义1";
  41.             DataGridViewComboEditBoxColumn cmb2 = new DataGridViewComboEditBoxColumn();
  42.             cmb2.Name = "cmb2";
  43.             cmb2.DisplayMember = "Show";
  44.             cmb2.ValueMember = "Value";
  45.             cmb2.DataSource = dtbl; 
  46.             dataGridView1.Columns.Add(cmb2);
  47.             dataGridView1.Columns["cmb2"].HeaderText = "自定义2";
  48.             DataGridViewComboEditBoxColumn cmb3 = new DataGridViewComboEditBoxColumn();
  49.             cmb3.Name = "cmb3";
  50.             cmb3.Items.Add("自动添加");
  51.             dataGridView1.Columns.Add(cmb3);
  52.             dataGridView1.Columns["cmb3"].HeaderText = "自动添加";
  53.         }
  54.         private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
  55.         {
  56.             if (e.Control is DataGridViewComboBoxEditingControl)
  57.             {
  58.                 DataGridView dgv = (DataGridView)sender;
  59.                 if (dgv.CurrentCell.OwningColumn.Name == "cmb0")
  60.                 {
  61.                     DataGridViewComboBoxEditingControl cb = (DataGridViewComboBoxEditingControl)e.Control;
  62.                     cb.DropDownStyle = ComboBoxStyle.DropDown;
  63.                 }
  64.             }
  65.         }
  66.     }
  67. }