Home » VB.Net

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?



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



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.




How to select Row by givieing cell value in datagridview 


please help me


In VB.Net I use DataGridView1.CurrentRow.Cells(0).Value to get the contents of the first column from the row that is currently selected (multiselect is disabled, so there can only be one row selected at a time).

I need something like this in C# but can't nail it with the code. I have to get the value from the first cell in the selected row because that cell contains the autoincrement value that is the primary key in a table in my database.


Hello Everybody.

I have bound a datatable to a datagridview .

My question is the following.

How can I get the proper datatable row by double clicking the datagridview row header.

I've already know that I can get the index of selected row in datagridview and loop it through by the data of corresponding datacollums to find out the proper row in datatable.

But I believe there must be another easy way.



Can anyone please send me links/examples for a drag and drop of an individual cell to another cell on a different datagridview?

I've looked at the thread 'Drag and drop from a datagridview to another' (http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/1099d9df-a75b-4e54-b995-ac6ed7e926f9/) but when i run the code nothing happens.

Alot of the examples I have seen are drag and drop for an entire row which is not what I am lookin for. Thanks!


VB.Net :

I want to set the column header font to bold for just the column that is sorted.  Is there a way to address a specific DGV Column Header Cell as opposed to the entire Header Row?

I had the same problem as others where the tablix header will not repeat on multiple pages of the report. 
I followed the instructions here http://blogs.msdn.com/robertbruckner/archive/2008/10/13/Repeat-Header-And-Visible-Fixed-Header-Table.aspx and it did not solve the problem.  After trying several scenarios, I found that it was not repeating because I have each header value in a rectangle which spans one column, rather than having each header value in it's own tablix column.

For instance, if we have fields CompanyID and CompanyName on the report, the repeating headers work if I have them in separate columns like this:

                              Tablix Column 1  |  Tablix Column 2
Tablix Header row    Company ID        |  CompanyName
Tablix Detail row      [CompanyID]      |  [CompanyName]

It DOES NOT work when it is designed like this:

                             Tablix Column 1
Tablix Header Row   Company ID    Company Name  (two textboxes in a rectangle)
Tablix Detail Row    [CompanyID]   [CompanyName]  (two textboxes in a rectangle)

Additional info
We have about 1000 reports designed this way that we originally designed in Reporting Services 2005.  We upgraded to 2008 and the reports are displayed just fine except for this issue with the header repeating on multiple pages.  Is there any way to resolve this WITHOUT having to redesign all of these reports?

Thank you,

I have a report RDL upgraded from SSRS 2005 to 2008. The report has a normal table with columns and rows, each column header text is dynamic and from a table and is variable length.

The column header textbox has Textalign set to Right and writingMode set to tb-rl.

I have set the column header text CanGrow flag to false, so that if a table column header text is too long for the length of the textbox, it will automatically warp the text into two lines.

The problem is when I view the report, the report does not seems to display correctly. Each column has been enlarged (width wise) with NO header text.
It is very difficult for me to explain in word, please see the pictures listed below.

This shows the report with header textbox CanGrow flag set to False.

This shows the report with header textbox CanGrow flag set to true.

This shows what the report should look and also what it looks in RS2005 now.

I have been told IE8 standard mode has issue on vertical text but the original RS2005 RDL file render the same report with RS2005 correctly on IE8 in IE7 Standards mode or IE8 Standards mode.
If I used the same RDL file on RS2008, the problem occured.
Also if I upgraded the RDL to RS2008 using VS2008, again the same problem occured.

BTW, we are not using report viewer web control to view the report, we use URL.

Please can someone help!



Hello All,

I am manually adding a rows to a datagrid view.

everytime the first row gets selected and turns dark blue in the background color.

my problem is sometimes I need to show red color on error. So I need to disable the default cell selection .

how do i make a setting so tat no row gets selected in the grid by default.




In VS2010 (VB) I have been primarily working with Linq to SQL database technology and really appreciate how easy it is to connect to my tables via a datacontext, databind Linq query results to a datagridview using a bindingsource, and detect and persist changes in records with datacontext.submitchanges().

What I don't like is the fact that when I bind a datagridview to a loosely-typed Linq query, the datagridview is not sortable.  I also miss the efficiency of being able to filter the data displayed on the datagridview using the rowfilter property which is available in a dataview object created from a strongly-typed dataset table, rather than a loosely-typed Linq query.

I'm sure there must be a way to persist row/cell changes from a datagridview which is bound to a dataview.  I would like to check for and persist table/database changes after every navigation through the datagridview rows.  Using Linq, calling db.SubmitChanges after every navigation seems to work fine.  But what if the databindings are as follows:

    bindingsource.datasource = dataset.table.asDataView
    datagridview.datasource = bindingsource

Thanks for any guidance.


Hi everyone,

i have a datagridview of four columns, i'd like to enable the selection of just one column and disable the rest.

how can i achieve that?



Hi everybody

i  use vs2k8, sql2k8, windows forms, vc#

i have a DataGridView bound

when i start to type in the fisrt row and cell, it enters in the edit mode and insert a new row below, when i finish with the row, it goes to the next row and the same procedure.

i want to go to the next row when i enter a certain value in the first cell of the row












When i enter 100, it goes to r1, c2 Ok





When i enter 110 i want it to go to r3, c1







i've tried almost everything:


dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex+1].Cells[0];

does not work.

dataGridView1.Rows[dataGridView1..CurrentCell.RowIndex+1].Selected = true;

does not work.


dataGridView1.CurrentCell = dataGridView1[0, dataGridView1.CurrentCell.RowIndex+1];

does not work.

((BindingSource)dataGridView1.DataSource).Position = dataGridView1.CurrentCell.RowIndex+1;



Any ideas?

thank you




(.NET 2.0 Framework)
In the DataGridView control, I have programmatically selected a row that's currently not displayed in the grid.  However, the grid does not seem to scroll to the selected row automatically, and there doesn't seem to be a way to scroll programmatically to any row, let alone the selected row.

Some details:
- my DataGridView is running under VirtualMode
- I first execute DataGridView.ClearSelection() then DataGridView.Rows[index].Selected = true
- I have traced the rows that aren't displayed and the pertinent properties are:
   DataGridViewRow.Visible = true
   DataGridViewRow.Displayed = false

I see no functions under DataGridView or DataGridViewRow to force the DataGridView to force a scroll.  Can anyone confirm this?  And is there a work around?

I have DataTable as a DataSource of DataGridView. I am using DataGridView for viewing data, sorting, adding, removing etc. DataGridView contains only few columns. All data can be viewed or manipulated by textboxes. I am not using databinding between DataGridView active row and textboxes. When some row in DataGridView is selected, data of this row can be manipulated by textboxes (again, I am not using databinding)

Scenario for adding new rows:
User clicks on button
New dataRow is added with default values into DataTable.
Selection in DataGridView is changed to newly added row
User can change row data by textboxes

I have working solution:

DataTable dataTable = (DataTable)_dataGridView1.DataSource;
DataRow row = dataTable.NewRow();
row[_columnId] = _idOfNewlyAddedRow;

  for( int i=0; i<_dataGridView1.Rows.Count; i++)
       if((( DataRowView)_dataGridView1.Rows[ i].DataBoundItem).Row == row)
           _dataGridViewCodes.Rows[ i].Selected = true;

I am able find out index of row in DataGridView based on DataRow in DataTable.
But as you can see it can be very slow in case of a lot of entries.

Is there any other possibility how to convert DataRow to appropriate index in DataGridView? Or only some kind of mapping function DataRow -> DataGridViewRow + DataGridView.Rows.IndexOf(...) is possible?

Thanks a lot


I thought this would be simple but doh !

I am using a data grid bound to my customers table in sql.

When i select a single record in the datagridview and send the id to my report viewer it runs ok.

If i select multiple rows I do not get any results returned even though i have a IN clause on my SQL statement

Further investigation shows that the value I am trying to pass is enclosed in quotation marks. e.g  "Value1,value2" instead of what i want "Value1","value2" etc...

I have tried various split(string) without any luck.

All help appreciated




Hello All,
Is there a way to keep the selected row in DataGridView always visible? I keep on inserting rows at random positions in the datagridview. But always want to make sure that the selected row is within the visible area of datagridview and always visible.

Any  code or sample will be helpful.

How can I programatically make a row in a DataGridView selected? The SelectedRows property is read only.



I have form that has a datagridview (datagridview1), one textbox and one button. This datagridview shows data from an excel database. My table looks something like this.

Order Number                      Customer         Completed

B2000                                     SKY                     NO

A2001                                      RR                      YES

AC205                                    SKY                      YES

AD568                                    NEW                     NO


Basically I will insert the order number into the Textbox and Click the Button. And I want the row of that order number to be highlighted in datagridview.

For Example, I type in AC205 in the textbox and click button and the 3rd row in the above table gets highlighted.


Thanks for you help




I will have a treenode with some nodes. I will have a datagridview on my form. Initially i will load some data in to the gridview. Now if i select a node at my trreview i would like to make a particular row as selected one.

Suppose my treeview is as follows

Root |-> Child |->Child1

If i select child i would like to make a corresponding row as selected if child1 another row should get selected.

Any idea please

how to select a row in datagridviewprogramatically???????
idea is to move the selection back to selected row after a page refresh....
i can set back the page position, but how to move the selection??!

I am creating an application to add,delete,update data in DataGrid view.

So i want to check if selected row is empty.

If it is empty it will show message to user that You can't insert a blank row..fill some data.

Same way you can't delte an empty row.

How should i check using C# programming.


<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure