I started touching VB.NET from November.
The data brought from DB is displayed in DataGridView.
I want to set the value in the specific cell of the previous row to the specific cell of the newly added row when I add a new row there.
The last line to be added.
At this time, I want to set the CLASS cell "J3" in the previous row to the same cell in the newly added row.
To add a row, prepare a "+ Add row" button on the screen so that a new row is added each time you click it.
button is pressed.Applicable source code
Private Sub NewRowsButton_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewRowsButton.Click Dim dr As DataRow = ClassList.NewRow ClassList.Rows.Add (dr) End Sub
Private Sub NewRowsButton_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewRowsButton.Click Dim dr As DataRow = ClassList.NewRow dr ("CLASS") = "J3" ClassList.Rows.Add (dr) End Sub
If i do the above, "J3" will appear in the CLASS cell when adding a row, but of course the previous row might not always be "J3", so it was no good.Additional questions
When deleting a newly added line,
Private Sub DeleteRowsButton_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteowsButton.Click Try 'DataGridView = mrs Dim ClassData As DataTable = CType (mrs.DataSource, DataTable) For i As Integer = 0 To ClassData.Rows.Count-1 'celCHECK is a selection box. If not selected and "0" is selected, it will change to "1". If ConvertInt32 (ClassData.Rows (i) ("CHECK")) = 0 Then Continue For ClassData.Rows.RemoveAt (i) Next Catch ex As Exception m_objLogFunc.WriteExceptionLog (ex) Finally 'Force return to screen getMenuBar () End Try End Sub
I have a deletion process like this.
For example, when there are 5 rows of data, when you delete the 3rd row (Rows (2)), it will be hit by saying that there is no row in the 5th row (Rows (4)).
I wondered if I could put "i-= 1" after deletion, but it didn't work. Along with the addition of new lines, we would appreciate any good advice.
Answer # 1
If you want to get the value of the previous last line quickly, is it like this?
(If ClassList is empty, take appropriate measures.)
Answer to additional questions
Dim drLast As DataRow = ClassList.Rows (ClassList.Rows.Count-1)) Dim className As String = drLast ("CLASS") Dim dr As DataRow = ClassList.NewRow dr ("CLASS") = className ClassList.Rows.Add (dr)
I think it's better to have a thread as another question, but here are some additional questions to answer.
For i = 0 It is not recommended to delete rows such as ClassData.Rows.RemoveAt (i) in To ... to Next.
If ClassData.Rows.RemoveAt (i) is performed in the loop, the index will be shifted by the amount of the removed DataRow.
This may cause the DataRow in Rows (i) to be a different DataRow than intended.
Also, if you define "For i As Integer = 0 To ClassData.Rows.Count-1" in the loop, even if the value of ClassData.Rows.Count changes during the loop, the upper limit of i will be I think it will be fixed.
I think this is the reason for the "no rows in Rows (4)" error.
There are various countermeasures, but if you want to use the current code,
Instead of "For i As Integer = 0 To ClassData.Rows.Count-1",
"For i As Integer = ClassData.Rows.Count-1 To 0 Step -1"
Is to evaluate the line from the opposite direction.
If this is the case, you don't have to worry about misalignment caused by missing DataRow.
- python - get the position where a specific string matches using a regular expression
- i want to acquire a specific value by adding a condition to the column value from php db
- [vbnet] i don't know how to edit and update the data displayed in datagridview
- vbnet - about the datagridview cellpainting event
- [vbnet] i want to perform validation check for each row in datagridview
- sql server - [vbnet] i don't know how to calculate time in datagridview and how to convert to time format (hhmm → hh:mm)
- i want to go back to a specific commit on github from the state where git was deleted
- vbnet - i want to display an image when the datagridview screen is opened
- vbnet i want to display by adding elements in an array
- Methods for adding and inserting specific characters in Python strings
- C # DataGridView method for dynamically adding rows and columns
- Jquery specific examples introduce when AJAX is used, where should AJAX be used
- jQuery implements a method of adding specific content to a string by a specified length
- The specific method of adding watermark to Excel in C #
- error message when adding datagridview row