DataGrid Row Searching

G

Guest

Background:

I have a parent table (Employee) with two child tables (Phone and Email)
I have a datagrid bound to the dataset, with datamember = Employee for the
employee table
I have two other datagrids for the Phone and Email bound to the same
dataset, with DataMember = EmployeeToPhone and EmployeeToEmail relations,
respectively.

This allows me to select an employee in the employee grid, and the phone
numbers and emails for that employee in the phone/email grids and the
relations are maintained without me having to do anything special.

The problem I'm currently having is with validation. After the user is
completely done with their updates, I loop through all the datarows in the
GetChanges(Added) and GetChanges(Modified) method of each DataSet.DataTable.
If I encounter an invalid cell value, I would like to send focus back to that
cell in the datagrid.

Here are my problems:
1. Based on the information that I have at the time (the DataRow and column
mapping name of the incorrect-valued cell), I can't find a way to figure out
what row and column numbers this corresponds to in the DataGrid.
2. Now, say the user has a column sorted - this doesn't affect the dataset,
only the datagrid. How can I get the right row/column to select now?
3. Lastly, for the Phone and Email grids, I will need to first select the
Parent row in the Employee datagrid before selecting the right Cell to send
focus to for editing. How do I do this (maybe the answers in 1 or 2 may help
me with this one, but I am still stuck on this).

Thanks in advance!
 
G

Guest

Cor,

This is a completely different method of validation than I described. This
would go through each DataRow and set the RowError property if it is invalid,
which would then give the standard error flag next to the rows in the
DataGrid on a refresh. This also doesn't explain anything about how to
select the cell with the first instance of an error.

The problem with this method of validation for me is that if I have errors
in the child tables (which are displayed in their own DataGrids), you will
not see the error unless you select a row in the parent table that the child
row belongs to. Your link might be something I'd do in addition to what I
have now, but I need more than just that.

What I need specifically:
- How do I select a cell in a datagrid based on its primary key/ID,
regardless of how the data is sorted in the grid? Remember the fact that the
ID is not visible through the DataGrid.Item(row,col) property, since I'm
filtering the visible columns with the TableStyles.GridColumnStyles().
 
C

Cor Ligthert [MVP]

Keith,

Both the dataview (defaultview) and the currencymanager are in my idea
ussable to get the position of a row in a datagrid.

In my idea is it the best if you use the dataview.find, than you get an
index, that index is the same as the row in a DataGrid. (Your datasource
should than be the dataview or better the defaultview of the table).

http://msdn2.microsoft.com/en-us/library/system.data.dataview.find.aspx

I hope this helps,

Cor
 
G

Guest

Doesn't seem to be working right.

I've tried the following 3 things in an attempt to get the correct row
(intRow = ...), but all return -1:

Dim strSort As String
Dim dvEmployee As DataView =
DirectCast(grdEmployee.BindingContext(dsDataSet, "Employee").Current,
DataRowView).DataView
strSort = dvEmployee.Sort
Dim dvTemp As New DataView(dsDataSet.Tables("Employee"))
dvTemp.Sort = strSort
intRow = dvTemp.Find(objRow("EmployeeID"))
intRow = dvEmployee.Find(objRow("EmployeeID"))

dsDataSet.Tables("Employee").DefaultView.Sort = strSort
intRow =
dsDataSet.Tables("Employee").DefaultView.Find(objRow("EmployeeID"))
 
C

Cor Ligthert [MVP]

Keith,

You are right I thought the primary key was used by this but it is the sort
property. So it is sorting what you don't want. Sorry

Than you have in my idea only as option something as this.

dim i as integer
dim dv as DirectCast(grdEmployee.DataSource,DataView)
For i from 0 to dv.Count -1
if dv(i)("EmployeeId") = WhatIWant then exit for
Next
Row in the grd = i

Don't think it is slow behind the scene is a lot of looping done.

Not tested but this seems very simple for me.

Cor
 
G

Guest

This is what I ended up with - you pass in a dataview and the key value you
want to find, and it will return the row. The hardest part was finding how
to get access to the dataview that the grid is bound to, which appears to be:

DirectCast(grdEmployee.BindingContext(dsDataSet, "Employee").Current,
DataRowView).DataView

I'm not sure why, but that seems to work. It seems like there should be an
"easier" way to get at this, but now that I found it, it doesn't matter all
that much. It was very hard to pull out the right dataview object because
i'm bound to a dataset and not a dataview.

This will only work for tables with a single primary key (which covers all
of my tables - we use an ID for all PKs)

Private Function GetRowBySinglePrimaryKey(ByVal dvDataView As DataView,
ByVal strKey As Object) As Integer
Try
Dim II As Integer
For II = 0 To dvDataView.Count - 1
If dvDataView(II)(dvDataView.Table.PrimaryKey(0).ToString) =
strKey Then
Return II
End If
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top