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

Saving changes made in DataGridView

I've been searching forever for examples of saving data changes in a
DataGridView. There's all kinds of examples, but none really show how
to save changes. Someone please help me.

I have a Windows Forms program with a DataGridView and a BindingSource
added to a form. Here is the code I'm using to populate the grid:
Dim CRClassesTableAdapter As SqlDataAdapter
Dim CRClassesTable As New DataTable()

Dim connectionString As New SqlConnection("Initial Catalog=" &
My.Settings.Database & ";Data Source=" & My.Settings.Server &
";Integrated Security=SSPI;")

CRClassesTableAdapter = New SqlDataAdapter("Select * from CRClasses",
connectionString)

CRClassesTableAdapter.Fill(CRClassesTable)

BindingSource1.DataSource = CRClassesTable

BindingSource1.Filter = "fldCLStatus <'Closed' AND fldStartDate >=
'" & medStart.Text & "' AND fldStartDate <= '" & medEnd.Text & "'"

BindingSource1.Sort = "fldStartDate, fldClassName"

Grid1.DataSource = BindingSource1
Databse is in SQL Server 2000. I'm using VB2005. I would like each row
to update once that row has lost focus. How would I do it?
Aug 25 '06 #1
11 27257

Kevin wrote:
I've been searching forever for examples of saving data changes in a
DataGridView. There's all kinds of examples, but none really show how
to save changes. Someone please help me.

I have a Windows Forms program with a DataGridView and a BindingSource
added to a form. Here is the code I'm using to populate the grid:
Dim CRClassesTableAdapter As SqlDataAdapter
Dim CRClassesTable As New DataTable()

Dim connectionString As New SqlConnection("Initial Catalog=" &
My.Settings.Database & ";Data Source=" & My.Settings.Server &
";Integrated Security=SSPI;")

CRClassesTableAdapter = New SqlDataAdapter("Select * from CRClasses",
connectionString)

CRClassesTableAdapter.Fill(CRClassesTable)

BindingSource1.DataSource = CRClassesTable

BindingSource1.Filter = "fldCLStatus <'Closed' AND fldStartDate >=
'" & medStart.Text & "' AND fldStartDate <= '" & medEnd.Text & "'"

BindingSource1.Sort = "fldStartDate, fldClassName"

Grid1.DataSource = BindingSource1
Databse is in SQL Server 2000. I'm using VB2005. I would like each row
to update once that row has lost focus. How would I do it?
Look at the DataAdaptor's UpdateCommand.

B.

Aug 25 '06 #2
On 25 Aug 2006 08:11:22 -0700, "Brian Tkatch"
<Ma***********@ThePentagon.comwrote:
>
Kevin wrote:
>I've been searching forever for examples of saving data changes in a
DataGridView. There's all kinds of examples, but none really show how
to save changes. Someone please help me.

I have a Windows Forms program with a DataGridView and a BindingSource
added to a form. Here is the code I'm using to populate the grid:
Dim CRClassesTableAdapter As SqlDataAdapter
Dim CRClassesTable As New DataTable()

Dim connectionString As New SqlConnection("Initial Catalog=" &
My.Settings.Database & ";Data Source=" & My.Settings.Server &
";Integrated Security=SSPI;")

CRClassesTableAdapter = New SqlDataAdapter("Select * from CRClasses",
connectionString)

CRClassesTableAdapter.Fill(CRClassesTable)

BindingSource1.DataSource = CRClassesTable

BindingSource1.Filter = "fldCLStatus <'Closed' AND fldStartDate >=
'" & medStart.Text & "' AND fldStartDate <= '" & medEnd.Text & "'"

BindingSource1.Sort = "fldStartDate, fldClassName"

Grid1.DataSource = BindingSource1
Databse is in SQL Server 2000. I'm using VB2005. I would like each row
to update once that row has lost focus. How would I do it?

Look at the DataAdaptor's UpdateCommand.

B.

That's what I've tried doing, but I just get errors. I'm trying to do
this:

Me.Validate()
Me.BindingSource1.EndEdit()
CRClassesTableAdapter.Update(CRClassesTable)

I get the error: "Update requires a valid UpdateCommand when passed
DataRow collection with modified rows."
What I'm looking for is a CODE EXAMPLE.
Aug 25 '06 #3
Kevin,
As you don't use the designer than you have to build those yourself. By
instance this

