Hi Tim,
Thanks for posting in the community.
First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to use two datagrids on
a winform, and use Datagrid A (readonly to display the data) and Datagrid B
to display just the current select on in Datagrid A for modification. The
datasource your are using is a heirarchal tables.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.
I think you may try to bind the datagrid A on the Dataset. But for the
datagrid B you need to bind it to a new dataview, since the dataview should
create from the datatable, you need to reset the datasource of datagrid B
when you inspect the child table if the datagrid B.
In the DataView, you may use the RowFilter to display just the current
selected one.
Here I write a demo code for you.
<Code>
Dim v As DataView
Dim ds As Dataset1
Public str() As String
Public columnName() As String
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim sda1 As New SqlDataAdapter( "SELECT * FROM Customers",
Me.SqlConnectio n1)
Dim sda2 As New SqlDataAdapter( "SELECT * FROM Orders",
Me.SqlConnectio n1)
Dim sda3 As New SqlDataAdapter( "SELECT * FROM [Order Details]",
Me.SqlConnectio n1)
ds = New Dataset1
sda1.Fill(ds.Cu stomers)
sda2.Fill(ds.Or ders)
sda3.Fill(ds.Or der_Details)
DataGrid1.DataS ource = ds
DataGrid1.DataM ember = "Customers"
AddHandler ds.Customers.Ro wChanged, AddressOf dt_RowChanged
AddHandler ds.Orders.RowCh anged, AddressOf dt_RowChanged
AddHandler ds.Order_Detail s.RowChanged, AddressOf dt_RowChanged
End Sub
Private Sub dt_RowChanged(B yVal sender As Object, ByVal e As
System.Data.Dat aRowChangeEvent Args)
If Not (Me.columnName Is Nothing Or Me.str Is Nothing) Then
Dim i As Integer
Dim strbld As New StringBuilder
For i = 0 To columnName.Leng th - 1
If i = 0 Then
strbld.Append(c olumnName(i) + "='" + str(i) + "'")
Else
strbld.Append(" AND " + columnName(i) + "='" + str(i) +
"'")
End If
Next
v.RowFilter = strbld.ToString
End If
End Sub
Private Sub DataGrid1_DataS ourceChanged(By Val sender As Object, ByVal e
As System.EventArg s) Handles DataGrid1.DataS ourceChanged
Dim dt As DataTable
Dim cr As CurrencyManager
If Me.DataGrid1.Da taMember = "" Then
Exit Sub
End If
cr = CType(BindingCo ntext(Me.DataGr id1.DataSource,
Me.DataGrid1.Da taMember), CurrencyManager )
dt = CType(cr.Curren t, DataRowView).Ro w.Table
v = New DataView(dt)
DataGrid2.DataS ource = v
Dim dc() As DataColumn = dt.PrimaryKey
Dim i As Integer
i = 0
Dim strbld As New StringBuilder
ReDim columnName(dc.L ength - 1)
ReDim str(dc.Length - 1)
For i = 0 To dc.Length - 1
columnName(i) = dc(i).ColumnNam e
str(i) = CType(cr.Curren t, DataRowView).Ro w.Item(columnNa me(i))
If i = 0 Then
strbld.Append(c olumnName(i) + "='" + str(i) + "'")
Else
strbld.Append(" AND " + columnName(i) + "='" + str(i) + "'")
End If
Next
v.RowFilter = strbld.ToString
End Sub
Private Sub DataGrid1_Mouse Up(ByVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles DataGrid1.Mouse Up
Dim ds As Dataset1
ds = Me.DataGrid1.Da taSource
Dim dt As DataTable
Dim cr As CurrencyManager
cr = CType(BindingCo ntext(Me.DataGr id1.DataSource,
Me.DataGrid1.Da taMember), CurrencyManager )
Dim drv As DataRowView
drv = CType(cr.Curren t, DataRowView)
dt = drv.Row.Table
Dim dc() As DataColumn = dt.PrimaryKey
Dim i As Integer
i = 0
Dim strbld As New StringBuilder
ReDim columnName(dc.L ength - 1)
ReDim str(dc.Length - 1)
For i = 0 To dc.Length - 1
columnName(i) = dc(i).ColumnNam e
str(i) = CType(cr.Curren t, DataRowView).Ro w.Item(columnNa me(i))
If i = 0 Then
strbld.Append(c olumnName(i) + "='" + str(i) + "'")
Else
strbld.Append(" AND " + columnName(i) + "='" + str(i) + "'")
End If
Next
v.RowFilter = strbld.ToString
End Sub
End Class
</Code>
If you have any concern on this issue,please post here.
My test works on the dataset xml schema as below.
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="Dataset1" targetNamespace ="http://tempuri.org/Dataset1.xsd"
elementFormDefa ult="qualified"
attributeFormDe fault="qualifie d" xmlns="http://tempuri.org/Dataset1.xsd"
xmlns:mstns="ht tp://tempuri.org/Dataset1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="u rn:schemas-microsoft-com:xml-msdata">
<xs:element name="Dataset1" msdata:IsDataSe t="true">
<xs:complexType >
<xs:choice maxOccurs="unbo unded">
<xs:element name="Customers ">
<xs:complexType >
<xs:sequence>
<xs:element name="CustomerI D" type="xs:string " />
<xs:element name="CompanyNa me" type="xs:string " />
<xs:element name="ContactNa me" type="xs:string " minOccurs="0" />
<xs:element name="ContactTi tle" type="xs:string " minOccurs="0" />
<xs:element name="Address" type="xs:string " minOccurs="0" />
<xs:element name="City" type="xs:string " minOccurs="0" />
<xs:element name="Region" type="xs:string " minOccurs="0" />
<xs:element name="PostalCod e" type="xs:string " minOccurs="0" />
<xs:element name="Country" type="xs:string " minOccurs="0" />
<xs:element name="Phone" type="xs:string " minOccurs="0" />
<xs:element name="Fax" type="xs:string " minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Orders">
<xs:complexType >
<xs:sequence>
<xs:element name="OrderID" msdata:ReadOnly ="true"
msdata:AutoIncr ement="true" type="xs:int" />
<xs:element name="CustomerI D" type="xs:string " minOccurs="0" />
<xs:element name="EmployeeI D" type="xs:int" minOccurs="0" />
<xs:element name="OrderDate " type="xs:dateTi me" minOccurs="0" />
<xs:element name="RequiredD ate" type="xs:dateTi me" minOccurs="0" />
<xs:element name="ShippedDa te" type="xs:dateTi me" minOccurs="0" />
<xs:element name="ShipVia" type="xs:int" minOccurs="0" />
<xs:element name="Freight" type="xs:decima l" minOccurs="0" />
<xs:element name="ShipName" type="xs:string " minOccurs="0" />
<xs:element name="ShipAddre ss" type="xs:string " minOccurs="0" />
<xs:element name="ShipCity" type="xs:string " minOccurs="0" />
<xs:element name="ShipRegio n" type="xs:string " minOccurs="0" />
<xs:element name="ShipPosta lCode" type="xs:string " minOccurs="0" />
<xs:element name="ShipCount ry" type="xs:string " minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Order_x00 20_Details">
<xs:complexType >
<xs:sequence>
<xs:element name="OrderID" type="xs:int" />
<xs:element name="ProductID " type="xs:int" />
<xs:element name="UnitPrice " type="xs:decima l" />
<xs:element name="Quantity" type="xs:short" />
<xs:element name="Discount" type="xs:float" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="Dataset1K ey1" msdata:PrimaryK ey="true">
<xs:selector xpath=".//mstns:Customers " />
<xs:field xpath="mstns:Cu stomerID" />
</xs:unique>
<xs:unique name="Dataset1K ey2" msdata:PrimaryK ey="true">
<xs:selector xpath=".//mstns:Orders" />
<xs:field xpath="mstns:Or derID" />
</xs:unique>
<xs:unique name="Dataset1K ey3" msdata:PrimaryK ey="true">
<xs:selector xpath=".//mstns:Order_x00 20_Details" />
<xs:field xpath="mstns:Or derID" />
<xs:field xpath="mstns:Pr oductID" />
</xs:unique>
<xs:keyref name="Customers Orders" refer="Dataset1 Key1">
<xs:selector xpath=".//mstns:Orders" />
<xs:field xpath="mstns:Cu stomerID" />
</xs:keyref>
<xs:keyref name="OrdersOrd er_x005F_x0020_ Details" refer="Dataset1 Key2">
<xs:selector xpath=".//mstns:Order_x00 20_Details" />
<xs:field xpath="mstns:Or derID" />
</xs:keyref>
</xs:element>
</xs:schema>
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.