Visual Studio 如何更改组合框的默认值

dvtswwa3  于 2022-11-17  发布在  其他
关注(0)|答案(2)|浏览(226)

我正在尝试基于所选项目创建SQL语句并抛出组合框。我希望将默认项目选为ID,但现在它返回NULL。我做错了什么?

private void Win_Shown(object sender, EventArgs e)
{
   myBox.SelectedValue = "ID";
   myBox.SelectedText = "ID";
   myBox.SelectedItem = "ID";

   myBox.Items.Add("ID");
   myBox.Items.Add("Name");
   myBox.Items.Add("Surname");
   myBox.Items.Add("Mobile"); 
}

Then in for SQL语句

MySQL.DisplayAndSearch("SELECT * FROM Data WHERE " + this.myBox.SelectedItem.ToString() + " LIKE '%" + txt_Search.Text + "%'", dataGridView1);

感谢您的帮助:)

xmjla07d

xmjla07d1#

您在插入前选择了项目:

private void Win_Shown(object sender, EventArgs e)
{
   // first, add the items   
   myBox.Items.Add("ID");
   myBox.Items.Add("Name");
   myBox.Items.Add("Surname");
   myBox.Items.Add("Mobile"); 

   // now select it
   myBox.SelectedItem = "ID";
}
ozxc1zmp

ozxc1zmp2#

你试过吗?

private void Win_Shown(object sender, EventArgs e)
    {
    
       myBox.Items.Add("ID");
       myBox.Items.Add("Name");
       myBox.Items.Add("Surname");
       myBox.Items.Add("Mobile"); 

// now select it
            myBox.SelectedIndex = myBox.FindString("Mobile");//id..etc
    }

相关问题