http://www.vb-tips.com/dbPages.aspx?...2-d7c12bbb3726

However if your select is easy you can as well use the commandbuilder.

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor
"Kevin" <ke****@cfl.rr.comschreef in bericht
news:5k********************************@4ax.com...
On 25 Aug 2006 08:11:22 -0700, "Brian Tkatch"
<Ma***********@ThePentagon.comwrote:
>>
Kevin wrote:
>>I've been searching forever for examples of saving data changes in a
DataGridView. There's all kinds of examples, but none really show how
to save changes. Someone please help me.

I have a Windows Forms program with a DataGridView and a BindingSource
added to a form. Here is the code I'm using to populate the grid:
Dim CRClassesTableAdapter As SqlDataAdapter
Dim CRClassesTable As New DataTable()

Dim connectionString As New SqlConnection("Initial Catalog=" &
My.Settings.Database & ";Data Source=" & My.Settings.Server &
";Integrated Security=SSPI;")

CRClassesTableAdapter = New SqlDataAdapter("Select * from CRClasses",
connectionString)

CRClassesTableAdapter.Fill(CRClassesTable)

BindingSource1.DataSource = CRClassesTable

BindingSource1.Filter = "fldCLStatus <'Closed' AND fldStartDate >=
'" & medStart.Text & "' AND fldStartDate <= '" & medEnd.Text & "'"

BindingSource1.Sort = "fldStartDate, fldClassName"

Grid1.DataSource = BindingSource1
Databse is in SQL Server 2000. I'm using VB2005. I would like each row
to update once that row has lost focus. How would I do it?

Look at the DataAdaptor's UpdateCommand.

B.


That's what I've tried doing, but I just get errors. I'm trying to do
this:

Me.Validate()
Me.BindingSource1.EndEdit()
CRClassesTableAdapter.Update(CRClassesTable)

I get the error: "Update requires a valid UpdateCommand when passed
DataRow collection with modified rows."
What I'm looking for is a CODE EXAMPLE.

Aug 25 '06 #4
I tried it your way Cor. I went to Data Add New Data Source... and
added my CRClasses table. I then dragged it onto my form and it
created its own DataGridView and menu bar. I deleted the menu bar and
put my own "Save Changes" button on the form. I cut and pasted the
code from the original save button on the menu bar to my button--the
following three lines:

Me.Validate()
Me.CRClassesBindingSource.EndEdit()
Me.CRClassesTableAdapter1.Update(Me.CFDataSQLDataS et.CRClasses)

When I press the button, I'm getting the same error I got before:

"Update requires a valid UpdateCommand when passed DataRow collection
with modified rows."

Any suggestions?

I'm adding a Filter and a Sort to my CRClassesBindingSource. Could
that have anything to do with it?


On Fri, 25 Aug 2006 18:03:46 +0200, "Cor Ligthert [MVP]"
<no************@planet.nlwrote:
>Kevin,
As you don't use the designer than you have to build those yourself. By
instance this

http://www.vb-tips.com/dbPages.aspx?...2-d7c12bbb3726

However if your select is easy you can as well use the commandbuilder.

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor
"Kevin" <ke****@cfl.rr.comschreef in bericht
news:5k********************************@4ax.com.. .
>On 25 Aug 2006 08:11:22 -0700, "Brian Tkatch"
<Ma***********@ThePentagon.comwrote:
>>>
Kevin wrote:
I've been searching forever for examples of saving data changes in a
DataGridView. There's all kinds of examples, but none really show how
to save changes. Someone please help me.

I have a Windows Forms program with a DataGridView and a BindingSource
added to a form. Here is the code I'm using to populate the grid:
Dim CRClassesTableAdapter As SqlDataAdapter
Dim CRClassesTable As New DataTable()

Dim connectionString As New SqlConnection("Initial Catalog=" &
My.Settings.Database & ";Data Source=" & My.Settings.Server &
";Integrated Security=SSPI;")

CRClassesTableAdapter = New SqlDataAdapter("Select * from CRClasses",
connectionString)

CRClassesTableAdapter.Fill(CRClassesTable)

BindingSource1.DataSource = CRClassesTable

BindingSource1.Filter = "fldCLStatus <'Closed' AND fldStartDate >=
'" & medStart.Text & "' AND fldStartDate <= '" & medEnd.Text & "'"

BindingSource1.Sort = "fldStartDate, fldClassName"

Grid1.DataSource = BindingSource1
Databse is in SQL Server 2000. I'm using VB2005. I would like each row
to update once that row has lost focus. How would I do it?

Look at the DataAdaptor's UpdateCommand.

B.


That's what I've tried doing, but I just get errors. I'm trying to do
this:

Me.Validate()
Me.BindingSource1.EndEdit()
CRClassesTableAdapter.Update(CRClassesTable)

I get the error: "Update requires a valid UpdateCommand when passed
DataRow collection with modified rows."
What I'm looking for is a CODE EXAMPLE.
Aug 25 '06 #5
Kevin,

You are sure you have done all defaults, and got no errors using that.

This seems something of missing a primary key in your select, but with the
default from that drag method you cannot change that.

Cor

"Kevin" <ke****@cfl.rr.comschreef in bericht
news:2i********************************@4ax.com...
>I tried it your way Cor. I went to Data Add New Data Source... and
added my CRClasses table. I then dragged it onto my form and it
created its own DataGridView and menu bar. I deleted the menu bar and
put my own "Save Changes" button on the form. I cut and pasted the
code from the original save button on the menu bar to my button--the
following three lines:

Me.Validate()
Me.CRClassesBindingSource.EndEdit()
Me.CRClassesTableAdapter1.Update(Me.CFDataSQLDataS et.CRClasses)

When I press the button, I'm getting the same error I got before:

"Update requires a valid UpdateCommand when passed DataRow collection
with modified rows."

Any suggestions?

I'm adding a Filter and a Sort to my CRClassesBindingSource. Could
that have anything to do with it?


On Fri, 25 Aug 2006 18:03:46 +0200, "Cor Ligthert [MVP]"
<no************@planet.nlwrote:
>>Kevin,
As you don't use the designer than you have to build those yourself. By
instance this

http://www.vb-tips.com/dbPages.aspx?...2-d7c12bbb3726

However if your select is easy you can as well use the commandbuilder.

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor
"Kevin" <ke****@cfl.rr.comschreef in bericht
news:5k********************************@4ax.com. ..
>>On 25 Aug 2006 08:11:22 -0700, "Brian Tkatch"
<Ma***********@ThePentagon.comwrote:
Kevin wrote:
I've been searching forever for examples of saving data changes in a
DataGridView. There's all kinds of examples, but none really show how
to save changes. Someone please help me.
>
I have a Windows Forms program with a DataGridView and a BindingSource
added to a form. Here is the code I'm using to populate the grid:
>
>
Dim CRClassesTableAdapter As SqlDataAdapter
Dim CRClassesTable As New DataTable()
>
Dim connectionString As New SqlConnection("Initial Catalog=" &
My.Settings.Database & ";Data Source=" & My.Settings.Server &
";Integrated Security=SSPI;")
>
CRClassesTableAdapter = New SqlDataAdapter("Select * from CRClasses",
connectionString)
>
CRClassesTableAdapter.Fill(CRClassesTable)
>
BindingSource1.DataSource = CRClassesTable
>
BindingSource1.Filter = "fldCLStatus <'Closed' AND fldStartDate >=
'" & medStart.Text & "' AND fldStartDate <= '" & medEnd.Text & "'"
>
BindingSource1.Sort = "fldStartDate, fldClassName"
>
Grid1.DataSource = BindingSource1
>
>
Databse is in SQL Server 2000. I'm using VB2005. I would like each row
to update once that row has lost focus. How would I do it?

Look at the DataAdaptor's UpdateCommand.

B.
That's what I've tried doing, but I just get errors. I'm trying to do
this:

Me.Validate()
Me.BindingSource1.EndEdit()
CRClassesTableAdapter.Update(CRClassesTable)

I get the error: "Update requires a valid UpdateCommand when passed
DataRow collection with modified rows."
What I'm looking for is a CODE EXAMPLE.

Aug 26 '06 #6
Here is some code I did for a friend to demonstrate populating a
datagridview from a database table with the ability to save changes. Change
the connection string to suit your situation. The command builder is what
creates the update command for you.

Create a form and add a datagridview, but do all the data connection work in
code as below. The save command is in a menustrip.
Public Class Form2
Dim MyConnection As New SqlClient.SqlConnection
Dim MyAdapter As SqlClient.SqlDataAdapter
Protected ds As New DataSet
Protected cb As SqlClient.SqlCommandBuilder

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

MyConnection.ConnectionString = "Data
Source=.\SQLEXPRESS;AttachDBFileName='C:\temp\Test .mdf';Integrated
Security=SSPI"
MyAdapter = New SqlClient.SqlDataAdapter("Select * from
AddressTable", MyConnection)
MyAdapter.Fill(ds)
cb = New SqlClient.SqlCommandBuilder(MyAdapter) 'in case we want to
do any inserts, deletions, etc.
Me.DataGridView1.DataSource = ds.Tables(0)

End Sub

Private Sub SaveDataToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
SaveDataToolStripMenuItem.Click
MyAdapter.Update(ds)
End Sub
End Class

"Kevin" <ke****@cfl.rr.comwrote in message
news:3p********************************@4ax.com...
I've been searching forever for examples of saving data changes in a
DataGridView. There's all kinds of examples, but none really show how
to save changes. Someone please help me.


Aug 27 '06 #7
Finally something that works! Thank you William.
On Sun, 27 Aug 2006 12:46:40 -0400, "William LaMartin"
<la******@tampabay.rr.comwrote:
>Here is some code I did for a friend to demonstrate populating a
datagridview from a database table with the ability to save changes. Change
the connection string to suit your situation. The command builder is what
creates the update command for you.

Create a form and add a datagridview, but do all the data connection work in
code as below. The save command is in a menustrip.
Public Class Form2
Dim MyConnection As New SqlClient.SqlConnection
Dim MyAdapter As SqlClient.SqlDataAdapter
Protected ds As New DataSet
Protected cb As SqlClient.SqlCommandBuilder

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

MyConnection.ConnectionString = "Data
Source=.\SQLEXPRESS;AttachDBFileName='C:\temp\Tes t.mdf';Integrated
Security=SSPI"
MyAdapter = New SqlClient.SqlDataAdapter("Select * from
AddressTable", MyConnection)
MyAdapter.Fill(ds)
cb = New SqlClient.SqlCommandBuilder(MyAdapter) 'in case we want to
do any inserts, deletions, etc.
Me.DataGridView1.DataSource = ds.Tables(0)

End Sub

Private Sub SaveDataToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
SaveDataToolStripMenuItem.Click
MyAdapter.Update(ds)
End Sub
End Class

"Kevin" <ke****@cfl.rr.comwrote in message
news:3p********************************@4ax.com.. .
>I've been searching forever for examples of saving data changes in a
DataGridView. There's all kinds of examples, but none really show how
to save changes. Someone please help me.

Aug 28 '06 #8
Kevin,

I am sorry to have to say this.

William made a fine sample for you, but his code is almost exactly in this
link that I have showed you in this thread.

http://msdn.microsoft.com/library/de...classtopic.asp

Not that Williame did copied it, Ken and me have for sure as well this in a
way on our website (not in this simple way).

But please will you look at the help we give you. You did confuse me,
because you told you were using what I had said, but that was probably about
a reply from some days ago.

Cor

"Kevin" <Ke****@nospam.cfl.rr.comschreef in bericht
news:39********************************@4ax.com...
Finally something that works! Thank you William.
On Sun, 27 Aug 2006 12:46:40 -0400, "William LaMartin"
<la******@tampabay.rr.comwrote:
>>Here is some code I did for a friend to demonstrate populating a
datagridview from a database table with the ability to save changes.
Change
the connection string to suit your situation. The command builder is what
creates the update command for you.

Create a form and add a datagridview, but do all the data connection work
in
code as below. The save command is in a menustrip.
Public Class Form2
Dim MyConnection As New SqlClient.SqlConnection
Dim MyAdapter As SqlClient.SqlDataAdapter
Protected ds As New DataSet
Protected cb As SqlClient.SqlCommandBuilder

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

MyConnection.ConnectionString = "Data
Source=.\SQLEXPRESS;AttachDBFileName='C:\temp\Te st.mdf';Integrated
Security=SSPI"
MyAdapter = New SqlClient.SqlDataAdapter("Select * from
AddressTable", MyConnection)
MyAdapter.Fill(ds)
cb = New SqlClient.SqlCommandBuilder(MyAdapter) 'in case we want
to
do any inserts, deletions, etc.
Me.DataGridView1.DataSource = ds.Tables(0)

End Sub

Private Sub SaveDataToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
SaveDataToolStripMenuItem.Click
MyAdapter.Update(ds)
End Sub
End Class

"Kevin" <ke****@cfl.rr.comwrote in message
news:3p********************************@4ax.com. ..
>>I've been searching forever for examples of saving data changes in a
DataGridView. There's all kinds of examples, but none really show how
to save changes. Someone please help me.


Aug 28 '06 #9
I believe your first suggestion was to add a new DataSource to my
project and then drag it to my Form, which would create a DataGridView
on my Form. That's the example I tried. When it would do what I wanted
it to do, I came back here to see other suggestions. That's when I saw
William's response, so I tried that before I even got to your other
suggestion.

Maybe you could help me with my next problem?

I'm using William's code to fill my DataGridView when the user presses
a button. I've set up the fields manually in the DataGridView, setting
the DataSource property to each field in my table.

The problem is, when the user presses the button again, the
DataGridView doesn't clear the old rows, it just adds to them. So I
tried setting the Grid's DataSource to DBNull. Not only did I wipe out
all my manual field settings, but the old rows are still there. How
can I clear the DataGridView?
On Mon, 28 Aug 2006 06:34:48 +0200, "Cor Ligthert [MVP]"
<no************@planet.nlwrote:
>Kevin,

I am sorry to have to say this.

William made a fine sample for you, but his code is almost exactly in this
link that I have showed you in this thread.

http://msdn.microsoft.com/library/de...classtopic.asp

Not that Williame did copied it, Ken and me have for sure as well this in a
way on our website (not in this simple way).

But please will you look at the help we give you. You did confuse me,
because you told you were using what I had said, but that was probably about
a reply from some days ago.

Cor

"Kevin" <Ke****@nospam.cfl.rr.comschreef in bericht
news:39********************************@4ax.com.. .
>Finally something that works! Thank you William.
On Sun, 27 Aug 2006 12:46:40 -0400, "William LaMartin"
<la******@tampabay.rr.comwrote:
>>>Here is some code I did for a friend to demonstrate populating a
datagridview from a database table with the ability to save changes.
Change
the connection string to suit your situation. The command builder is what
creates the update command for you.

Create a form and add a datagridview, but do all the data connection work
in
code as below. The save command is in a menustrip.
Public Class Form2
Dim MyConnection As New SqlClient.SqlConnection
Dim MyAdapter As SqlClient.SqlDataAdapter
Protected ds As New DataSet
Protected cb As SqlClient.SqlCommandBuilder

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

MyConnection.ConnectionString = "Data
Source=.\SQLEXPRESS;AttachDBFileName='C:\temp\T est.mdf';Integrated
Security=SSPI"
MyAdapter = New SqlClient.SqlDataAdapter("Select * from
AddressTable", MyConnection)
MyAdapter.Fill(ds)
cb = New SqlClient.SqlCommandBuilder(MyAdapter) 'in case we want
to
do any inserts, deletions, etc.
Me.DataGridView1.DataSource = ds.Tables(0)

End Sub

Private Sub SaveDataToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
SaveDataToolStripMenuItem.Click
MyAdapter.Update(ds)
End Sub
End Class

"Kevin" <ke****@cfl.rr.comwrote in message
news:3p********************************@4ax.com ...
I've been searching forever for examples of saving data changes in a
DataGridView. There's all kinds of examples, but none really show how
to save changes. Someone please help me.
Aug 28 '06 #10
Kevin,

The datagridview shows the dataset, as you fill it more times, it will add
only to that, so if that load code from william is in that button event,
than you have to clear the dataset before.

ds.clear

I hope this helps,

Cor

"Kevin" <ke****@cfl.rr.comschreef in bericht
news:1n********************************@4ax.com...
>I believe your first suggestion was to add a new DataSource to my
project and then drag it to my Form, which would create a DataGridView
on my Form. That's the example I tried. When it would do what I wanted
it to do, I came back here to see other suggestions. That's when I saw
William's response, so I tried that before I even got to your other
suggestion.

Maybe you could help me with my next problem?

I'm using William's code to fill my DataGridView when the user presses
a button. I've set up the fields manually in the DataGridView, setting
the DataSource property to each field in my table.

The problem is, when the user presses the button again, the
DataGridView doesn't clear the old rows, it just adds to them. So I
tried setting the Grid's DataSource to DBNull. Not only did I wipe out
all my manual field settings, but the old rows are still there. How
can I clear the DataGridView?
On Mon, 28 Aug 2006 06:34:48 +0200, "Cor Ligthert [MVP]"
<no************@planet.nlwrote:
>>Kevin,

I am sorry to have to say this.

William made a fine sample for you, but his code is almost exactly in this
link that I have showed you in this thread.

http://msdn.microsoft.com/library/de...classtopic.asp

Not that Williame did copied it, Ken and me have for sure as well this in
a
way on our website (not in this simple way).

But please will you look at the help we give you. You did confuse me,
because you told you were using what I had said, but that was probably
about
a reply from some days ago.

Cor

"Kevin" <Ke****@nospam.cfl.rr.comschreef in bericht
news:39********************************@4ax.com. ..
>>Finally something that works! Thank you William.
On Sun, 27 Aug 2006 12:46:40 -0400, "William LaMartin"
<la******@tampabay.rr.comwrote:

Here is some code I did for a friend to demonstrate populating a
datagridview from a database table with the ability to save changes.
Change
the connection string to suit your situation. The command builder is
what
creates the update command for you.

Create a form and add a datagridview, but do all the data connection
work
in
code as below. The save command is in a menustrip.
Public Class Form2
Dim MyConnection As New SqlClient.SqlConnection
Dim MyAdapter As SqlClient.SqlDataAdapter
Protected ds As New DataSet
Protected cb As SqlClient.SqlCommandBuilder

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

MyConnection.ConnectionString = "Data
Source=.\SQLEXPRESS;AttachDBFileName='C:\temp\ Test.mdf';Integrated
Security=SSPI"
MyAdapter = New SqlClient.SqlDataAdapter("Select * from
AddressTable", MyConnection)
MyAdapter.Fill(ds)
cb = New SqlClient.SqlCommandBuilder(MyAdapter) 'in case we want
to
do any inserts, deletions, etc.
Me.DataGridView1.DataSource = ds.Tables(0)

End Sub

Private Sub SaveDataToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
SaveDataToolStripMenuItem.Click
MyAdapter.Update(ds)
End Sub
End Class

"Kevin" <ke****@cfl.rr.comwrote in message
news:3p********************************@4ax.co m...
I've been searching forever for examples of saving data changes in a
DataGridView. There's all kinds of examples, but none really show how
to save changes. Someone please help me.
>
>

Aug 28 '06 #11
Thanks Cor. That's why you're the MVP.

On Mon, 28 Aug 2006 18:51:25 +0200, "Cor Ligthert [MVP]"
<no************@planet.nlwrote:
>Kevin,

The datagridview shows the dataset, as you fill it more times, it will add
only to that, so if that load code from william is in that button event,
than you have to clear the dataset before.

ds.clear

I hope this helps,

Cor

"Kevin" <ke****@cfl.rr.comschreef in bericht
news:1n********************************@4ax.com.. .
>>I believe your first suggestion was to add a new DataSource to my
project and then drag it to my Form, which would create a DataGridView
on my Form. That's the example I tried. When it would do what I wanted
it to do, I came back here to see other suggestions. That's when I saw
William's response, so I tried that before I even got to your other
suggestion.

Maybe you could help me with my next problem?

I'm using William's code to fill my DataGridView when the user presses
a button. I've set up the fields manually in the DataGridView, setting
the DataSource property to each field in my table.

The problem is, when the user presses the button again, the
DataGridView doesn't clear the old rows, it just adds to them. So I
tried setting the Grid's DataSource to DBNull. Not only did I wipe out
all my manual field settings, but the old rows are still there. How
can I clear the DataGridView?
On Mon, 28 Aug 2006 06:34:48 +0200, "Cor Ligthert [MVP]"
<no************@planet.nlwrote:
>>>Kevin,

I am sorry to have to say this.

William made a fine sample for you, but his code is almost exactly in this
link that I have showed you in this thread.

http://msdn.microsoft.com/library/de...classtopic.asp

Not that Williame did copied it, Ken and me have for sure as well this in
a
way on our website (not in this simple way).

But please will you look at the help we give you. You did confuse me,
because you told you were using what I had said, but that was probably
about
a reply from some days ago.

Cor

"Kevin" <Ke****@nospam.cfl.rr.comschreef in bericht
news:39********************************@4ax.com ...
Finally something that works! Thank you William.
On Sun, 27 Aug 2006 12:46:40 -0400, "William LaMartin"
<la******@tampabay.rr.comwrote:

>Here is some code I did for a friend to demonstrate populating a
>datagridview from a database table with the ability to save changes.
>Change
>the connection string to suit your situation. The command builder is
>what
>creates the update command for you.
>
>Create a form and add a datagridview, but do all the data connection
>work
>in
>code as below. The save command is in a menustrip.
>
>
>Public Class Form2
Dim MyConnection As New SqlClient.SqlConnection
Dim MyAdapter As SqlClient.SqlDataAdapter
Protected ds As New DataSet
Protected cb As SqlClient.SqlCommandBuilder
>
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
>System.EventArgs) Handles MyBase.Load
>
MyConnection.ConnectionString = "Data
>Source=.\SQLEXPRESS;AttachDBFileName='C:\temp \Test.mdf';Integrated
>Security=SSPI"
MyAdapter = New SqlClient.SqlDataAdapter("Select * from
>AddressTable", MyConnection)
MyAdapter.Fill(ds)
cb = New SqlClient.SqlCommandBuilder(MyAdapter) 'in case we want
to
>do any inserts, deletions, etc.
Me.DataGridView1.DataSource = ds.Tables(0)
>
End Sub
>
Private Sub SaveDataToolStripMenuItem_Click(ByVal sender As
>System.Object, ByVal e As System.EventArgs) Handles
>SaveDataToolStripMenuItem.Click
MyAdapter.Update(ds)
End Sub
>End Class
>
>"Kevin" <ke****@cfl.rr.comwrote in message
>news:3p********************************@4ax.c om...
>I've been searching forever for examples of saving data changes in a
>DataGridView. There's all kinds of examples, but none really show how
>to save changes. Someone please help me.
>>
>>
>
Aug 28 '06 #12

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

Similar topics

1
by: Jagdip Singh Ajimal | last post by:
I have an application that connects and edits a database's tables. My question is, what is the best way to save who did what changes to this database? I need to be able to display this in the...
4
by: Jonathan Wood | last post by:
Okay, I've normally avoided a lot of database stuff but I can see that'll be hard with .NET. I created an Access database and have successfully bound TextBoxes to fields within that database....
6
by: mike11d11 | last post by:
I'm trying to create an application that will have multiple users working off a table on a SQL server. Since multi users will be updating different records at any given moment, how can i get those...
1
by: mankolele | last post by:
Hi all Is there someway to track changes made to data in a mysql database? Like someone changes a phone number in a record on a form -Is there a way to find out what changes were made to what...
6
by: lucyh3h | last post by:
Hi, In one of my pages, I use javascript (AJAX) to populate one mulitple select field based on user's click event on another control. I noticed that when I navigate back to this page by clicking...
3
by: =?Utf-8?B?QnJhbmRvbg==?= | last post by:
Hi, I have an aspx page that has the "include" code in it which includes another page that displays information. I also have an upload page that allows users to upload a simple html document...
1
by: =?Utf-8?B?QnJhbmRvbg==?= | last post by:
Hi, I have an aspx page that contains an include section which calls an html page for text to be displayed on the aspx page. I also have an upload page that allows users to upload a simple html...
5
by: vovan | last post by:
I have set of controls (Textboxes, checkboxes etc) along with the Grid on Windows Form. I use BindingSource to populate both Grid and the set of Controls. User selects the record in the grid and...
1
by: amitjaura | last post by:
Well i have a datagridview in my application and i want it to reflect any changes made in server database instantly with some trigger or so? I have a choice to use timer and get the desired results...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
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
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.