Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access invisible columns in a Datagridview (WinForms)

I'm showing some data in a DataGridView using a list which I get from the Entity Framework. In this grid I set some database columns like the id to invisible.

When the user clicks on the gridview I need to know which object was clicked for further steps, the problem I cannot get the id column, neither through:

datagridview1.CurrentRow.Cells[0].Value // here I get only visible cells

nor through:

datagridview1.CurrentRow.DataBoundItem 

It seems that through setting some columns to invisible the objects attached have anonymous types

Any ideas?

Thank you

like image 640
CloudyMarble Avatar asked Jun 26 '12 06:06

CloudyMarble


People also ask

How to hide columns in the Windows Forms datagridview?

In the DataGridView control, the Visible property value of a column determines whether that column is displayed. There is support for this task in Visual Studio. Also see How to: Hide Columns in the Windows Forms DataGridView Control Using the Designer. Set the DataGridViewColumn.Visible property to false.

When do users refer to a single column in a datagridview?

When users view data displayed in a Windows Forms DataGridView control, they sometimes need to refer to a single column or set of columns frequently.

Which datagridview control contains a column named CUSTOMERID?

A DataGridView control named dataGridView1 that contains a column named CustomerID. References to the System and System.Windows.Forms assemblies. Basic Column, Row, and Cell Features in the Windows Forms DataGridView Control

How do I access cells in radgridview?

Download free 30-day trial Cells can be accessed by index or the column Name property. RadGridView uses virtualization for its visual elements. This means that only the rows that are currently visible have a visual element. When the grid is scrolled up and down the visual elements are reused.


Video Answer


2 Answers

I just tried this:

private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    var value = dataGridView.Rows[e.RowIndex].Cells[0].Value;
}

and it worked.

In my example column 0 is the hidden column, that contains the id property that you want to extract.

like image 57
Jakub Kaleta Avatar answered Oct 12 '22 22:10

Jakub Kaleta


For me it works to take the columnName not the index.

so if your column has the name "id" you could use:

datagridview1.CurrentRow.Cells["id"].Value
like image 29
Manu Avatar answered Oct 12 '22 23:10

Manu