I'm getting an error of There is no row at position 6. when I tried to delete row six from the datagrid. In fact, I tried all the rows in the datagrid and still receiving the same error message. Here's what tried before:
Dim dgRowNumber As Integer = dgComputer.CurrentRowIndex
Try
DSetProgram.Program.Rows(dgRowNumber).Delete()
Catch exError As Exception
MessageBox.Show(exError.Message)
End Try
DAdptProgram.Update(DSetProgram.Tables("Program"))
DSetProgram.Tables("Program").AcceptChanges()
But still it gives me the error that there is no row at the position of the variable "dgRowNumber". So I tried this:
dsUsrProfile.Tables("strTableName").Rows(dgRowNumber).Delete()
DAprofile.Update(dsUsrProfile.Tables("strTableName"))
dsUsrProfile.Tables("strTableName").AcceptChanges()
If (dsUsrProfile.HasChanges) Then
MsgBox("Changes has been committed")
End If
But the same error happened. However, I have a total of 6 rows in the table called "Program". In addition, when the program runs, the variable "dgRowNumber" do have an actual number that corresponds to the datagrid. If this variable would to contain some number that is not relating to the datagrid or out of the range then yes, I understand but it's not. It is within the range of the rows in the datagrid and database table.
Thanks for suggestion; however, that is not the problem. I tried your suggestion and it's still not working. The error is still the same. The number after the word "position" corresponds to whatever row I select in the datagrid but it just gives me an error. Like I said, it's not like the row number is out of range. It is still within the range.
I figured out a part of the problem is when I test the code on another dataset and dataadapter using the same datagrid; however, I don't know how to solve it or detect it. Here's what I found out.
In this statement:Dim dgRowNumber As Integer = dgComputer.CurrentRowIndex
The code suppose to find the row that I select to be deleted and it did show the correct row number I want to delete. In this statement: DSetProgram.Program.Rows(dgRowNumber).Delete() , the dgRowNumber still show the correct row number to be deleted. However, when the code finish executing, the record that was deleted wasn't the one that I specify to be deleted. I tried this a couple of times and it seems to be that the deletion was random or it appears to be random despite what row I specify to be delete!
So my question is, how/why this happen and how do I fix the problem?
Regarding the Random deletion; well, it's not true. It depends on the arrow of the header in each of the columns in the datagrid pointed at. If the arrow pointed up, then the deletion starts at the bottom. If the arrow pointed down, then the deletion starts at the top regardless of what row I specify to be deleted.
Any clue why this is happening and how do I fix this?
I think you're right. I need to validate my selected row by using real data. The question is I don't know how to do that. Mind showing me how or pointing me to an article that will show me how to do that?
Thanks for helping me out. Unfornately, I wasn't able to unzip that file. When I tried to unzip the file, the program gives me an error of "Bad header". I don't know what that means. Anywa, will you zip the project into .zip instead of .rar?
Again, thank you very much giving up your time to help me.
If I would to do it the way your sample showed me then that means I'm not using ADO.NET at all correct? I thought ADO.NET supose to make datagrid editing a lot easier. This is the manual way though. Because I'm using design mode to create my dataset and dataadapter, is it possible for me to just call one of those already generated queries to delete the row I want? I remember that there is a way; however, I don't remember how to call the delete SQL query instead of writing from scratch, since I'm jusing a dataset that was creaed at design time.
ADO.NET is the underlying structure for the databases , tables , columns ..etc . So , it's completely isolated from DataGrid or any other control . So as soon you declare any ADO.NET obj (connection , table , column..etc) , then you're using ADO.NET .
For me , I hate those database controls . I like to code them .
For the deleting issue , check the demo I posted above . It should show you how to use SQL Statement "DELETE" to delete a record .
Sorry if I'm still missing the point you're dragging my attention to .
I can do a manual delete with a SQL query. I thought it would be much easier just to specify which row to delete in the datagrid and the program will automatically delete it for me. Perhaps the DataGrid is not that useful then.
I don't know if you are doing something supernatural and this DataGrid is no way good for your current proj . From what I saw , it still works good . Why not just specifying a unique word or number and let it handled by this SQL string . I did it in a way , it builds the string easily avoiding error generations . I'm trying to force you to do that but what's the real problem now ?
Guess what? I got something weard going on here. Somehow it works if I created a textbox and just enter a row number into this textbox and leave everything the same; however, instead of using the CurrentRowIndex, I'm using the number from the textbox to make my deletion and it seems to work. Now, I have not test this on when I sort the datagrid. But my question is, why is this working?
What is ite? I tried without that and it still gives me the blue underline. I tried putting the word "Item" and still giving the same thing. for the "MyDataSet", I put my database Table name and still same blue underline. I tried putting the name of my DataSet in and the same thing happend.
dr = ds.Tables("tablename").Rows.RemoveAt("indexnumber")
since the parameter for RemoveAt has to be an integer and should match, the row number you are on the datagrid, usually the one you selected by clicking aand saving the row number for another event to remove
If what I'm looking for works then I want to try that route first. As far as SQL string, I know how to do that...the only problem I have right now is that I'm too far in the project that I don't want to start all over by writing every SQL string in my project just to accomodate the delete routine.
Yes thats right but just remember I have left out all the code to populate that datagrid first as written it would be ridiculous as you have nothing to remove.
Basic order here, create dataset and populate
Bind datagrid to dataset
You must already have done this in your application I feel sure
Now you have a datagrid populated with data and you want to remove one item
The basic technique I use, is to react to the click event on the datagrid and save the currentrow index in an integer variable
Then have what is effectively a remove button
When they click that, first of all I check that the saved current row index is selected and if so use the remove at method to remove it from the displayed items in the datagrid
In the meantime if you could look at my thread called the The downside of Application.Enablestyles and offer any insights, boy would I be grateful!!!
Your problem is covered in the VB.Net help although I know that VS 2003 is shippped without help and they tell you install MSDN help, yes all 2gb of it, I rely on the help I got with VS 2002 or MSDN itself and finally here.
RichardAtherton, thanks for the help. However, I have one more question before I implement the code. Why do I need to declare a New DataSet variable to put into this mydatarow = mydataset.tables(MyNewDataSet ).Rows.RemoveAt(currow). I already created a dataset and dataadapter at design time. So what's the purpose declaring the MyNewDataSet again?
maybe that was a crossover from my last post, no of course you don't have to create a new dataset,Ijust wrote some code that would not have any syntax errors.
I did assume that you already had a populated dataset and simply wanted to manipluate it.
First of all, any time when I do this:
dr5 = ds5.Tables.Item("RentAdjustmentsList").Rows(FindResult.Item) , with the datarow variable, I get a blue underline. Secondly, the RemoveAt dosn't work. It's not deleting the row in the datagrid. It only delete when I when use the word "Delete", but again, it's not deleting the selected row.
Many thanks for the effor though. I was going to attach my project to this thread; however, looks like this site only allow 100K and my zip file is over 300K.