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
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 users view data displayed in a Windows Forms DataGridView control, they sometimes need to refer to a single column or set of columns frequently.
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
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With