0

I have a table called stock_department having this design:

| department_id | parent_id | code | name           |
| --------------------------------------------------|
| 1             | 0         | NULL | Admin          |
| 4             | 0         | NULL | Department     |
| 6             | 1         | NULL | Admin--1       |
| 7             | 4         | NULL | Sub Department | 

In which I'm inserting department and sub department while inserting sub department the department id is being inserted as parent id.

When I display all sub-department in data grid view on the column of parent department it shows id but I want to display the name instead of id.

enter image description here

What I am doing:

string query = "SELECT * FROM stock_department WHERE parent_id!=0";
            MySqlDataAdapter dataAdaptar = new MySqlDataAdapter(query, connection);
            DataTable table = new DataTable();
            dataAdaptar.Fill(table);


            //Set AutoGenerateColumns False
            dataGridView1.AutoGenerateColumns = false;

            dataGridView1.DataSource = null;

            //Set Columns Count
            dataGridView1.ColumnCount = 3;

            /* AUTO RESIZE COLUNM WIDTH STARTS*/
            this.dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            this.dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            this.dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            /* AUTO RESIZE COLUNM WIDTH EDNS*/

            //Add Columns
            dataGridView1.Columns[0].Name = "department_id";
            dataGridView1.Columns[0].DataPropertyName = "department_id";
            dataGridView1.Columns[0].HeaderText = "Id";
            dataGridView1.Columns[0].Visible = false;


            dataGridView1.Columns[1].Name = "name";
            dataGridView1.Columns[1].DataPropertyName = "name";
            dataGridView1.Columns[1].HeaderText = "Subdepartment Name";


            dataGridView1.Columns[2].Name = "parent_id";
            dataGridView1.Columns[2].DataPropertyName = "parent_id";
            dataGridView1.Columns[2].HeaderText = "Parent Name";

            dataGridView1.DataSource = table;

HERE IS ANOTHER QUERY

string selectQuery = "select name FROM stock_department WHERE department_id='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
            connection.Open();
            MySqlCommand command = new MySqlCommand(selectQuery, connection);
            MySqlDataReader reader = command.ExecuteReader();
            reader.Read();
            string srno = reader["name"].ToString();
            connection.Close();

i want to display this srno string on column dataGridView1.Columns[2].HeaderText = "Parent Name";

Paul Karam
  • 4,052
  • 8
  • 30
  • 53
Faiz Sandhi
  • 306
  • 2
  • 12

1 Answers1

0

What you need to do is join the table to itself.

Your query should look like this:

SELECT sd1.*, sd2.name as ParentName 
FROM stock_department sd1
INNER JOIN stock_department sd2
    ON sd1.parent_id = sd2.department_id
WHERE sd1.parent_id != 0

What we did in the query is select all the data that you need (where parent_id is not equal to 0) and we joined it with the table itself to select the name from the parent id.

Then in your C# Code, you replace this part:

dataGridView1.Columns[2].Name = "parent_id";
dataGridView1.Columns[2].DataPropertyName = "parent_id";
dataGridView1.Columns[2].HeaderText = "Parent Name";

With this:

dataGridView1.Columns[2].Name = "parent_id";
dataGridView1.Columns[2].DataPropertyName = "ParentName";
dataGridView1.Columns[2].HeaderText = "Parent Name";
Paul Karam
  • 4,052
  • 8
  • 30
  • 53
  • Glad I was able to help. Don't forget to accept an answer if it solved your problem. That way this post does not show up in searches for unresolved questions. – Paul Karam Feb 08 '18 at 07:42