简体   繁体   English

C# mysql 仅查看数据网格视图中的特定列

[英]C# mysql view only specific columns in data grid view

Short and probably easy question, I don't want to view in dataGridView columnID and few more columns as there is no use of viewing that info.简短且可能很简单的问题,我不想在 dataGridView columnID 和更多列中查看,因为查看该信息没有用处。

I have a clear code, the connection is open , the data loads fine and the comment code (btw most common solution in google) gives empty table with all columns (no rows).我有一个清晰的代码,连接打开,数据加载正常,注释代码(顺便说一句,谷歌中最常见的解决方案)给出了所有列(无行)的空表。

I tried so many things that I can't even list them there:(我尝试了很多东西,以至于我什至无法在其中列出它们:(

Any ideas?有任何想法吗?

DataTable SqlDataTable = new DataTable();
MySqlDataReader reader;

MySqlCommand sqlCommand = new MySqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "SELECT * FROM table_name";

reader = sqlCommand.ExecuteReader();

//while (reader.Read())
//{
//    string columnID = reader["columnID"].ToString();                        
//}

SqlDataTable.Load(reader);

reader.Close();
sqlConnection.Close();

DataGridView = SqlDataTable;

First when U want view data in DataGridView U should set the data source of it's like首先,当你想在 DataGridView 中查看数据时,你应该设置它的数据源就像

DataGridView.DataSource = yourDataTable

Second, if U want to hide DataGridView column, U most know the index of the column or the name and then you can use this code I assume the first column is ID So其次,如果你想隐藏 DataGridView 列,你最知道列的索引或名称然后你可以使用这个代码我假设第一列是 ID 所以

By Index按索引

DataGridView.Columns[0].Visible = false;

By Name按名字

DataGridView.Columns["ID"].Visible = false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM