Home » VB.NetRSS

DataGridView - Need to know Col. 0 value of row and the col. header value for the select cell - How

I'm using VB 2005 and a DataGridView which has PromiseDate in first column and ProductTypes in the remaining columns that contain a count of past due items for the date that is in column 0 for that row.

What I want to happen is when a Cell is clicked, I want get the Date from Col. 0 for the row of the clicked cell and the value of the column header for the clicked cell (product) so I can then use them in a query to show the user the orders that make up the count in that cell.

I have the grid set to "dgvPastDueCnt.SelectionMode = DataGridViewSelectionMode.CellSelect" and take it I need code put in the "dgvPastDueCnt_CellClick" event. 

How do I get the Row/Column(0) and Column Header value for the selected Cell?

Thanks.

 

6 Answers Found

 

Answer 1

Try something like this:

'MAKE SURE THERE IS A CELL SELECTEDIf DataGridView1.SelectedCells.Count <> 0 Then'GET THE SELECTED CELLDim SelectedCell = DataGridView1.SelectedCells(0)

      'GET THE COLUMN HEADER TEXT FOR SELECTED CELLDim ColumnHeaderText = SelectedCell.OwningColumn.HeaderText

      'GET THE DATE FROM COLUMN 0 OF SELECTED CELL ROWDim PromiseDate = CDate(SelectedCell.OwningRow.Cells(0).Value)

      'OR AS A STRING'Dim PromiseDate = SelectedCell.OwningRow.Cells(0).value.ToStringEndIf
 

Answer 2

Thanks for the response.  I'm still having trouble figuring out the syntax.  I get the blue error line under SelectedCell.OwningColumn with message "OwningColumn is not a member of 'String'" and under SelectCell.OwningRow has "OwningRow is not a member of 'String'".  Any idea how I fix that?

Thanks - here is the code I have:

 

 

 

PrivateSub dgvPastDueCnt_CellClick(ByVal sender AsObject, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPastDueCnt.CellClick

    'MAKE SURE THERE IS A CELL SELECTEDIf dgvPastDueCnt.SelectedCells.Count <> 0 Then'GET THE SELECTED CELLDim SelectedCell AsString = dgvPastDueCnt.SelectedCells(0).ToString

      'GET THE COLUMN HEADER TEXT FOR SELECTED CELLDim ColumnHeaderText AsString = SelectedCell.OwningColumn.HeaderText

      ' Tried this, but not valid either 'Dim ColumnHeaderText As String = dgvPastDueCnt.DataGridViewCell.OwningColumn.HeaderText'GET THE DATE FROM COLUMN 0 OF SELECTED CELL ROWDim PromiseDate AsString = SelectedCell.OwningRow.Cells(0).value.ToString

    EndIfEndSub

 

Answer 3

My code says:

Dim SelectedCell = DataGridView1.SelectedCells(0)

Your code says:

Dim SelectedCell AsString = dgvPastDueCnt.SelectedCells(0).ToString

You are specifically converting the cell  to a string value, which is why you can't access the OwningColumn member, it is not a member of the string class.

 

Answer 4

Thanks for thr reply.

I added the "AS String" to SelectedCell and ColumnHeaderText because if I don't they give me the error "Option Strict On requires all variable declarations to have an 'AS' clause". 

Is there a different way I should have it because I have "Option Strict ON"?

Thanks for your help.

 

Answer 5

In VB2005 you need to specify all types. In 2008/2010, there is a feature called type inference which allows you to leave off the declaration type, and still use option strict and get static typing.

Anyway, just delcare SelectedCell as the actual type it is.

Declare SelectedCell as a DataGridViewCell, and you won't have the problem.

 

 

Answer 6

Works great now!   Thanks for taking the time to help me with this.  I really appreciate it.

Thanks

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter