C# 如何在一个Datagridview 的 某一个cell里面 绑定一个自定义的combobox

用CellEndEdit事件触发函数去响应cell值的改变.
combbox绑定的问题,我把datagridview的那一列定义成DataGridViewComboBoxColumn,然后再把指定cell强制转换为datagridviewcomboboxcell就可以绑定数据了.

 

// 你可以在DataGridView的EditingControlShowing事件中加入如下代码,来给你的ComboxBox加一个事件,在这个事件中处理绑定的问题。
private void variousCostDgv3_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control.GetType().Equals(typeof(DataGridViewComboBoxEditingControl)))
{
DataGridViewComboBoxEditingControl editingControl = e.Control as DataGridViewComboBoxEditingControl;
editingControl.DropDownClosed += new EventHandler(editingControl_DropDownClosed);
}
}

// 然后,在这个事件处理函数中做绑定的处理,我这段代码是动态的改变一个cell的值,并修改对齐方式。你可以改成绑定新Cell的操作。

private void editingControl_DropDownClosed(object sender, EventArgs e)
{
DataGridViewComboBoxEditingControl ctl = sender as DataGridViewComboBoxEditingControl;
DataRowView drv = this.variousCostDgv3.Rows[ctl.EditingControlRowIndex].DataBoundItem as DataRowView;
drv["unit_name"] = ctl.Text;
drv["VARIOUS_COST_UNIT"] = CommonDataManager.GetChargeUnitData().GetCD(ctl.Text);
if (CommonDataManager.GetChargeUnitData().GetCD(ctl.Text).Equals(EstimateCommon.CHARGE_UNIT_ALL))
{
drv["number"] = "-";
drv["PEOPLE_NUM"] = 1;
this.variousCostDgv3["numberCol3", ctl.EditingControlRowIndex].ReadOnly = true;
this.variousCostDgv3["numberCol3", ctl.EditingControlRowIndex].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
else
{
drv["number"] = drv["PEOPLE_NUM"];
if (EstimateCommon.ESTIMATE_TYPE_REAL.Equals(mDsForInternal.Tables["CZ_ESTIMATE"].Rows[0]["ESTIMATE_TYPE"].ToString()))
{
this.variousCostDgv3["numberCol3", ctl.EditingControlRowIndex].ReadOnly = false;
}
this.variousCostDgv3["numberCol3", ctl.EditingControlRowIndex].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
}
this.variousCostDgv3.Refresh();
}

你可能感兴趣的:(datagridview)