473,379 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Editing a DataTable from a DataGrid

Art
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
Nov 21 '05 #1
12 1756
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
Nov 21 '05 #2
Art
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

"Cor Ligthert" wrote:
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

Nov 21 '05 #3
"Art" <Ar*@discussions.microsoft.com> schrieb
> 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?

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

Nov 21 '05 #4
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
Nov 21 '05 #5
Armin,

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

Cor
Nov 21 '05 #6
Art
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

"Cor Ligthert" wrote:
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

Nov 21 '05 #7
"Cor Ligthert" <no************@planet.nl> schrieb
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

Nov 21 '05 #8
Art
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

"Cor Ligthert" wrote:
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

Nov 21 '05 #9
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
Nov 21 '05 #10
Art
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).BeginIni t()
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

"Cor Ligthert" wrote:
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

Nov 21 '05 #11
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
Nov 21 '05 #12
Art
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

"Cor Ligthert" wrote:
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

Nov 21 '05 #13

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

Similar topics

3
by: Tony | last post by:
Hello, I want to edit a dataset returned from a webservice, in the dataset, there is three columns - DisplayOrder, Location, Time... What i would like to do is replace the DisplayOrder column with...
2
by: Martin Hazell | last post by:
For various reasons, I have had to produce a quick (!) page to edit one column of data in a database with ASP.net. With this being my first foray into ASP.net, I apoligise for any basic erros I have...
4
by: TT (Tom Tempelaere) | last post by:
Hi, Suppose there is a DataGrid on a form bound to a DataTable. When a user is editing a cell without leaving the cell, and then closes the form, the value is not updated to the underlying data...
0
by: Dave | last post by:
Tried posting in the Winform Forum without much luck, so posting here... After inserting a new data row to a DataTable that is bound to a datagrid, I am unable to change data in a row that is...
2
by: Mark Perona | last post by:
I created an ASP.net form with an editable datagrid on it. I can create new records, and update and delete existing records. The problem I have is that I want a field in the grid to be editible...
10
by: JohnR | last post by:
I have a datatable as the datasource to a datagrid. The datagrid has a datagridtablestyle defined. I use the datagridtablestyle to change the order of the columns (so they can be different than...
5
by: =?Utf-8?B?QWRhciBXZXNsZXk=?= | last post by:
Hi All, I have a GridView inside the EditItemTemplate of a FormView. Both FormView and GridView are data bound using an ObjectDataSource. When the FormView's ObjectDataSource object has a...
0
by: prerak_v_shah | last post by:
Hi, Friends, I have been developing datagrid program in .Net 1.1 and having VB.Net as a programming language. I have created a datagrid which is bound to a datatable as its datasource. It also...
8
by: Sep410 | last post by:
Hi all, Here is the code that I have: For h = 2 To dt.Rows.Count - 1 For j = 0 To 11 Excel.Cells(h, k).value = (dt.Rows(h).Item(j).Value).ToString ...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.