DataGridView内含ComboBox的⽤法(赋值,两个下拉框联
动,得到下拉框所取值)
1. 创建⼀个窗⼝
右边的部分即为⼀个datagridview表,表名为OrganNameChange(后续代码会⽤到);
表包含四列:“Organ Names” 为⽂本框,“Check”为勾选框,“Type”和“Rename”均为下拉框。
1.1  datagridview的基本设置
这⾥的datagridview的基本设置有:
表的前⼀列不显⽰(c#默认显⽰):RowHeadersVisible 设为true;
表的最后⼀⾏不显⽰:AllowUserToAddRows 设为 false;
整个表的颜⾊想要交替出现:AlternatingRowsDefaultCellStyle中的背景⾊设置;
表的只读属性:设置ReadOnly
1.2 表格的功能介绍
表格第⼀列:“Organ Names”需要从外部读⼊数据;
表格第⼆列:在“All”勾选后,所有第⼆列全部被勾选,同时下拉框清空;⼀旦第⼆列中的⼀个被取消勾选,“All”也取消勾选,同时这⼀个单元格所在的⾏“Type”被初始化,可以进⾏下拉操作;
表格第三列:“Type”被选择后,“Rename”根据“Type”中所选的内容进⾏初始化,同时可以进⾏下拉操作;
表格第四列:“Rename”被选择后,得到下拉框所选的内容,将该列的值存储。
2. 功能实现
2.1 界⾯初始化
界⾯在打开时,可对整个界⾯进⾏初始化,并注册事件,代码如下(datagridview名为OrganNameChange)
private void InitializeView()
{
All_check.Checked = true;
All_check_CheckedChanged(null, null);
this.OrganNameChange.EditingControlShowing -= new DataGridViewEditingControlShowingEventH
andler(OrganNameChange_EditingControlShowing);      this.OrganNameChange.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(OrganNameChange_EditingControlShowing);      this.OrganNameChange.CellClick -= new DataGridViewCellEventHandler(OrganNameChange_CellClick);
this.OrganNameChange.CellClick += new DataGridViewCellEventHandler(OrganNameChange_CellClick);
this.OrganNameChange.DataError += delegate(object sender, DataGridViewDataErrorEventArgs e){ };
}
typeof的用法其中加载了“All”的初始状态和“All”被勾选后的其他联动;剩余⼏个就是下拉框的⼀些事件了。
2.2  “All” checkbox的勾选事件
所有第⼆列全部被勾选,同时下拉框清空
private void All_check_CheckedChanged(object sender, EventArgs e)
{
if(All_check.Checked)
{
for(int k = 0; k < OrganNameChange.RowCount; k++)
{
OrganNameChange.Rows[k].Cells["Check"].Value = true;
DataGridViewComboBoxCell groupNames = (DataGridViewComboBoxCell)OrganNameChange.Rows[k].Cells["Type"];
DataGridViewComboBoxCell organNames = (DataGridViewComboBoxCell)OrganNameChange.Rows[k].Cells["Rename"];
groupNames.Items.Clear();
organNames.Items.Clear();
groupNames.ReadOnly = true;
organNames.ReadOnly = true;
}
}
}
2.3 每列CheckBox 的勾选状态事件
⼀旦第⼆列中的⼀个被取消勾选,“All”也取消勾选,同时这⼀个单元格所在的⾏“Type”被初始化,可以进⾏下拉操作
private void OrganNameChange_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int k = e.RowIndex;
DataGridViewCheckBoxCell check_cur = (DataGridViewCheckBoxCell)OrganNameChange.Rows[k].Cells["Check"];
DataGridViewComboBoxCell groupNames = (DataGridViewComboBoxCell)OrganNameChange.Rows[k].Cells["Type"];
DataGridViewComboBoxCell organNames = (DataGridViewComboBoxCell)OrganNameChange.Rows[k].Cells["Rename"];            if(e.ColumnIndex == 1) //checkBox改动
{
bool flag = Convert.ToBoolean(check_cur.EditedFormattedValue);
if(!flag)
{
All_check.Checked = false;
groupNames.ReadOnly = false;
organNames.ReadOnly = false;
foreach (TreeSave<OrganInfoModel> groupInfo in allOrganInfo.Nodes)
{
//从外部传⼊数据,改变下拉框的items
groupNames.Items.Add(groupInfo.Data.ItemName);
}
}
else
{
groupNames.Items.Clear();
organNames.Items.Clear();
groupNames.ReadOnly = true;
organNames.ReadOnly = true;
}
}
}
2.4 下拉框的选择和初始化
private void OrganNameChange_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if(OrganNameChange.CurrentCell != null && e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
{
//向⽤户显⽰⽤于编辑选定单元格的值中的控件
tempGroupName = e.Control as ComboBox;
//创建下拉框改变事件
(tempGroupName).SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
(tempGroupName).SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
}
}
/// <summary>
/// 这⾥可以开始写下拉框具体要做什么
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBox_SelectedIndexChanged(object sender,EventArgs e)
{
int k = OrganNameChange.CurrentCell.RowIndex;
DataGridViewComboBoxCell ReOrganNames = (DataGridViewComboBoxCell)OrganNameChange.Rows[k].Cells["Rename"];            if(OrganNameChange.CurrentCell.ColumnIndex == 2) //在Type栏选择后,对Rename栏初始化
{
ReOrganNames.Items.Clear();
foreach (TreeSave<OrganInfoModel> organInfo in allOrganInfo.Nodes[0].Nodes)
{
ReOrganNames.Items.Add(organInfo.Data.ItemName);
}
}
if(OrganNameChange.CurrentCell.ColumnIndex == 3) //在Rename栏中选中organname后
{
string curName = tempGroupName.Text.ToString();
OrganNameChange.CurrentCell.Value = curName;
standOrganNames[k] = curName; //把下拉框选中的值传⾄standOrganNames储存起来
}
}
/// <summary>
/
// datagridview中单元格被点击后的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OrganNameChange_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView tempDataGridView = sender as DataGridView;
DataGridViewComboBoxEditingControl ec = tempDataGridView.EditingControl as DataGridViewComboBoxEditingControl;
if(ec == null) //当前ComboBox不在编辑状态
{
SendKeys.Send("{F4}"); //展开当前ComboBoxCell
}
}
所有功能实现就到此结束了。
3. 实际效果

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。