Good morning
I'm looking for a csharp code that i can use to use click on a column of the datagridview and use the selected item example interger ( columnheader Number) appears in a textbox1.text
Good morning
I'm looking for a csharp code that i can use to use click on a column of the datagridview and use the selected item example interger ( columnheader Number) appears in a textbox1.text
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satish KathiRank #8,369 · 223 reputationPosted Jul 9, 2008, 7:52 AM EDT
{
textBox1.Text = e.ColumnIndex.ToString();
}
Arnold Petrona156 reputationPosted Jul 9, 2008, 10:29 AM EDT
Thanks.
But the one you send me it will give me the index of the column. Let me give you an example
I have 3 Columns A, B & C with the following information in A i have a name , in B a lastname and in C the ID for example
When i click on one of the items in the row it will give me the Column C item( ID) in the textbox1.text
Satish KathiRank #8,369 · 223 reputationPosted Jul 9, 2008, 10:47 AM EDT
You can use any of these statements to get value in the selected cell
textBox1.Text = (string)dataGridView1[e.ColumnIndex, e.RowIndex].Value;
textBox1.Text = (string)dataGridView1.CurrentCell.Value;
textBox1.Text = (string)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
If you want only Id value when you selecet any column in a particular row, you can use, change the values in red font as per your requirement that is column(Id column) you are looking for
textBox1.Text = (
string)dataGridView1[columnIndex or columnName, e.RowIndex].Value;textBox1.Text =
(string)dataGridView1.Rows[e.RowIndex].Cells[coulmnIndex or columnName].Value;Niradhip ChakrabortyRank #429 · 5.8K reputationPosted Jul 9, 2008, 1:56 PM EDT
I have done this in datagrid of windows application.
Get the logic from the code and apply it in your code.
private void dtgUsers_Click(object sender, System.EventArgs e)
{
DataGridCell currentCell;
string currentCellData="";
// Get the current cell.
currentCell = dtgUsers.CurrentCell;
// Get the current cell's data.
currentCellData = (string)dtgUsers[currentCell.RowNumber,currentCell.ColumnNumber];
// Set the TextBox's text to that of the current cell.
textbox1.text = currentCellData;
}
Note: 'dtgUsers' id of datagrid
Arnold Petrona156 reputationPosted Jul 9, 2008, 7:07 PM EDT
I will have during the week more questions
Thsnkd
Arnold Petrona156 reputationPosted Jul 9, 2008, 7:29 PM EDT
Satish KathiRank #8,369 · 223 reputationPosted Jul 10, 2008, 6:22 AM EDT
It should work fine without error, can you send more code to analyze? What is the error you are getting?
You can also check the value returned before assigning to the TextBox.Text
If(dataGridView1[2, e.RowIndex].Value != null)
{
textBox1.Text = (string)dataGridView1[2, e.RowIndex].Value;
}
else
{
textBox1.Text = "";
}
Arnold Petrona156 reputationPosted Jul 11, 2008, 2:50 AM EDT
When I have this code:
private void dataGridView1_CellContntClick( object sender, DataGridViewCellEventArgs e)
if(dataGridView[2, e.RowIndex].Value !=null)
{ textBox2.Text = ( string)dataGidView[2,e.RowIndex].Value; }
else { MessageBox.Show(" Field is empty"); }
it give me the error ( Unable to cast object of type System.DBNull' to type System.String'.)
Satish KathiRank #8,369 · 223 reputationPosted Jul 11, 2008, 6:46 AM EDT
Also check for the DBNull Value
if(dataGridView[2, e.RowIndex].Value != null && dataGridView[2, e.RowIndex].Value != DBNull.Value)
{ textBox2.Text = ( string)dataGidView[2,e.RowIndex].Value; }
else { MessageBox.Show(" Field is empty"); }
Arnold Petrona156 reputationPosted Jul 11, 2008, 9:15 AM EDT
Many Many Thanks My Friend
i will have more questions during the weekends.
Have a nice weekend