Form1.cs
资源名称:CombEdit.rar [点击查看]
上传用户:b2s168
上传日期:2021-04-20
资源大小:45k
文件大小:3k
源码类别:
组合框控件
开发平台:
Visual C++
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace CombEdit
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- DataTable dtbl = new DataTable();
- dtbl.Columns.Add("Show");
- dtbl.Columns.Add("Value");
- DataRow drow=dtbl.Rows.Add();
- drow["Show"] = "销售部";
- drow["Value"] = "001";
- drow = dtbl.Rows.Add();
- drow["Show"] = "人事部";
- drow["Value"] = "002";
- DataGridViewComboBoxColumn cmb0 = new DataGridViewComboBoxColumn();
- cmb0.Name = "cmb0";
- cmb0.DisplayMember = "Show";
- cmb0.ValueMember = "Value";
- cmb0.DataSource = dtbl;
- dataGridView1.Columns.Add(cmb0);
- dataGridView1.Columns["cmb0"].HeaderText = "系统";
- DataGridViewComboBoxExColumn cmb1 = new DataGridViewComboBoxExColumn();
- cmb1.Name = "cmb1";
- cmb1.DisplayMember = "Show";
- cmb1.ValueMember = "Value";
- cmb1.DataSource = dtbl;
- dataGridView1.Columns.Add(cmb1);
- dataGridView1.Columns["cmb1"].HeaderText = "自定义1";
- DataGridViewComboEditBoxColumn cmb2 = new DataGridViewComboEditBoxColumn();
- cmb2.Name = "cmb2";
- cmb2.DisplayMember = "Show";
- cmb2.ValueMember = "Value";
- cmb2.DataSource = dtbl;
- dataGridView1.Columns.Add(cmb2);
- dataGridView1.Columns["cmb2"].HeaderText = "自定义2";
- DataGridViewComboEditBoxColumn cmb3 = new DataGridViewComboEditBoxColumn();
- cmb3.Name = "cmb3";
- cmb3.Items.Add("自动添加");
- dataGridView1.Columns.Add(cmb3);
- dataGridView1.Columns["cmb3"].HeaderText = "自动添加";
- }
- private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
- {
- if (e.Control is DataGridViewComboBoxEditingControl)
- {
- DataGridView dgv = (DataGridView)sender;
- if (dgv.CurrentCell.OwningColumn.Name == "cmb0")
- {
- DataGridViewComboBoxEditingControl cb = (DataGridViewComboBoxEditingControl)e.Control;
- cb.DropDownStyle = ComboBoxStyle.DropDown;
- }
- }
- }
- }
- }