find A row in DataGridview using datarow values

Find a row datagridView without looping

Page 1 of 1

2 Replies - 28633 Views - Last Post: 08 July 2010 - 01:31 PM Rate Topic: -----

#1 dmehta5   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-April 10

find A row in DataGridview using datarow values

Posted 08 July 2010 - 06:49 AM

hi ,
i have a Datagridview which am populating from a datatable, am not using databinding here , jsut populating from the datatable column by column.
1. if the value is not present in the datagridview then add the row and make the row as some green color.
2. If the datarow of datatable is present in the datagridview mark it as Gold color.

I have seen so many topics in the net ... but they all use datagridviewrow.indexof(datagridviewrow) to find out if row in present in the datagridview , provided the row has been selected by the user.

1 .how to find that a particular row is present in the datagridview?
                                            DataGridViewRow row1 =new DataGridViewRow();
                                            row1.CreateCells(datagridview1, someObjectarrayValuesAlsoPresentinDataGridView[]);
int ind = datagridview1.Rows.IndexOf(row1);
datagridview1.Rows[ind].DefaultCellStyle.BackColor = Color.Gold;

this doesnt work...
ind values always comes as -1.
datagridview1.Rows[ind].DefaultCellStyle.BackColor = Color.Gold;

throws as exception Index value out of range.
Is there any way it can be implemented.
bool value = datagridview1.Rows.contains(row1);

this also returns false all the time.

Y the indexof(row) nt working.... though the row gets populated with correct cell values.
Note: My row is not selected by the user.

Is This A Good Question/Topic? 0
  • +

Replies To: find A row in DataGridview using datarow values

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: find A row in DataGridview using datarow values

Posted 08 July 2010 - 07:33 AM

Moved to C#. This forum is for reporting posts that violate some rule here or other reasons :)
Was This Post Helpful? 0
  • +
  • -

#3 Bacanze   User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 202
  • Joined: 09-April 10

Re: find A row in DataGridview using datarow values

Posted 08 July 2010 - 01:31 PM

This had me thinking for a while as it's been a while since I've used a DGV, and don't have VS to test.

This *should* work to select the row number

int rowNumber = dgvdata.CurrentCell.RowIndex;

Also theres a property to select the entire row when the cell is clicked, the property SelectionMode can be used for this if that's what you want.

Feel free to post again if you need help, I'm a tired atm so I only briefly read your post :sleep1:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1