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.
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";