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

Problem with datagridview

Hi all,

This is the situation...

DatagridView Control with datasource set to datatable.

I want the user to be able to delete x number of rows by selecting a row
with mouse then clicking a 'delete' button.

Then user clicks a 'Commit' button to update the database.

This is my delete row code:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
If Me.DataGridView1.SelectedRows.Count = 0 Then Exit Sub
Dim x As Integer = bs.Position
udt.Item(x).Delete()
End Sub

This is my commit changes code:

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

The problem is that once I have deleted a row, the datatable and
datagridview become unsynchronized and the selected row in the datagrid
isn't the one that will be deleted from the dataset. (I hope that makes
sense)

I can keep things in sync by calling datatable.acceptchanges() after the row
deletion, but then the tableadapter.update() command doesn't write the
changes to the database.

Any helpful suggestions will be greatly appreciated. I can post the complete
(simplified) example if it will help.

Kind regards,

Martin.
Dec 16 '06 #1
2 4678
Martin,

Just something how a datatable is working, I think that you than will easily
handle your problem.

A datatable hold rows. All the rows have rowstatuses which have statuses.
This can be deleted, changed new, unchanged (and one other one but for this
not important).

If you do an update all rows with a status deleted, changed and new are
handled, they use than the SQL commands, Delete, Update and Insert. The
nonchanged rows are not handled. The state of the row when it was retrieved
using the select is hold as well in the datarow. This is checked to the
currentstate of the datatabase. If there is a difference between those,
there is a currencyerror.

If the rows are handled correct than the dataadapter.update command has an
inbuild AcceptChanges command. What AcceptChanges does is setting all rows
to unchanged and removes the change information. By that it becomes again
equal to the current state of the database.

Be aware that this is very confusing in your code, if this is true I would
change it as quick as possible.

udt.Item(x).Delete(). I can not find what this means.Be also aware that you
should do deletes in a collection always bottom up, you will become sure in
problem if you don't doe that, because otherwise the position is changing.

I hope that this gives you the information to handle your problem.

Cor

"Martin" <ma****@martin.comschreef in bericht
news:B4*****************@newsfe4-win.ntli.net...
Hi all,

This is the situation...

DatagridView Control with datasource set to datatable.

I want the user to be able to delete x number of rows by selecting a row
with mouse then clicking a 'delete' button.

Then user clicks a 'Commit' button to update the database.

This is my delete row code:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
If Me.DataGridView1.SelectedRows.Count = 0 Then Exit Sub
Dim x As Integer = bs.Position
udt.Item(x).Delete()
End Sub

This is my commit changes code:

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

The problem is that once I have deleted a row, the datatable and
datagridview become unsynchronized and the selected row in the datagrid
isn't the one that will be deleted from the dataset. (I hope that makes
sense)

I can keep things in sync by calling datatable.acceptchanges() after the
row deletion, but then the tableadapter.update() command doesn't write the
changes to the database.

Any helpful suggestions will be greatly appreciated. I can post the
complete (simplified) example if it will help.

Kind regards,

Martin.

Dec 16 '06 #2
Hi Cor,

Thanks for the response, I agree, my example wasn't very clear, sorry about
that.

I have, however been tinkering since my first post and have come up with a
resonable solution; To the grid I add a non-databound extra column and once
the grid is populated I fill each cell in this column with the index of the
containing row. I then use this value to calculate which row to delete when
I select a row.

This approach enables me to delete rows in an arbitrary order and then
either commit or reject the changes as I wish.

I'm sure it's a design issue that has led to this problem in the first
place, but as a relative beginner to vb2005 and database programming in
general I'm still very much on a learning curve.

Once again, thanks,

Martin.
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Martin,

Just something how a datatable is working, I think that you than will
easily handle your problem.

A datatable hold rows. All the rows have rowstatuses which have statuses.
This can be deleted, changed new, unchanged (and one other one but for
this not important).

