how to retrieve data from bounded DataGrid view in c#?

Farshad Valizade 501 Reputation points
2023-04-13T11:29:39.61+00:00

hi every body i have an application that have 3 tables : Province , City , in province I have all province id and name . in City I select Province and set a name for the city and save them in the database. My problem is in the City. in the city form I have a Combo box for Province and a binding source in this form and load data from province table and set to it. a text Box for city name for save city name. it works fine but when I want to select a row from DataGrid view and show related province and city in the related control it just put province in the province combo box . what sould I do?


        private void Form1_Load(object sender, EventArgs e)
        {
            City city = new City();
            Province province = new Province();

            comboBox1.DataSource = province.Getall();
            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "Pid";

            var result = province.Getall().Join(
                                                city.Getall() ,
                                                pr => pr.Pid ,
                                                ci=>ci.Pid ,
                                                (pr,ci) => new
                                                {
                                                    PID = pr.Pid,
                                                    ProvinceName = pr.Name ,
                                                    CityID = ci.Cid,
                                                    CityName = ci.Name
                                                });
            dataGridView1.DataSource = result.ToList();

        }

Capture

Capture

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-04-14T08:34:11.3933333+00:00

    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;
                }
            }
        }
    }
    

    enter image description here

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.