先看效果:
下面公布代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ClientModel;
namespace ClientTools
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 定义下拉列表框
private ComboBox cmb_Temp = new ComboBox();
private void Form1_Load(object sender, EventArgs e)
{
// 绑定性别下拉列表框
BindSex();
BindData();
EmployeeInfo employeeInfo = new EmployeeInfo();
// this.dataGridViewX1.DataSource = null;
// this.dataGridViewX1.DataSource = ClientDAL.EmployeeDAL.GetEmployeeList(employeeInfo);
// 添加下拉列表框事件
cmb_Temp.SelectedIndexChanged += new EventHandler(cmb_Temp_SelectedIndexChanged);
// 设置下拉列表框不可见
cmb_Temp.Visible = false;
// 将下拉列表框加入到DataGridView控件中
this.dataGridViewX1.Controls.Add(cmb_Temp);
}
private void BindSex()
{
DataTable dtSex = new DataTable();
dtSex.Columns.Add("Value");
dtSex.Columns.Add("Name");
DataRow drSex;
drSex = dtSex.NewRow();
drSex[0] = "1";
drSex[1] = "男";
dtSex.Rows.Add(drSex);
drSex = dtSex.NewRow();
drSex[0] = "0";
drSex[1] = "女";
dtSex.Rows.Add(drSex);
cmb_Temp.ValueMember = "Value";
cmb_Temp.DisplayMember = "Name";
cmb_Temp.DataSource = dtSex;
cmb_Temp.DropDownStyle = ComboBoxStyle.DropDownList;
}
private void BindData()
{
DataTable dtData = new DataTable();
dtData.Columns.Add("ID");
dtData.Columns.Add("Name");
dtData.Columns.Add("Sex");
DataRow drData;
drData = dtData.NewRow();
drData[0] = 1;
drData[1] = "张三";
drData[2] = "1";
dtData.Rows.Add(drData);
drData = dtData.NewRow();
drData[0] = 2;
drData[1] = "李四";
drData[2] = "1";
dtData.Rows.Add(drData);
drData = dtData.NewRow();
drData[0] = 3;
drData[1] = "王五";
drData[2] = "1";
dtData.Rows.Add(drData);
drData = dtData.NewRow();
drData[0] = 4;
drData[1] = "小芳";
drData[2] = "0";
dtData.Rows.Add(drData);
drData = dtData.NewRow();
drData[0] = 5;
drData[1] = "小娟";
drData[2] = "0";
dtData.Rows.Add(drData);
drData = dtData.NewRow();
drData[0] = 6;
drData[1] = "赵六";
drData[2] = "1";
dtData.Rows.Add(drData);
this.dataGridViewX1.DataSource = dtData;
}
private void dataGridViewX1_CurrentCellChanged(object sender, EventArgs e)
{
try
{
if (this.dataGridViewX1.CurrentCell.ColumnIndex == 2)
{
Rectangle rect = dataGridViewX1.GetCellDisplayRectangle(dataGridViewX1.CurrentCell.ColumnIndex, dataGridViewX1.CurrentCell.RowIndex, false);
string sexValue = dataGridViewX1.CurrentCell.Value.ToString();
if (sexValue == "1")
{
cmb_Temp.Text = "男";
}
else
{
cmb_Temp.Text = "女";
}
cmb_Temp.Left = rect.Left;
cmb_Temp.Top = rect.Top;
cmb_Temp.Width = rect.Width;
cmb_Temp.Height = rect.Height;
cmb_Temp.Visible = true;
}
else
{
cmb_Temp.Visible = false;
}
}
catch
{
}
}
private void cmb_Temp_SelectedIndexChanged(object sender, EventArgs e)
{
if (((ComboBox)sender).Text == "男")
{
dataGridViewX1.CurrentCell.Value = "男";
dataGridViewX1.CurrentCell.Tag = "1";
}
else
{
dataGridViewX1.CurrentCell.Value = "女";
dataGridViewX1.CurrentCell.Tag = "0";
}
MessageBox.Show(((ComboBox)sender).Text);
}
private void dgv_User_Scroll(object sender, ScrollEventArgs e)
{
this.cmb_Temp.Visible = false;
}
private void dgv_User_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
this.cmb_Temp.Visible = false;
}
}
}
标签:Temp,drData,dataGridViewX1,cmb,datagridview,Add,男女,dtData,下拉框 From: https://blog.51cto.com/51souta/5805139