Editing a DataTable from a DataGrid

G

Guest

Hi,

I need help -- again --

I have a DataGrid with a DataTable as its source. It displays wonderfully
when I put in some test data (withinin the code).

Now, I'd like to change one of the values in the Grid, and have the
DataTable change. I've tried using a Validating (and Validated) event to
display the changed data. It doesn't change. I've tried adding
AcceptChanges and GetChanges to my code (for the Table).

I'm guessing this shouldn't be hard to do, but I haven't figured it out.
Can anyone tell me what I'm leaving out?

Thanks,

Art
 
C

Cor Ligthert

Art,
Now, I'd like to change one of the values in the Grid, and have the
DataTable change. I've tried using a Validating (and Validated) event to
display the changed data. It doesn't change. I've tried adding
AcceptChanges and GetChanges to my code (for the Table).
This is a little bit unclear for me. Are you changing the data in the grid
and want to have it in the datatable. Or are you changing the data in the
datatable and do you want it reflected in the datagrid?

Cor
 
G

Guest

Cor,

Thanks for taking my question - again.

I am changing the data on the form, in the grid -- as a user would change it
while running the program. I want the DataTable to then be changed to
reflect the change. Clearly I'm new to using DataGrids and DataTables.

Thanks Again,

Art
 
A

Armin Zingler

Art said:
I am changing the data on the form, in the grid -- as a user would
change it while running the program. I want the DataTable to then
be changed to reflect the change. Clearly I'm new to using
DataGrids and DataTables.

If the datatable is bound to the datagrid, the datagrid automatically
updates the datatable.

Armin
 
C

Cor Ligthert

Art,

Data is pushed down from the datagrid to its datasource in two ways.

By a rowchange from the user

By an endcurrentedit in code

(In this sample assuming that the ds.Tables(0) is the datasource)
\\\
BindingContext(ds.Tables(0)).EndCurrentEdit()
///

I hope this helps,

Cor
 
C

Cor Ligthert

Armin,

There is a big time lap in your sending. I did not see it, before I wrote
and sent my answer.

Cor
 
A

Armin Zingler

Cor Ligthert said:
Armin,

There is a big time lap in your sending. I did not see it, before I
wrote and sent my answer.

I first write some posts, then read them again before sending them all at
once. Between writing the first and the last post there might be a certain
period of time. If I additionally had to do something else (really, I
sometimes have to ;-) ), the period might be even longer, but that's not
intended.


Armin
 
G

Guest

Cor,

Thanks -- I'll give it a try.

Also, about that time lag -- I've been seeing it too, and also I have not
been getting the e-mail notifications of a post.

Art
 
G

Guest

Cor,

Apparently I'm missing something huge and completely misunderstanding
DataGrids and DataTables. If you can, could you suggest something to look at.

All I did was created a DataGrid by dragging and dropping. Then, in my
forms load event I created a DataTable. I added a few rows of data to it by
adding rows. I then set the DataSource of the DataGrid to the DataTable.

I next created a couple of events so I could look at what's in the DataTable
in the Immediate Window.

If I add a new row to my DataGrid (when the form is running) I see that data
appear in my DataTable, although not where I'd expect it. I can't seem to
change the data in the DataTable that I put in it when I originally set it up.

I did not even know about the BindingContext until you mentioned it.

If there's any advice or suggestions (other than "Please stop trying to
write these programs!") I'd really appreciate it.

Thanks,

Art
 
C

Cor Ligthert

Art,

Can you show your code without the designer part.
First paste it in a notebook, copy it back and paste it in this message
otherwise it is moslty unreadable. Than I have a look for it tomorrow. You
can as well mail the complete code to my mailadres when you want.

Although I will try to answer you using this newsgroup than.

Cor
 
G

Guest

Cor,

I appreciate the help. Since I'm only trying to learn how to do this at the
moment, everything is in my form class. I left in the generated code -- just
in case.

Thanks!

Art

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid
CType(Me.DataGrid1,
System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.AlternatingBackColor =
System.Drawing.SystemColors.InactiveCaptionText
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(56, 56)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(272, 128)
Me.DataGrid1.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(400, 294)
Me.Controls.Add(Me.DataGrid1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1,
System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub
#End Region

Dim MyTable As DataTable
Dim temp As New DataGrid

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
MyTable = New DataTable("MyTableName")
MyTable.AcceptChanges()
Dim Col_1 As DataColumn
Col_1 = MyTable.Columns.Add("Item number",
Type.GetType("System.Int32"))
With MyTable.Columns
.Add("Item Name", Type.GetType("System.String"))
.Add("Cost", Type.GetType("System.Double"))
End With
MyTable.Rows.Add(New Object() {1, "Item 1", 3.6})
MyTable.Rows.Add(New Object() {5, "Item 2", 10.12})
MyTable.Rows.Add(New Object() {9, "Item 4", 23.42})
Me.DataGrid1.DataSource = MyTable
End Sub

Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles DataGrid1.CurrentCellChanged
BindingContext(MyTable).EndCurrentEdit()
Stop
End Sub
End Class
 
C

Cor Ligthert

Art,

You need only to push data in the datatable, when you need it for an update
or whatever else. In the normally it is done by a rowchange so that is quick
enough.

An update starts mostly, if you are using a datagrid, with an update button,
where ever that is, it can even be the close button.

I do not use the archaic "With". It makes reading not easier and in my
opinion only efficient in VBA (office). Where we cannot (or don't) use the
import to set things default or to make a namespace more readable.

What the acceptchanges does is telling that all rowstate has to be set as
started.
When you add a row than that row is set as 'added'. However when you won't
that you can say at the end. Acceptchanges. Than all is set ot a beginning
state and will never be inserted to by instance to a database, because the
rowstate is unchanged. However when you use it in a dataset which you keep
as a file on disk, it can in this case be usefull to do it at the end to
detect changes (dt.haschanges) in a next session.

I changed the code in the way as I could have written it.
\\\
Dim MyTable As DataTable
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
MyTable = New DataTable("MyTableName")
MyTable.Columns.Add("Item number", Type.GetType("System.Int32"))
MyTable.Columns.Add("Item Name", Type.GetType("System.String"))
MyTable.Columns.Add("Cost", Type.GetType("System.Double"))
MyTable.Rows.Add(New Object() {1, "Item 1", 3.6})
MyTable.Rows.Add(New Object() {5, "Item 2", 10.12})
MyTable.Rows.Add(New Object() {9, "Item 4", 23.42})
Me.DataGrid1.DataSource = MyTable
End Sub
Private Sub Button_Update(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ButtonUpdateClick
BindingContext(MyTable).EndCurrentEdit()
End Sub
///

I hope this helps,

Cor
 
G

Guest

Cor,

Thanks for your help -- everything is working now.

It turns out that the way I've been testing some of this stuff has been a
problem. I've put stop statements in, and then in the immediate window I've
checked values of my variables or objects.

I had been typing: ?MyTable.Columns(1)(1) in order to see what was in
that particular cell. So I decided to make that investigation part of the
code with a button. I found that that syntax was improper -- even though I
did not get an error in the immediate window.

When I put something in my code to read back to me specified locations in
MyTable, it worked perfectly.

Your help was critical to my getting through this. Thanks very much.

Art
 

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