user3421241
user3421241

Reputation: 57

Get an id from a dataGridView

Can anyone help me with this. I have a table and I want that when I click a cell in it to get the row id (which would be the first column). Actually this is part of a project I am trying to do in which I need to display all childs of a parent. So I was thinking to get this id from the parent and then iterate through all the childs and verify which have the same id as a foreign key and then show them . Please help me with some ideas. I can provide code if you can't understand my question

public AirlineReservation()
        {
            InitializeComponent();      
        }

        private void getData()
        {
            SqlDataAdapter parentDataAdapter = new SqlDataAdapter("select * from Airline", connection);
            parentDataAdapter.Fill(ds, "Airline");
            SqlDataAdapter childDataAdapter = new SqlDataAdapter("select * from Plane", connection);
            childDataAdapter.Fill(ds, "Plane");

            DataColumn parentColumn = ds.Tables["Airline"].Columns["airline_id"];
            DataColumn childColumn = ds.Tables["Plane"].Columns["airline_id"];

            rel = new DataRelation("PlaneAirline", parentColumn, childColumn);
            ds.Relations.Add(rel);

            parentBindingSource.DataSource = ds;
            parentBindingSource.DataMember = "Airline";
            childBindingSource.DataSource = parentBindingSource;
            childBindingSource.DataMember = "PlaneAirline";   
        }

        private void AirlineReservation_Load(object sender, EventArgs e)
        {
            parentDataGridView.DataSource = parentBindingSource;
            childDataGridView.DataSource = childBindingSource;
            getData();  
        }

        private void dg_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataTable airlines = ds.Tables["Airline"];
            foreach (DataRow airline in airlines.Rows) {
                Console.WriteLine("AirlineID = " + airline["airline_id"]);
                Console.WriteLine("AirlineName = " + airline["name"]);
                DataRow[] planes = airline.GetChildRows("PlaneAirline");
                Console.WriteLine("This airline has following planes:");
                foreach (DataRow plane in planes)
                {
                    Console.WriteLine("  PlaneID = " + plane["plane_id"]);
                }
            }           
        }

I have made the relation between the 2 tables and so far when I click a cell it shows me all the childs and their parents. But I want to show only the childs of the selected cell.

Upvotes: 0

Views: 1172

Answers (1)

devmb
devmb

Reputation: 805

DataGridViewCellEventArgs contains members to get the clicked row and column index. By that you can determine the clicked cell and get it's data. That should make you able to get the corresponding planes.

private void dg_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    Console.WriteLine("Clicked row: " + e.RowIndex);
    Console.WriteLine("Clicked column: " + e.ColumnIndex);
    Console.WriteLine("Cell value: " + dg.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
}

Upvotes: 1

Related Questions