-
Oct 21st, 2022, 03:40 AM
#1
Thread Starter
Hyperactive Member
DataGridView item editing is not following
Hi, Consider a DGV in another form as an event recorder. You want to increase its row index +1 everytime it gets a row of information. It contains 3 columns for instance. ("No.", "Message", "Date&Time")
In first form Button1 Adds an "A" information row, Button2 "B" and Button 3 shows the EventRecorder form.
Form1 Code:
Code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.DataGridView1.Rows.Add("", "A", Now)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Form2.DataGridView1.Rows.Add("", "B", Now)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Form2.Show()
End Sub
End Class
In Form2 RowsAdded event might be helpful for this case
Form2 Code:
Code:
Public Class Form2
Private Sub DataGridView1_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
DataGridView1.Item(0, DataGridView1.RowCount - 1).Value = DataGridView1.RowCount.ToString
End Sub
End Class
2 Questions:
1) Why when I show Form2, It opens and shows DGV with rows (either with correct or incorrect values) for first time, but when I open it for 2nd time and so on it is completely blank and empty?
2) Why added rows have empty rows index columns? (I'm suspected to the way I added "A" and "B" messages since I left first column value empty with "". But if RowsAdded event take place after I added message row, it should be updated with row index I don't know why it doesn't)
-
Oct 21st, 2022, 03:48 AM
#2
Re: DataGridView item editing is not following
Originally Posted by pourkascheff
1) Why when I show Form2, It opens and shows DGV with rows (either with correct or incorrect values) for first time, but when I open it for 2nd time and so on it is completely blank and empty?
By using the type name, you are referring to the default instance of Form2. You can read about default instances here. When you call Show to display a form, closing that form will dispose it. That means that, the next time you use the default instance, a new Form2 instance is created. That new instance doesn't know anything about the rows you added to the previous instance.
A potential solution is to add your rows to a list of some sort, which might be a DataTable or a generic List of a custom type or whatever. You can then pass that list to each Form2 instance and it can bind it to its own grid. Every instance will then be displaying the same data and your main form always holds that data. This could also make your second question moot, because you could add the row number when you add the row to the list.
Tags for this Thread
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
|