DataGridView:Column's DataPropertyName



i have many datagridviews component in my project and each tabpage i have a method that refresh that datagridviews component and in that method

i needed to check ;

private void IENoRefresh(Wisej.Web.DataGridView Gv){

if (Gv.RowCount > 0)
if (!Gv.SelectedRows[0].IsNewRow)

Gv.SelectedRows[0][“ProjeID”].Value.ToString() != “”


and in design code added  column name properties shown below

this.dgvcolumnname.HeaderText= “ProjeID”;
this.dgvcolumnname.DataPropertyName = “ProjeID”;
this.dgvcolumnname.Name = “xyz”;

now im migrating my project from devexpress to wisej and i realized that when i check dgv.SelectedRows[0][“ProjeID”].Value.ToString() != “” i have an exception that is “ProjeID”  named column not found in that datagridview but when i write its name “xyz” have  no problem .

The question i’d like to ask is there anyway that i can check that gridview column by using DataPropertyName properties without getting an exception .By the way i know i can write common name properties for all column names and can use it but i have alot of columns ,want to know is there another way?



  • You must to post comments

The DataPropertyName is related to data binding and it’s not unique since multiple columns can be bound to the same data property, plus if the indexer is a string then it can be either the name or the DataPropertyName for clarity.

You can use System.Linq on the columns collection to find the column index like this:

dataGridView.Columns.First(c => c.DataPropertyName == “Name”).Index;

Incidentally I noticed that the Rows and SelectedRows collection only allow the index and the name in the indexer, they should also use the column instance, like this:

dataGridView.SelectedRows[0][dataGridView.Columns.First(c => c.DataPropertyName == “Name”)]

but now you can do this:

dataGridView.SelectedRows[0][dataGridView.Columns.First(c => c.DataPropertyName == “Name”).Index]

If the column doesn’t exist you’ll get an exception from Linq.


  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.