473,386 Members | 1,738 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,386 software developers and data experts.

update data in datagrid

Hi, all
How can I update data (multiple rows, but not every rows) using dataset in
datagrid?

I mean is there any way I can let datagrid know which row(s)/column(s) has been
modified and update them at database?
Thanks.
Nov 20 '05 #1
5 2009
If the DataSet was populated from a database using a DataAdapter, then when
you call the DataAdapter's Update method it will know which rows are
different from the original.

If you built the table in the DataSet yourself, I believe there is a method
of a row that tells you if it is "dirty" (hasn't been updated).
<ja***@hotmail.com> wrote in message
news:8b********************************@4ax.com...
Hi, all
How can I update data (multiple rows, but not every rows) using dataset in
datagrid?

I mean is there any way I can let datagrid know which row(s)/column(s) has been modified and update them at database?
Thanks.

Nov 20 '05 #2
Hi Jason,

Your datagrid shows always dynamicly the data in the datasource.

So when that is by instance a dataview with a rowfilter and your new datarow
in the datasets is conform that rowfilter it will be showed up when you add
it to the dataset.

I hope this gives an idea?

Cor
How can I update data (multiple rows, but not every rows) using dataset in
datagrid?

I mean is there any way I can let datagrid know which row(s)/column(s) has been modified and update them at database?
Thanks.

Nov 20 '05 #3
Here is the code, Button1 will load data from local Access (*.mdb) file, and
Button2 will save modfied data to Access file. But somehow, after I press
Button2, nothing happened. Please help.
Thanks in advance.
Imports System.Data.OleDb

Public Class Form1
Inherits System.Windows.Forms.Form

Public ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=D:\test.mdb"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
' & "Data Source=D:\test.mdb"

' Open the Access database.
Dim cn As New OleDbConnection
cn.ConnectionString = ConnString

Try
cn.Open()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
' Ensure that the connection is closed.
' (It does not throw an exception even if the Open command failed.)
cn.Close()
End Try

' Define the command
Dim sqlString As String = "SELECT DCN,Field1 from Table1"

Dim da As New OleDbDataAdapter(sqlString, cn)

Dim ds As New DataSet

da.Fill(ds, "Table1")

DataGrid1.DataSource = ds.Tables("Table1")
' Close the connection only if it was opened.
'ConnectionState.Closed = 0
If (cn.State And ConnectionState.Open) <> 0 Then
cn.Close()
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

Dim cn As New OleDbConnection(ConnString)
cn.Open()

Dim da As New OleDbDataAdapter("SELECT DCN,Field1 from Table1", cn)

Dim ds As New DataSet

Dim cmdBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(da)

da.Fill(ds, "Table1")
' Update database with modified data
da.ContinueUpdateOnError = True
da.UpdateCommand = cmdBuilder.GetUpdateCommand()
da.Update(ds, "Table1")
' Close the connection only if it was opened.
'ConnectionState.Closed = 0
If (cn.State And ConnectionState.Open) <> 0 Then
cn.Close()
End If
End Sub

On Fri, 25 Jun 2004 17:17:01 -0600, ja***@hotmail.com wrote:
Hi, all
How can I update data (multiple rows, but not every rows) using dataset in
datagrid?

I mean is there any way I can let datagrid know which row(s)/column(s) has been
modified and update them at database?
Thanks.


Nov 20 '05 #4
Hi Jason,

The answer is why it is not updating is is simple, however why that strange
closing code.
In my opinion it is even better to open an access database at the begin of
the program and to close it at the end. That prevent users from moving the
mdb file in the maintime (this is only for access).

The closing statement is strange, where did you read that, after all is
working it is better to place all in a try, catch finally end try block.
However because the sentence above you do not need a finally.

The answer on why it is not updating is simple, you fill the dataset with
old information before you do the update so what you have entered in the
datagrid will be disapeared.

You probably need more code however first delete the sentence I point you on
in this procedure and better change it in a good way.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cn As New OleDbConnection(ConnString)
cn.Open()
Dim da As New OleDbDataAdapter("SELECT DCN,Field1 from Table1", cn) Dim ds As New DataSet
Dim cmdBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(da) da.Fill(ds, "Table1") This one above fills the dataset again with the old data from the datagrid
so you have to remove this.
' Update database with modified data
da.ContinueUpdateOnError = True This one is a killer I would replace this with a Try, Catch and EndTry block
like this.
(This as well in the button1 at the fill, I show you the most simple one) da.UpdateCommand = cmdBuilder.GetUpdateCommand() Try da.Update(ds, "Table1")

Catch ex as OleDbexception
messagebox.show(ex.tostring)
'this has to be if it is good in an if block where you do your
actions when there is an error
End Try

This is absolute not complete, however to give you a go.

(all typed in this message so watch typos or other errors)

I hope this helps?

Cor
Nov 20 '05 #5

Thanks Cor, it works.

By the way, "The closing statement is strange, where did you read that?" Ha...
From Microsoft Press, "Programming Microsoft Visual Basic.Net Version 2003, pg
714, Opening and Closing the Connection".

On Sun, 27 Jun 2004 09:38:39 +0200, "Cor Ligthert" <no**********@planet.nl>
wrote:
Hi Jason,

The answer is why it is not updating is is simple, however why that strange
closing code.


Nov 20 '05 #6

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

Similar topics

13
by: abdoly | last post by:
i wrote a code to update datagrid with the datagrid updatecommand but i cant get the updated values after being update that is the code private void DataGrid1_UpdateCommand(object source,...
3
by: D. Shane Fowlkes | last post by:
I have a Datagrid which in theory, should allow you to edit and update the records. I've stripped my test page down so that it's only attempting to update one field - "description". Yet when I...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
5
by: Jay Villa | last post by:
Is it possible to update only few columns in datagrid during OnUpdateCommand event ? If so, could you help me .... -thanks Jay
4
by: Jonathan Upright | last post by:
Greetings to anyone who can help: I'm using WebMatrix to make ASP.NET pages, and I chose the "Editable DataGrid" at the project selector screen. As you may know, it defaults to the Microsoft...
1
by: mursyidatun ismail | last post by:
Dear all, database use: Ms Access. platform: .Net i'm trying to update a record/records in a table called t_doctors by clicking da edit link provided in the database. when i ran through da...
3
by: Jim | last post by:
I have a datagrid with a DataAdapter as the DataSource. The user fills in their data for 3 columns and I want to programically add a value to the 4th (invisible) column (employee number). That way...
5
by: Stephen Plotnick | last post by:
I'm very new to VB.NET 2003 Here is what I have accomplished: MainSelectForm - Selects an item In a public class I pass a DataViewRow to ItemInformation1 Form ItemInformation2 Form
1
by: Sharon | last post by:
Hello All, Is it possible to update Sql Table through DataGrid. I have a DataGrid which is being populated through a stored procedure, all i wanted to do is to update one field...
1
by: geeteshss | last post by:
Dear all, actually i spent a whole month on the R&D of datagrid edit ,update,cancel events but recently my guide told me to make it user friendly because no user would like to go on searching rows...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
0
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,...
0
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...
0
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,...
0
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...

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.