Winforms Datagrid. How to retrieve single cell value after double click

I have a datagrid bound to a dataset that can be created in several ways based on user input.  Once it is displayed (it is readonly), I want the user to double click to select a row, and then retreive values from 2 cells to use as key to retrieve the data from the database.  How do I get the values of the cells?  And can the cells be hidden?
MGothelfAsked:
Who is Participating?
 
MuhammadAdilCommented:
Hello Dear

i think u want to get column values from selected row
so u can get values in that way

string column1 = this.GrdLibrayDB.CurrentRow.Cells["ColumnName"].Value; \\ColumnName= (Such As Customer ID)
string column2 = this.GrdLibrayDB.CurrentRow.Cells["ColumnName"].Value;

Hidding Specific Column

this.ColumnName.Visible = false;
0
 
Bob LearnedCommented:
1) .NET version?

2) Cells can't be hidden, but columns can.

3) Individual cell values:

   string cellValue = dataGrid1.Item[column, row].ToString();

Bob
0
 
MGothelfAuthor Commented:
I know how to tell the column that is clicked (dataGrid1.CurrentCell.ColumnIndex), but how do I know which row???
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Bob LearnedCommented:
1) .NET version?  2005?

2) DataGridView?

3) With 2003 and the DataGrid, the current column is dataGrid1.CurrentCell.ColumnNumber and row is dataGrid1.CurrentCell.RowNumber.

4) I am thinking now that you are using a DataGridView with 2005.

5) With 2005 and the DataGrid, the current column is dataGrid1.CurrentCell.ColumnIndex and row is dataGrid1.CurrentCell.RowIndex.

Bob
0
 
MGothelfAuthor Commented:
Yes I am using a DataGridView with 2005.

So I can get the row and column, but DataGridView does not have a definition for Item.  What is the equivalent of dataGrid1.Item[column, row].ToString();

0
 
Bob LearnedCommented:
Try it anyway, even if Intellisense doesn't show it.

Also try this:

 string cellValue = dataGrid1[column, row].Value;

or this:

  DataGridViewCell cell = dataGrid1[columnIndex, rowIndex];
  string cellValue = cell.Value;

Bob
0
 
MuhammadAdilCommented:
Hello Dear

GrdLibraryDB is my gridname u should change this according to ur grid
0
 
MGothelfAuthor Commented:
Thanks, your solution works fine, although it should read:

string column1 = this.GrdLibrayDB.CurrentRow.Cells["ColumnName"].Value.ToString();

MG
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.