| |

VerySource

 Forgot password?
 Register
Search
View: 916|Reply: 4

Ask a question about ComboBox?

[Copy link]

2

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2021-3-2 12:00:02
| Show all posts |Read mode
There is a drop-down form on the web page:
<select>
<option value="值">text</option>
</select>
What we can see is the text of the <option> item, select this item to get value="value".

The corresponding ComboBox control in C# Form is
What we see is the Items.Text property, but how is the value property assigned to each item?
Reply

Use magic Report

0

Threads

119

Posts

67.00

Credits

Newbie

Rank: 1

Credits
67.00

 China

Post time: 2021-3-2 13:00:01
| Show all posts
C# WinForm does not have this thing
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 Invalid IP Address

Post time: 2021-3-2 13:30:02
| Show all posts
this.cmbSno.DataSource=ds.Tables["student"];
this.cmbSno.DisplayMember="Sno";
this.cmbSno.ValueMember="Sno";
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 Singapore

Post time: 2021-3-2 13:45:01
| Show all posts
Just use this directly comboBox1.SelectedItem
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2021-3-2 14:00:01
| Show all posts
Specify DisplayMember and ValueMember when binding.

for example:

A table studentDetails, two fields (sno, sname), bound to Commbobox:

//Binding when FormLoad, display name
private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421");
            SqlDataAdapter sda = new SqlDataAdapter("select * from studentDetails", con);
            DataSet ds = new DataSet();
            sda.Fill(ds, "student");
            this.comboBox1.DataSource = ds.Tables["student"];
            this.comboBox1.DisplayMember = "sname";
            this.comboBox1.ValueMember = "sno";
        }
//Get the value of the currently selected item, that is, the student ID
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            this.textBox1.Text = this.comboBox1.SelectedValue.ToString();
        }
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list