/// <summary>
/// 通用combox绑定
/// </summary>
/// <param name="cb"></param>
/// <param name="dt"></param>
/// <param name="strText"></param>
/// <param name="strValue"></param>
private void ComboDataBind( ComboBox cb,DataTable dt,string strText,string strValue)
{
if (0 < dt.Rows.Count)
{
cb.DisplayMember = strText;
cb.ValueMember = strValue;
cb.DataSource = dt.DefaultView;
cb.SelectedIndex = 0;
}
}
在form加载时上面的绑定是木有问题的,但是如果要两个combox关联着去绑定第二个,上面代码就会爆出“无法绑定到新的显示成员。 参数名: newDisplayMember”的错误,,,让我很是头疼+蛋碎,,,经历一番海查资料后,在回传的时候是要这样绑定的
private void PostBackComboDataBind(ComboBox cb, DataTable dt, string strText, string strValue) { if (0 < dt.Rows.Count) { cb.DataSource = dt.DefaultView; cb.DisplayMember = strText; cb.ValueMember = strValue; cb.SelectedIndex = 0; } }