[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
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 :)
Re: [2008] Datagridview row issue
Use the BindingSource, not the grid.
Re: [2008] Datagridview row issue
please show me how jm:wave:
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?
Re: [2008] Datagridview row issue
ok, i've added a bindingsource and linked it to my dgv:D
Code:
dgvCalls.DataSource = CallBindingSource
CallBindingSource.DataSource = calltable
How should I handle the next step?
Re: [2008] Datagridview row issue
What occurrences do you want to be notified of?
Quote:
I want to be able to add,edit and delete.
What events does the BindingSource have that would provide notification of those occurrences?
Re: [2008] Datagridview row issue
ok i understand so far:p , 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:blush:
Re: [2008] Datagridview row issue
guys please help. i'm stuck:(
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.
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.:p
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
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
Re: [2008] Datagridview row issue
please help me. i am almost there