[英]How to hide the ID column in the data grid but still being able to get it if the user clicks or sorts the record
我编写了以下代码来显示我在DataGrid中选择的字段。 我想做的是在DataGrid中隐藏COUNTRY表的ID列。 因此,它不应显示给用户。 但是,如果用户单击任何列或对DataGrid进行排序,我仍然希望获得COUNTRY表记录的ID。 请帮助我。 我该怎么办? 该代码缺少什么?
OleDbDataAdapter da;
DataSet ds;
public void showGrid()
{
OleDbConnection conn = new OleDbConnection(ConnString);
string sql = @"Select id, country_code, country_name , from country";
OleDbDataAdapter da = new OleDbDataAdapter(sql,conn);
try
{
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds, "Cat");
// Turn this off so column names do not come from data source
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Cat";
dataGridView1.Columns[0].HeaderText = "Code";
dataGridView1.Columns[1].HeaderText = "Name";
dataGridView1.Columns[0].Name = "country_code";
dataGridView1.Columns[1].Name = "country_name";
dataGridView1.Columns[0].DataPropertyName = "country_code" ;
dataGridView1.Columns[1].DataPropertyName = "country_name";
conn.Close();
}
catch (Exception ex)
{
conn.Close();
MessageBox.Show(ex.Message);
}
}
在Form1_Load方法中添加此行
dataGridView1.Columns[0].Visible = false;
如果这可以解决您的问题,请标记为答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.