Winform中DataGridView网格添加ComBoBox
2021-06-15 17:03
标签:字体 ems winform init loaddata component als 字段名 null 在工作中遇到需要在DataGridView网格中添加ComBoBox的需求.解决后记录 1 在窗体类中声明一个全局的变量ComBoBox,并在Form的Load事件中设置号ComBoBox的下拉选项 2 在DataGridView绑定完数据源之后,将ComBoBox添加到DataGridView中 3 编写DataGridView的CurrentCellChanged事件和ComBoBox的SelectedIndexChanged事件 完整代码如下: public partial class Form1 : Form private void Form1_Load(object sender, EventArgs e) private void InitCmb() private void cmb_SelectedIndexChanged(object sender, EventArgs e) private void LoadDataSource() DataRow dr = tb.NewRow(); dr = tb.NewRow(); this.dgv1.DataSource = tb; } private void dgv1_CurrentCellChanged(object sender, EventArgs e) } Winform中DataGridView网格添加ComBoBox 标签:字体 ems winform init loaddata component als 字段名 null 原文地址:https://www.cnblogs.com/lylongs/p/10361926.html
{
ComboBox cmb = new ComboBox();
public Form1()
{
InitializeComponent();
this.dgv1.AutoGenerateColumns = false;
}
{
LoadDataSource();
InitCmb();
this.dgv1.Controls.Add(cmb);
this.dgv1.CurrentCellChanged += new System.EventHandler(this.dgv1_CurrentCellChanged);
}
{
this.cmb.Items.Add("新增");
this.cmb.Items.Add("删除");
this.cmb.Items.Add("修改类型");
this.cmb.SelectedIndexChanged += new System.EventHandler(this.cmb_SelectedIndexChanged);
}
{
dgv1.CurrentCell.Value = cmb.Text;
}
{
DataTable tb = new DataTable();
tb.Columns.Add("序号", typeof(Int32));
tb.Columns.Add("字段名");
tb.Columns.Add("操作");
dr["序号"] = 1;
dr["字段名"] = "Name";
dr["操作"] = "";
tb.Rows.Add(dr);
dr["序号"] = 2;
dr["字段名"] = "Gender";
dr["操作"] = "";
tb.Rows.Add(dr);
{
if (dgv1.DataSource == null)
return;
DataTable tb = (DataTable)dgv1.DataSource;
DataGridViewCell currentCell = dgv1.CurrentCell;
if (currentCell != null && currentCell.RowIndex {
Rectangle rect = dgv1.GetCellDisplayRectangle(currentCell.ColumnIndex, currentCell.RowIndex, true);
cmb.Text = currentCell.Value.ToString();
cmb.Size = rect.Size;//其实这里设置它的Size,只有width有效,如果要Height有效,需要重写ComBoBox控件,系统自带的ComBoBox空间的高度只能通过字体大小来自动适应
cmb.Top = rect.Top;
cmb.Left = rect.Left;
cmb.Visible = true;
}
else
{
cmb.Visible = false;
}
}
文章标题:Winform中DataGridView网格添加ComBoBox
文章链接:http://soscw.com/index.php/essay/94212.html