WindowsForm的ComboBox初始化绑定并选中默认值

1.通过查询将默认值放到第一个,然后进行绑定

        //绑定线别
        private void LineBind()
        {
            SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
            DataTable myDataTable = new DataTable();

            string strLine = ConfigurationManager.AppSettings["Line"];
            SqlDataAdapter myDp = new SqlDataAdapter("select Title from AkLine where Title='" + strLine + "' union all select Title from AkLine where Title<>'" + strLine + "'", myConnection);
            myDp.Fill(myDataTable);

            myConnection.Dispose();
            myDp.Dispose();

            cbLine.DataSource = myDataTable;
            cbLine.DisplayMember = "Title";
            cbLine.ValueMember = "Title";
        }

 

2.控件中写死内容,通过下面的方式可以载入默认值

cbStation.SelectedIndex = cbStation.Items.IndexOf(ConfigurationManager.AppSettings["Station"]);

 

你可能感兴趣的:(combobox)