Hi @Farshad Valizade , Welcome to Microsoft Q&A.
The code you posted is limited, and your two example images are the same.
For your question, how to display relevant information according to the selected row, you can refer to the code below.
namespace WinFormsApp2
{
public partial class Form1 : Form
{
public class Province
{
public int Pid { get; set; }
public string? Name { get; set; }
}
public class City
{
public int Cid { get; set; }
public int Pid { get; set; }
public string? Name { get; set; }
}
private List<Province> provinces;
private List<City> cities;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadData();
comboBox1.DataSource = provinces;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Pid";
//
dataGridView1.DataSource = cities;
}
private void LoadData()
{
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
provinces = new List<Province>
{
new Province { Pid = 1, Name = "TX" },
new Province { Pid = 2, Name = "CA" },
new Province { Pid = 3, Name = "GG" }
};
cities = new List<City>
{
new City { Cid = 1, Pid = 1, Name = "CityName 1" },
new City { Cid = 2, Pid = 1, Name = "CityName 2" },
new City { Cid = 3, Pid = 2, Name = "CityName 3" },
new City { Cid = 4, Pid = 2, Name = "CityName 4" },
new City { Cid = 5, Pid = 3, Name = "CityName 5" },
new City { Cid = 6, Pid = 3, Name = "CityName 6" }
};
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int provinceId = ((City)dataGridView1.SelectedRows[0].DataBoundItem).Pid;
string cityName = ((City)dataGridView1.SelectedRows[0].DataBoundItem).Name;
comboBox1.SelectedValue = provinceId;
textBox1.Text = cityName;
}
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.