How to Set default combobox

前端 未结 5 1656
梦谈多话
梦谈多话 2021-02-19 00:48

So I\'ve been looking to set a default value for my combobox. I found a few things but none of them seem to work.

Actually, it works if I create a simple combobox and us

5条回答
  •  鱼传尺愫
    2021-02-19 01:23

    My solution:

    int? defaultID = null;
    foreach (DataRow dr in dataSource.Tables["DataTableName"].Rows)
    {
         if ((dr["Name"] != DBNull.Value) && ((string)dr["Name"] == "Default Name"))
         {
              defaultID = (int)dr["ID"];
         }
    }
    if (defaultID != null) comboBox.SelectedValue = defaultID;
    

提交回复
热议问题