在DataGridView中创建下拉框,可直接选择备选值。源码已经上传,有需要可以下载参考。
真实显示效果如下图:
一. 先创建一个datagridview参数列表
ParaTb = new DataTable();
//添加列
DataColumn[] Col = new DataColumn[2];
for (ushort i = 0; i < Col.Length; i++)
Col[i] = new DataColumn();
Col[0].ColumnName = "参数名称";
Col[1].ColumnName = "参数值";
ParaTb.Columns.AddRange(Col);
二. 设置创建的参数列表显示效果
//设置行高
for (byte i = 0; i < dataGridView1.Rows.Count; i++)
dataGridView1.Rows[i].Height = 25;
//设置列宽
int wd = (dataGridView1.Width - 10) / 3;
dataGridView1.Columns[0].Width = wd * 2;
dataGridView1.Columns[1].Width = wd;
dataGridView1.Columns[0].ReadOnly = true;
dataGridView1.Columns[1].ReadOnly = false;
dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("黑体", 9);
dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.DefaultCellStyle.Font = new Font("黑体", 9);
dataGridView1.RowHeadersVisible = false;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AllowUserToResizeColumns = false;
dataGridView1.AllowUserToResizeRows = false;
dataGridView1.MultiSelect = false;
三. 创建一个Combox下拉框控件
cbbItemsList = new List<string>();
cbb = new ComboBox();
cbb.DropDownStyle = ComboBoxStyle.DropDown;
cbb.BackColor = Color.LightGreen;
cbb.ForeColor = Color.Black;
cbb.Visible = false;
四. 将创建的Combox控件加入列表中
dataGridView1.Controls.Add(cbb);
五. 设置Combox下拉框控件的事件
private void cbb_SelectIndexChanged(object sender, EventArgs e)
{
ComboBox cbb = sender as ComboBox;
cbb.Visible = false;
}
private void cbb_Leave(object sender, EventArgs e)
{
ComboBox cbb = sender as ComboBox;
cbb.Visible = false;
}
private void cbb_VisibleChanged(object sender, EventArgs e)
{
ComboBox cbb = sender as ComboBox;
if (cbb.Visible == false)
{
if (dataGridView1.Contains(cbb))
dataGridView1.CurrentCell.Value = ((ComboBox)sender).Text;
else
dataGridView1.CurrentCell.Value = ((ComboBox)sender).Text;
}
}
private void cbb_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D0 || e.KeyCode == Keys.NumPad0)
{
ComboBox cbb = sender as ComboBox;
cbb.Visible = true;
cbb.Focus();
if (cbb.Text.Contains("*"))
{
cbb.Text = "";
cbb.SelectionStart = 0;
}
else
{
cbb.SelectionStart = cbb.Text.Length;
}
}
}
六. 双击列表时,弹出Combox下拉框
Rectangle rectangle = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, false);
string value = dataGridView1.CurrentCell.Value.ToString();
cbb.Text = value;
cbb.Left = rectangle.Left;
cbb.Top = rectangle.Top;
cbb.Width = rectangle.Width;
cbb.Height = rectangle.Height;
cbb.Items.Clear();
cbbItemsList.Clear();
int rowIndex = e.RowIndex;
if (dataGridView1[0, rowIndex].Value.ToString().Contains("速度"))
{
cbbItemsList.Add("1000");
cbbItemsList.Add("2000");
cbbItemsList.Add("3000");
cbbItemsList.Add("5000");
cbb.Items.AddRange(cbbItemsList.ToArray());
cbb.Visible = true;
cbb.Focus();
}