winforms 如何在GridControl中隐藏不必要的列?

pieyvz9o  于 2022-12-14  发布在  其他
关注(0)|答案(1)|浏览(400)

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)可以使用DataTable吗?如果可能的话,请提供一个示例

tez616oj

tez616oj1#

为两个网格设置DataSource之后,可以将Id列的Visible属性设置为false。
假设DefaultView是GridView,则下列程式码应该可以完成这项工作。

(gridControl1.DefaultView as GridView).Columns["Id"].Visible = false;

相关问题