我已經搜索並嘗試過,但無法找到正確的答案來滿足我的需要。從數據表填充數據到組合框
在我的應用程序我有組合框我想從數據庫表列leavetype添加數據到它。
table hsettings
hsysid
leavetype
martialstatus
現在我用鼠標點擊事件,
private void txtleavetype_MouseClick(object sender, MouseEventArgs e)
{
try
{
con = new SqlConnection(cs.DBConn);
con.Open();
cmd = new SqlCommand(" select leavestype from hrsettings", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
txtleavetype.Items.Add(dr["leavestype"]);
}
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
每當我點擊下拉框它表明從表列,但對數據的每一次點擊加載多次同列數據。
1
2
3
4
1
2
3
4
我希望它每次點擊加載一次,或者如果有任何其他更好的方法來整理它。
如何在加載頁面時加載comboBox? –
@非幸運感謝您的回覆。 – Bukhalifa