c #windows Form Datagrid视图

时间:2018-02-08 06:57:42

标签: c# winforms

我有一个名为stock_department的表,有这样的设计:

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

我在插入子部门时插入部门和子部门,部门ID作为父ID插入。

当我在父部门的列上显示数据网格视图中的所有子部门时,它显示id但我想显示名称而不是id。

enter image description here

我在做什么:

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;

这里是另一个查询

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();

我想在列dataGridView1.Columns[2].HeaderText = "Parent Name";

上显示此srno字符串

1 个答案:

答案 0 :(得分:0)

您需要做的是join表格本身。

您的查询应如下所示:

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

我们在查询中所做的是选择您需要的所有数据(其中parent_id不等于0),我们将其与表本身连接以从父ID中选择名称。

然后在您的C#代码中,替换此部分:

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

有了这个:

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