Winform有2个网格。我将SQL Server数据库中的相关表显示给它们。
代码如下:
string connectionString = ConfigurationManager.ConnectionStrings["connectionSIPiT"].ConnectionString;
string command = "SELECT UserID, UserName,Login, idArm FROM Users";
string command2 = "SELECT id, name FROM arm";
sqlConnection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(command2, sqlConnection);
SqlDataAdapter adapter1 = new SqlDataAdapter(command, sqlConnection);
DataSet dataset1 = new DataSet();
adapter.Fill(dataset1, "arm");
adapter1.Fill(dataset1, "Users");
DataColumn keyColumn = dataset1.Tables[0].Columns[0];
DataColumn foreignKeyColumn = dataset1.Tables[1].Columns[3];
dataset1.Relations.Add("armUsers", keyColumn, foreignKeyColumn);
bindingSource1.DataSource = dataset1;
bindingSource1.DataMember = "arm";
bindingSource2.DataSource = bindingSource1;
bindingSource2.DataMember = "armUsers";
gridControl1.DataSource = bindingSource1;
gridControl2.DataSource = bindingSource2;
请帮我弄清楚如何在GridControl中隐藏不必要的列。如Id)可以使用数据表吗?如果可能的话,一个示例
在为两个网格设置DataSource之后,可以将Id列的Visible属性设置为false。
假设DefaultView是GridView,下面的代码应该可以完成这项工作。
(gridControl1.DefaultView as GridView).Columns["Id"].Visible = false;