How to hide the ID column in the data grid,

Posted by M3nhaq under C# on 12/2/2011 | Points: 10 | Views : 1503 | Status : [Member] | Replies : 7
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);
}
}





Responses

Posted by: Arefin on: 12/2/2011 [Member] Starter | Points: 25

Up
0
Down
Hi M3nhaq,

Your code is almost okay. Just put your ID column and set visible = false. When user clicks a column, then you will get ID value with corresponding column index.

Just try, if you face any problem let me inform.


Thanks,
Arefin

M3nhaq, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: M3nhaq on: 12/2/2011 [Member] Starter | Points: 25

Up
0
Down
Thanks Arefin , it helps.
I am able to hide country_code column on my form.
Kindly do one more favour. In my new code I am getting index of column click as I click on it. But, what I want to do is to get the country_code as I click its own record so I get further work on that code.
I guess it will appear like that int num = table["Cat"].Column["country_code"].Row[e.Rowindex];
Thanks in advance.

 public void showGrid()

{
OleDbConnection conn = new OleDbConnection(ConnString);
string sql = @"Select country_code, country_name from country";
//OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataAdapter da = new OleDbDataAdapter(sql,conn);
try
{
conn.Open();
dataGridView1.ColumnCount = 2;
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";
//to Hide the country_code Column
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[0].DataPropertyName = "country_code";
dataGridView1.Columns[1].DataPropertyName = "country_name";
conn.Close();
}
catch (Exception ex)
{
conn.Close();
MessageBox.Show(ex.Message);
}
}

private void buttonSave_Click(object sender, EventArgs e)
{
showGrid();
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show("ColumnIndex = " + e.ColumnIndex.ToString() + "\nRowIndex" + e.RowIndex.ToString());
}


M3nhaq, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 12/2/2011 [Member] Bronze | Points: 25

Up
0
Down
jst write:
table["Cat"].Rows[e.RowIndex].Cells["Contry_Code"].Value

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

M3nhaq, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: M3nhaq on: 12/2/2011 [Member] Starter | Points: 25

Up
0
Down
You mean this ???

 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

{
MessageBox.Show("ColumnIndex = " + e.ColumnIndex.ToString() + "\nRowIndex" + e.RowIndex.ToString());
int num = (int)ds.Tables["Cat"].Rows[e.RowIndex].Cells["Country_Code"].Value;
}

Its giving error....

M3nhaq, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 12/2/2011 [Member] Bronze | Points: 25

Up
0
Down
which error??

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

M3nhaq, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: M3nhaq on: 12/2/2011 [Member] Starter | Points: 25

Up
0
Down
Arefin and Hmanjarawala Thank you very much for help. I got the solution.
Thanks once again.... :)
This code return the dataGrid hidden cell value

int num = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);


Please give me one more tip. I want to use an event if user click on cell a message will display and no message should be display if I click on header to sort out the grid.

I try following triggers but it display an error as I click on column header.

 private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)


private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)



M3nhaq, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Varung on: 12/3/2011 [Member] Starter | Points: 25

Up
0
Down
dataGridView1_RowCommand(object sender,CommandEventargs e)
{
(label)lb=(label)dataGridView1.Rows[e.RowIndex].FindControl(id of ur label");
string id=lb.text;// you will get id here
}



G.Varun

M3nhaq, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response