use modifyCityUserComboBox.SelectedValue=userDto.GetIdCity();
C# Windows Form set value of selectedIndex comobox
-
18-07-2023 - |
Question
after searching on this site i can't find the correct solution and need help.
I'm trying to modify a field of an user (I simulate that user wants to change their city)
I have a table like this:
+---------+---------------------+
| id_city | name |
+---------+---------------------+
| 1 | India |
| 2 | Irland |
| 3 | France |
+---------+---------------------+
And when I launch the query and I get the result I need is like that:
+---------+---------------------+
| id_city | name |
+---------+---------------------+
| 3 | France |
| 1 | India |
| 2 | Irland |
+---------+---------------------+
And in my C# code when initialize the windows forms I fill the combobox:
modifyCityUserComboBox.ValueMember = "id_city";
modifyCityUserComboBox.DisplayMember = "name";
modifyCityUserComboBox.DataSource = dataTable;
Then I try to put in the combobox the city that user have, but in this part I have the problem. When I do:
modifyCityUserComboBox.SelectedIndex = userDto.GetIdCity();
(supose that India is the original city of the user and wants to change it)
I have this error = "Invalid argument= The value of '1' is not valid for 'SelectedIndex. Parameter name= SelectedIndex".
I've been discovered that combobox start selectedIndex in 0 and if I don't order the cities I only need do:
modifyCityUserComboBox.SelectedIndex = userDto.GetIdCity() - 1;
With this "trick" the IdCity it's 0 and combobox accept it but I don't want do it that way. I want show the cities ordered.
I've tried set True the Sort combobox property and not order the cities in the mysql query but I get the same error...
Anyone know how can I do it? Thanks!
Solution
OTHER TIPS
I don't know how did you set up your classes, but this is how I've done it and it works. First I insert all cities from database to ComboBox
list inside Form1
constructor. After that you can press button1
on Form1
to change the city depending on the User
inside button1_Click
event. Let me demonstrate you the Form1
:
Main method:
public Form1()
{
InitializeComponent();
List<DataTable> dataTable = new List<DataTable>();
string path = @"server=.\sqlexpress;database=Cities;integrated Security=True;";
using (SqlConnection con = new SqlConnection(path))
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "select id_city, name from city order by name";
try
{
con.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
dataTable.Add(new DataTable()
{
id_city = (int)dr["id_city"]
,name = dr["name"] as string
});
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
BindingSource bs = new BindingSource() { DataSource = dataTable };
comboBox1.ValueMember = "id_city";
comboBox1.DisplayMember = "name";
comboBox1.DataSource = bs;
}
DataTable class:
class DataTable
{
public int id_city { get; set; }
public string name { get; set; }
}
button1_Click event:
private void button1_Click(object sender, EventArgs e)
{
User user = new User("India");
var myarr = comboBox1.Items.OfType<DataTable>().ToArray();
foreach (var item in myarr)
{
if (item.name.Equals(user.city))
{
comboBox1.SelectedItem = item;
break;
}
}
}
User class:
class User
{
public string city { get; set; }
public User(string str)
{
city = str;
}
}
Feel free to insert more cities in your database and change the country of User
inside button1_Click
event. Modify it the way you like, hope this helps!