473,607 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Datagrid add and edit features

I have a read-only datagrid that I use to display a heirarchal model
of three tables (The example I am setting up uses the Customer,
Orders, and Order Detail tables from the northwind database). I load
the three tables into a single dataset and then create the
relationships.

What I want to do is use a separate grid that would display the
selected record from the datagrid only, but still would be bound to
the dataset. The first grid would be used for navigating, while the
second grid would be used for modifying. The goal is that the second
grid would not be as 'busy', displaying only the information to be
modified.

Any help would be greatly appreciated. Thanks.
Nov 20 '05 #1
3 1567
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.

Nov 20 '05 #2
Hi Tim,

Thanks for posting in the community.

Did my suggestion help you>?
If you have any concern on this issue,please post here.

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.

Nov 20 '05 #3

Hi Peter,

Sorry for the late reply. I'm on the road this week and haven't had
access to an internet connection.

Thanks very much for the help. I'm fairly new to .Net (coming from VB6),
and didn't realize the dataview had additional properties not found in
the dataset. So, I was able to use your code to create a filtered
recordset, that was just what I was looking for. Thanks again...
greatly appreciated.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1365
by: admin | last post by:
New Article Building a DataGrid with Add and Edit Features By Tulga Kalayci The DataGrid server control is one of the most widely used controls in ASP.NET-based projects to display data in a table form. When using a mixture of database- and manually-driven DropDownLists, placing them in a DataGrid can be confusing. In this tutorial, I'll focus on resolving this issue using an example of an expense entry and reporting system. (Monday,...
3
1736
by: Dan Williams | last post by:
Anyone know if i can take advantage of the datagrid features and use it in a vertical format to view and edit one record from a database? For example, Name: Address: Town: County:
0
1015
by: jinhy82 | last post by:
Hello! I am using DataGrid to display details from Ms Access. Fe features such as Edit, Update and Delete are added, but they did no function. Can anyone help me? Thank you very much!!! For the update function, am I suppose to write the commane : UPDAT <tablename> SET = "..."? As I am using the "EditCommandColumn", so when I press the "Edit" link everyfield will changed to textbox form, may I know what is that nam for the textbox? Here...
0
252
by: admin | last post by:
New Article Building a DataGrid with Add and Edit Features By Tulga Kalayci The DataGrid server control is one of the most widely used controls in ASP.NET-based projects to display data in a table form. When using a mixture of database- and manually-driven DropDownLists, placing them in a DataGrid can be confusing. In this tutorial, I'll focus on resolving this issue using an example of an expense entry and reporting system. (Monday,...
0
261
by: admin | last post by:
New Article Building a DataGrid with Add and Edit Features By Tulga Kalayci The DataGrid server control is one of the most widely used controls in ASP.NET-based projects to display data in a table form. When using a mixture of database- and manually-driven DropDownLists, placing them in a DataGrid can be confusing. In this tutorial, I'll focus on resolving this issue using an example of an expense entry and reporting system. (Monday,...
0
8050
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7987
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8472
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8464
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8130
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8324
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6805
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5471
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3954
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.