|
-
Jul 25th, 2008, 04:52 AM
#1
Thread Starter
PowerPoster
[2008] Datagridview row issue
Hi Guys,
I have a dgv and I want to be able to add,edit and delete. I've got my code for updating to work. I grab the Id from colum1 of the row to update and write my updatecommand accordingly.
On the currentcellchange event I am grabbing the Id.
But when I am adding a new row this event is fired and bcos there is no Id in there an error is trigerred. How can I fix this. On which event should I grab the Id
-
Jul 25th, 2008, 05:00 AM
#2
Re: [2008] Datagridview row issue
I guess you could implement a system that checks a boolean value to see if you are adding a new row or editing an existing one and then before you clal the code to add a new row you just set this to True. Then if the boolean value is true you dont grab the ID and if its false then you do. Obviously you would need to reset this back to false after a new row is added to be ready for a cell change event.
However, I'm sure there will be a tidier/better way of doing it that someone with more experience will explain to you
-
Jul 25th, 2008, 08:21 AM
#3
Re: [2008] Datagridview row issue
Use the BindingSource, not the grid.
-
Jul 25th, 2008, 08:36 AM
#4
Thread Starter
PowerPoster
Re: [2008] Datagridview row issue
please show me how jm
-
Jul 25th, 2008, 08:19 PM
#5
Re: [2008] Datagridview row issue
Have you read the documentation for the BindingSource class? What events does it provide that might be useful to you? What other members does it provide that might be useful to you?
-
Jul 28th, 2008, 02:15 AM
#6
Thread Starter
PowerPoster
Re: [2008] Datagridview row issue
ok, i've added a bindingsource and linked it to my dgv
Code:
dgvCalls.DataSource = CallBindingSource
CallBindingSource.DataSource = calltable
How should I handle the next step?
-
Jul 28th, 2008, 02:17 AM
#7
Re: [2008] Datagridview row issue
What occurrences do you want to be notified of?
I want to be able to add,edit and delete.
What events does the BindingSource have that would provide notification of those occurrences?
-
Jul 28th, 2008, 02:32 AM
#8
Thread Starter
PowerPoster
Re: [2008] Datagridview row issue
ok i understand so far , I know i'm being a pest, but I'm a bit swamped with work right now. I have been doing some research tho. I would love it if you could show me the usage of the addnew for example and where I should place my code. sory
-
Jul 28th, 2008, 06:02 AM
#9
Thread Starter
PowerPoster
Re: [2008] Datagridview row issue
guys please help. i'm stuck
-
Jul 28th, 2008, 07:38 AM
#10
Addicted Member
Re: [2008] Datagridview row issue
I may get looked down upon for saying this, but this is how I would do it.
The first thing you're going to need is a For/Next loop (so that you can loop through each row). I would set up a simple Select statement to check for the record. If the record already exists, set the editing boolean to true, then use your update statement for that particular row. Remember to set the booleans back to false before going to the next row. Here's a quick and dirty example.
Code:
dim blnAdd as Boolean
dim blnEdit as Boolean
For each row in dgvRows
'Place Select statement here
'if the query returned a row then
blnEdit = True
'Use update query here
Else 'The query returned 0 rows
blnAdd = True
'Use Add query here
End if
blnAdd=False
blnEdit = False
Next
For added fun, you can add a blnDelete and make one nice For/Next loop for all your datagridview needs.
If my post helped you, please rate it!
Languages: VB/ASP.NET 2005, C# 2008,VB6
Databases: Oracle (knowledge not currently in use), DB2
FROM Customers
WHERE We_Know_What_We_Want <> DB.Null
SELECT *
0 rows returned
-
Jul 28th, 2008, 07:53 AM
#11
Thread Starter
PowerPoster
Re: [2008] Datagridview row issue
thanks for the suggestion Blakk_majik,
but i finally think I am getting somewhere. JMCilhinney, please assist me.
I figured out how to add event handlers for the Bindingsource.
I want the following scenario. when editing an exixting row, I want to pull the callid of the current row. so when I am updating I will use that callid for my update commands.
Code:
Private Sub CallBindingSource_CurrentChanged(ByVal sender As Object, ByVal e As EventArgs) _
Handles CallBindingSource.CurrentChanged
MessageBox.Show("You are in the BindingSource.CurrentChanged event.")
End Sub
Private Sub CallBindingSource_AddingNew(ByVal sender As Object, ByVal e As EventArgs) _
Handles CallBindingSource.AddingNew
MessageBox.Show("You are in the BindingSource.Addingnew event.")
End Sub
Private Sub CallBindingSource_PositionChanged(ByVal sender As Object, ByVal e As EventArgs) _
Handles CallBindingSource.PositionChanged
MessageBox.Show("You are in the BindingSource.PositionChanged event.")
End Sub
-
Jul 28th, 2008, 09:25 AM
#12
Thread Starter
PowerPoster
Re: [2008] Datagridview row issue
also I am using the class from the smdn website to show datepickers in a column. But when I am adding a new row I want todays date to appear in that column for the new row, but somehow when I click on the cell the date goes away and i get an error cos it is now blank. please help
-
Jul 29th, 2008, 01:03 AM
#13
Thread Starter
PowerPoster
Re: [2008] Datagridview row issue
please help me. i am almost there
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|