If you do an update all rows with a status deleted, changed and new are
handled, they use than the SQL commands, Delete, Update and Insert. The
nonchanged rows are not handled. The state of the row when it was
retrieved using the select is hold as well in the datarow. This is checked
to the currentstate of the datatabase. If there is a difference between
those, there is a currencyerror.

If the rows are handled correct than the dataadapter.update command has an
inbuild AcceptChanges command. What AcceptChanges does is setting all rows
to unchanged and removes the change information. By that it becomes again
equal to the current state of the database.

Be aware that this is very confusing in your code, if this is true I would
change it as quick as possible.

udt.Item(x).Delete(). I can not find what this means.Be also aware that
you should do deletes in a collection always bottom up, you will become
sure in problem if you don't doe that, because otherwise the position is
changing.

I hope that this gives you the information to handle your problem.

Cor

"Martin" <ma****@martin.comschreef in bericht
news:B4*****************@newsfe4-win.ntli.net...
>Hi all,

This is the situation...

DatagridView Control with datasource set to datatable.

I want the user to be able to delete x number of rows by selecting a row
with mouse then clicking a 'delete' button.

Then user clicks a 'Commit' button to update the database.

This is my delete row code:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
If Me.DataGridView1.SelectedRows.Count = 0 Then Exit Sub
Dim x As Integer = bs.Position
udt.Item(x).Delete()
End Sub

This is my commit changes code:

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

The problem is that once I have deleted a row, the datatable and
datagridview become unsynchronized and the selected row in the datagrid
isn't the one that will be deleted from the dataset. (I hope that makes
sense)

I can keep things in sync by calling datatable.acceptchanges() after the
row deletion, but then the tableadapter.update() command doesn't write
the changes to the database.

Any helpful suggestions will be greatly appreciated. I can post the
complete (simplified) example if it will help.

Kind regards,

Martin.


Dec 16 '06 #3

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

Similar topics

0
by: Pieter Coucke | last post by:
Hi, I have a DataGridView, that contains a list of Articles, which can be added (automaticly via the AllowUserToAddRows) and changed by the user. The current item is also displayed in textboxes...
1
by: sapkal.manish | last post by:
Question : How can I find actual row position of DataTable in DataGridView when DataTable is sorted / My source code like this Private WithEvent dgvInwDet as new DataGridView Private...
1
by: Rich | last post by:
Hello, I am reading data from a sql server table that is under replication. This table has the replicatin GUID column that is generated with replicatin. I am reading the data from a...
6
by: George | last post by:
Hi, I have been encountering a refresh problem with DataGridView, which is bound to a DataTable. When I make updates (Add, Delete, update) to the DataGridView, everything flow nicely to...
4
by: shibeta | last post by:
Hello, I have problem with DataGridView and BindingSource. I have created DataSet and added TableData to it with manualy created columns (without DataAdapter - I'm not using MSSQL). On the Form I...
1
by: Ahmed Yasser | last post by:
Hi all, i have a problem with the datagridview sorting, the problem is a bit complicated so i hope i can describe in the following steps: 1. i have a datagridview with two columns...
1
by: Jeff | last post by:
Hey ..NET 2.0 I've created a User Control which contain a DataGridView. This User Control is displayed on a TabPage. This TabPage is added to the TabControl during runtime. The problem is...
0
by: mianghous | last post by:
hi I have a little problem i m using a datagridview on ma aspx page and problem is that its size does not remain same... mean if i fecth 10 records it will show 10 and size will expand of the...
3
by: Andrus | last post by:
I have DataGridView in virtual mode containing 3500 rows. In code below, assigning to RowCount value to 3500 takes 8 seconds. CPU usage goes high at this time. Stepping by F11 into user code shows...
0
by: priyamtheone | last post by:
I'm trying to make a datagridview column to act like a datetimepicker column (C#.Net 2005). These are the behaviours that the dgv should have: 1) Initially all the cells of the dtp column should be...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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?
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.