如何从另一个表单引用控件,例如,我从另一个具有 DataGrid 的表单打开一个模态表单,如何从模态表单向 RadGridView 添加行。
formA 打开 -> 按钮打开 formB 模式 -> formB 代码将 radgridview 行添加到 formA -> 关闭 formB -> formA 已添加行:)
从formB尝试:
formA.radgridview1.rows.add("data")
无济于事:(
将用作 RadGridView 的数据源的 DataTable 在 Form1 内声明为 Public,在 Form2 内将行添加到 DataTable 而不是 RadGridView。
Form1里面
Public DataTable1 As New DataTable
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
DataTable1.Columns.Add("data")
DataTable1.Rows.Add("Test row from Form A")
RadGridView1.DataSource = DataTable1
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim Form2 As New Form2
Form2.ShowDialog()
Form2.Dispose()
End Sub
内部 Form2
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Form1.DataTable1.Rows.Add("Test row from Form B")
End Sub