I have write a following code to display the fields of my choice in dataGrid. What, I want to do is to hide the ID column of COUNTRY table in the dataGrid. So it should not appear to user. But, what else I want to do is to get the ID of COUNTRY table record if user click on any column. I able to get it if user click the record or sort the record Kindly help me, what should I need to do? what is missing in this code??
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);
}
}