我有一個表單數據綁定到一個客戶對象,其中一個字段是一個空的int表示一個「類型」。這顯示爲組合框,組合框綁定到「類型」表。如何將數據綁定對象的組合框字段綁定到數據源並仍然允許空值?
將具有空類型的客戶加載到窗體的數據源中時,組合框不顯示任何值,但是在單擊它時必須選擇一個值。表單/組合框永遠不會讓您變回空白項目(以在客戶對象上表示「null」)。
我不想在數據庫中使用「虛擬行」,目前通過添加一個虛擬對象並在提交事件(不乾淨!)中將其歸零。
是否可以做到這一點乾淨,保持可空主鍵?
我有一個表單數據綁定到一個客戶對象,其中一個字段是一個空的int表示一個「類型」。這顯示爲組合框,組合框綁定到「類型」表。如何將數據綁定對象的組合框字段綁定到數據源並仍然允許空值?
將具有空類型的客戶加載到窗體的數據源中時,組合框不顯示任何值,但是在單擊它時必須選擇一個值。表單/組合框永遠不會讓您變回空白項目(以在客戶對象上表示「null」)。
我不想在數據庫中使用「虛擬行」,目前通過添加一個虛擬對象並在提交事件(不乾淨!)中將其歸零。
是否可以做到這一點乾淨,保持可空主鍵?
用於綁定到類型組合的數據源可能還有其他1個條目,其中包含NULL值。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ TypeID Name ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 Business 2 Government -1 NULL
您可以爲沒有類型的客戶存儲-1(如果允許客戶不需要類型)。
關於爲空的數據綁定附加鏈接和資源:
我一直在說,到目前爲止,它已經搖了,你應該在你的數據庫的數據源之間有業務對象層和你的用戶界面,你可以只按照shahkalpesh的建議添加一個項目,而不用擔心它會進入數據庫。
Jez Humble在這個post and in comments的底部有一些關於綁定可空類型的信息如果「你顯式地將DataSourceUpdateMode設置爲DataSourceUpdateMode.OnPropertyChanged」,它建議綁定到可空類型是可行的。
在數據綁定可空類型的另一篇文章:代碼的樂趣 - Databinding and Nullable types in WinForms.NET
也許這個代碼結合可爲空的DateTimePicker可以幫助你找到這個或其他可爲空的問題addditional解決方案。
另外檢查出Dan Hannan的來源,我想出了我的擴展方法。
/// <summary>
/// From BReusable
/// </summary>
/// <param name="dtp"></param>
/// <param name="dataSource"></param>
/// <param name="valueMember"></param>
/// <remarks>With help from Dan Hanan at http://blogs.interknowlogy.com/danhanan/archive/2007/01/21/10847.aspx</remarks>
public static void BindNullableValue(this DateTimePicker dateTimePicker, BindingSource dataSource, String valueMember,bool showCheckBox)
{
var binding = new Binding("Value", dataSource, valueMember, true);
//OBJECT PROPERTY --> CONTROL VALUE
binding.Format += new ConvertEventHandler((sender, e) =>
{
Binding b = sender as Binding;
if (b != null)
{
DateTimePicker dtp = (binding.Control as DateTimePicker);
if (dtp != null)
{
if (e.Value == null)
{
dtp.ShowCheckBox = showCheckBox;
dtp.Checked = false;
// have to set e.Value to SOMETHING, since it's coming in as NULL
// if i set to DateTime.Today, and that's DIFFERENT than the control's current
// value, then it triggers a CHANGE to the value, which CHECKS the box (not ok)
// the trick - set e.Value to whatever value the control currently has.
// This does NOT cause a CHANGE, and the checkbox stays OFF.
e.Value = dtp.Value;
}
else
{
dtp.ShowCheckBox = showCheckBox;
dtp.Checked = true;
// leave e.Value unchanged - it's not null, so the DTP is fine with it.
}
}
}
});
// CONTROL VALUE --> OBJECT PROPERTY
binding.Parse += new ConvertEventHandler((sender, e) =>
{
// e.value is the formatted value coming from the control.
// we change it to be the value we want to stuff in the object.
Binding b = sender as Binding;
if (b != null)
{
DateTimePicker dtp = (b.Control as DateTimePicker);
if (dtp != null)
{
if (dtp.Checked == false)
{
dtp.ShowCheckBox = showCheckBox;
dtp.Checked = false;
e.Value = (Nullable<DateTime>)null;
}
else
{
DateTime val = Convert.ToDateTime(e.Value);
e.Value = val;
}
}
}
});
dateTimePicker.DataBindings.Add(binding);
}
如果您的主要目標是將組合恢復爲空白且值爲空,則只需在編輯行時按Ctrl + 0即可。
我花了兩天的瘋狂研究和心臟病發作的風險,只是爲了找到隱藏在某個小帖子中的信息。
也許你應該發佈鏈接到上述問題(在SO上)或網絡上的外部資源。 – Cerebrus 2009-05-27 17:18:44
只是一個建議,如何使你的「類型」的枚舉值爲「未指定」?我知道這不是一個答案,但你應該使用枚舉...... – BFree 2009-05-27 17:24:28
在某些情況下,枚舉可能是一個解決方案,但如果數據來自數據源,則不是。例如,考慮一款汽車的產品配置器 - 可能會有一個包含所有可用導航系統的組合框。在數據庫中,表Car將有一個可空的外鍵給表NavigationSystems。因此,如果用戶決定不購買導航系統,那麼您需要將組合框綁定到NavigationSystems表,但仍然允許null。這可能是一個解決方案,添加一個新的行「沒有導航系統」,並使外鍵不可爲空 - 但這也有缺點。 – 2009-05-27 17:33:13