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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Arnold PetronaPosted Jul 11, 2008, 1:15 PM
Many Many Thanks My Friend
i will have more questions during the weekends.
Have a nice weekend
Satish KathiPosted Jul 11, 2008, 10:46 AM
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 PetronaPosted Jul 11, 2008, 6:50 AM
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 KathiPosted Jul 10, 2008, 10:22 AM
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 PetronaPosted Jul 9, 2008, 11:29 PM
Arnold PetronaPosted Jul 9, 2008, 11:07 PM
I will have during the week more questions
Thsnkd
Niradhip ChakrabortyPosted Jul 9, 2008, 5:56 PM
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
Satish KathiPosted Jul 9, 2008, 2:47 PM
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;Arnold PetronaPosted Jul 9, 2008, 2:29 PM
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 KathiPosted Jul 9, 2008, 11:52 AM
{
textBox1.Text = e.ColumnIndex.ToString();
}