方法1
//private void BindGrid()
//{
//commonUtily.CustomizeDataGridView(dgvSblx);//格式化DataGridView
// dt = newHiway.Dms.BLL.PubModule.TPubManageBll().GetSblx_All();
//bs.DataSource = dt;
//dgvSblx.DataSource = bs;
//}
//private void BindCmb()
//{
//DataGridViewComboBoxColumn sjsblxmc = newDataGridViewComboBoxColumn();
// DataTabledt = new Hiway.Dms.BLL.PubModule.TPubManageBll().BindSblx();
//sjsblxmc.DataSource = dt;
//sjsblxmc.DisplayMember = "lxmc";
//sjsblxmc.ValueMember = "sblx";
//sjsblxmc.HeaderText = "上级设备类型名称";
//sjsblxmc.DisplayStyleForCurrentCellOnly = true;
//this.dgvSblx.Columns.Insert(3, sjsblxmc);
//}
//private DataGridViewComboBoxEditingControl dataGridViewComboBox =null;
////捕获事件
//private void dgvSblx_EditingControlShowing(object sender,DataGridViewEditingControlShowingEventArgs e)
//{
// if(e.Control is DataGridViewComboBoxEditingControl&&this.dgvSblx.CurrentCell.ColumnIndex == 3
//&&this.dgvSblx.CurrentCell.RowIndex != -1)
// {
//this.dataGridViewComboBox =(DataGridViewComboBoxEditingControl)e.Control;
////增加委托处理
//dataGridViewComboBox.SelectionChangeCommitted += newEventHandler(this.dataGridViewComboBox_SelectionChangeCommitted);
// }
//}
//private void dataGridViewComboBox_SelectionChangeCommitted(objectsender, EventArgs e)
//{
////处理业务
//MessageBox.Show(((ComboBox)sender).Text);
//}
//private void dgvSblx_CellEndEdit(object sender,DataGridViewCellEventArgs e)
//{
// if(this.dataGridViewComboBox != null)
// {
//dataGridViewComboBox.SelectionChangeCommitted -= newEventHandler(this.dataGridViewComboBox_SelectionChangeCommitted);
//this.dataGridViewComboBox = null;
// }
//}
方法2
// private ComboBox cmb_Temp = newComboBox();
// DataGridViewCheckBoxColumn chbValue = newDataGridViewCheckBoxColumn();
private void MainForm_Load(object sender,EventArgs e)
{
BindCmb();
BindData(); ;
dgvSblx.Columns[0].Visible = false;
BindCheckBox();
// 添加下拉列表框事件
cmb_Temp.SelectedIndexChanged += newEventHandler(cmb_Temp_SelectedIndexChanged);
this.dgvSblx.Controls.Add(cmb_Temp);
}
//绑定下拉列表框
private void BindCmb()
{
DataTable dtCmb = newHiway.Dms.BLL.PubModule.TPubManageBll().BindSblx();
cmb_Temp.ValueMember = "SBLXID";
cmb_Temp.DisplayMember = "LXMC";
cmb_Temp.DataSource = dtCmb;
cmb_Temp.DropDownStyle = ComboBoxStyle.DropDownList;
}
//绑定复选框
private void BindCheckBox() {
this.dgvSblx.Columns.Insert(1,chbValue);
this.dgvSblx.Columns[1].Name = "checked";
this.dgvSblx.Columns[1].HeaderText = "选择";
}
private void BindData()
{
DataTable dtData = newHiway.Dms.BLL.PubModule.TPubManageBll().GetSblx_All();
this.dgvSblx.DataSource = dtData;
this.dgvSblx.Columns[0].HeaderText = "设备类型信息";
this.dgvSblx.Columns[1].HeaderText = "上级设备名称";
this.dgvSblx.Columns[2].HeaderText = "设备名称";
this.dgvSblx.Columns[3].HeaderText = "备注";
}
//当用户选择的单元格移动到性别这一列时,我们要显示下拉列表框,添加如下事件
private void dgvSblx_CurrentCellChanged(object sender, EventArgse)
{
if (this.dgvSblx.SelectedRows != null&& this.dgvSblx.SelectedRows.Count!= 0)
{
return;
}
if (this.dgvSblx.CurrentCell != null)
{
if (this.dgvSblx.CurrentCell.RowIndex <this.dgvSblx.RowCount - 1)
{
if(!this.dgvSblx.Rows[this.dgvSblx.CurrentCell.RowIndex].ReadOnly)
{
if (this.dgvSblx.CurrentCell.ColumnIndex == 1)
{
Rectangle rect =dgvSblx.GetCellDisplayRectangle(dgvSblx.CurrentCell.ColumnIndex,dgvSblx.CurrentCell.RowIndex, false);
string val = Convert.ToString(dgvSblx.CurrentCell.Value);
cmb_Temp.Text = val;
cmb_Temp.Left = rect.Left;
cmb_Temp.Top = rect.Top;
cmb_Temp.Width = rect.Width;
cmb_Temp.Height = rect.Height;
cmb_Temp.Visible = true;
cmb_Temp.DroppedDown = true;
}
else
{
cmb_Temp.Visible = false;
}
}
}
}
}
// 滚动DataGridView时将下拉列表框设为不可见
private void dgvSblx_Scroll(object sender, ScrollEventArgs e)
{
}
// 改变DataGridView列宽时将下拉列表框设为不可见
private void dgvSblx_ColumnWidthChanged(object sender,DataGridViewColumnEventArgs e)
{
this.cmb_Temp.Visible = false;
}
//绑定数据表后将选定列中的每一单元格的Value和Tag属性(Tag为值文本,Value为显示文本)
private void dgvSblx_DataBindingComplete(object sender,DataGridViewBindingCompleteEventArgs e)
{
for (int i = 0; i < this.dgvSblx.Rows.Count - 1;i++)
{
if (dgvSblx.Rows[i].Cells[1].Value.ToString().Trim() != "")
{
dgvSblx.Rows[i].Cells[1].Tag =dgvSblx.Rows[i].Cells[1].Value.ToString();
string temp = dgvSblx.Rows[i].Cells[1].Tag.ToString().Trim();
if (temp != "" &&dgvSblx.Rows[i].Cells[1].ColumnIndex == 1)
{
DataTable temp_dt = newHiway.Dms.BLL.PubModule.TPubManageBll().GetSjsbmcBySjsblx_1(temp);
if (temp_dt.Rows.Count > 0)
{
dgvSblx.Rows[i].Cells[1].Value =temp_dt.Rows[0][0].ToString().Trim();
}
}
}
}
}
private void cmb_Temp_SelectedIndexChanged(object sender, EventArgse)
{
if (((ComboBox)sender).Text.Trim() != "")
{
dgvSblx.CurrentCell.Value = ((ComboBox)sender).Text.Trim();
}
}