Help!!
I need to get the text or value displayed in datagridview columns.
how can i do this?
regards
Jineesh
Help!!
I need to get the text or value displayed in datagridview columns.
how can i do this?
regards
Jineesh
Short summary of the answers already posted: and show the grid indexer / Rows+Cells approach to get a DataGridViewCell (that exposes the underlying value). correctly points out that an editing cell may expose a different value until the edit is committed. The notes below clarify which property to read and give practical ways to get the visible text reliably.
Which property to use: the cell's Value is the actual data item (the bound value or raw object). The FormattedValue is what the grid shows after formatting and type conversion, so use it when the displayed text is needed. For bound rows, use the row's DataBoundItem to read the underlying data object (DataRowView or a custom object) and pull the column/property by name; this is useful when display text is driven by ValueMember/DisplayMember. See the DataBoundItem docs for details (DataBoundItem).
When a cell is being edited: the current editor text may not be stored in Value yet. Options are: force a commit so the cell writes back to the source, read the editing control shown by the grid, or use the cell's edited formatted value while editing (as noted). Example approaches:
var drv = (DataRowView)dataGridView1.Rows[rowIndex].DataBoundItem;
var displayText = drv["ColumnName"].ToString(); dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); (See the EditingControlShowing and CommitEdit docs for event/behavior details: EditingControlShowing, CommitEdit.)
Troubleshooting tips: watch for DBNull.Value/null, ComboBox cells using DisplayMember/ValueMember, CheckBox cells returning booleans, and VirtualMode/binding delays. Prefer FormattedValue when the intention is to match exactly what the user sees (FormattedValue).
Jump to Post— IdanS 12Hi,
What you need is to use the following code:
this.dataGridView1[columnIndex,rowIndex];Enjoy!
Hi,
What you need is to use the following code:
this.dataGridView1[columnIndex,rowIndex]; Enjoy!
Rows and Cells collection of DataGridView control.
// First Row and first column
DataGridView1.Rows[0].Cells[0].Value
DataGridView1.Rows[0].Cells[1].Value // 2nd column
... Yes, this.dataGridView1[columnIndex,rowIndex] indexer returns an instance of DataGridViewCell.
You may have to use the DataGridView1.Rows[0].Cells[0].EditedFormattedValue to see the current value until the DataGrid gets updated. EditedFormattedValue displays the value currently whether or not it has been updated to the BindingSource, or the DataGrid.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.