Datagrid and Rows.Add 
Author Message
 Datagrid and Rows.Add

When I use Rows.Add to add a row to a DataGrid the form is brought to the
front of other forms in the application.

To duplicate this effect create two empty forms Form1 and Form2.
Copy the following chunks of code into the appropriate form.
Form1 should be the startup form...

Run the program and bring Form2 to the front.  Then click the "Add Row"
button in Form2 and Form1 will come to the fore.

How do I keep the Rows.Add from diddling the Forms order?
In my real app rows are being added in response to data coming in from afar
and I need to add data to the grid without the form popping to the front.

Thanks, Mike McAmis

--- m.mcamis
--- at
--- valley.net

**** Insert into Form1 ****

Private DataGrid1 As System.Windows.Forms.DataGrid
Private oDataTable As DataTable
Private oColumn1 As DataGridTextBoxColumn
Private oColumn2 As DataGridTextBoxColumn

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
  '
  '  Create DataTable, add a couple of columns
  '
  oDataTable = New DataTable("GridData")
  oDataTable.Columns.Add(New DataColumn("Column1"))
  oDataTable.Columns.Add(New DataColumn("Column2"))
  '
  '  Create DataGrid, add to form
  '
  DataGrid1 = New System.Windows.Forms.DataGrid()
  DataGrid1.Name = "DataGrid1"
  DataGrid1.Dock = DockStyle.Fill
  Controls.AddRange(New System.Windows.Forms.Control() {DataGrid1})
  '
  '  Add DataTable to grid
  '
  DataGrid1.DataSource = oDataTable
  '
  '  Start with a few rows
  '
  Call GridAddRow()
  Call GridAddRow()
  Call GridAddRow()
  '
  '  Create/show button form
  '
  Dim oForm2 As Form2 = New Form2()
  oForm2.Setup(Me)
  oForm2.Show()

End Sub

Public Sub GridAddRow()
  Dim oGridRow As DataRow

  oGridRow = oDataTable.NewRow
  oGridRow(0) = "xxx " & oDataTable.Rows.Count
  oGridRow(1) = "yyy " & oDataTable.Rows.Count
  oDataTable.Rows.Add(oGridRow)

End Sub

**** Insert into Form2 ****

Private oGridForm As Form1
Private WithEvents Button1 As System.Windows.Forms.Button

Public Sub Setup(ByVal oForm1 As Form1)
  '
  '  Save pointer back to grid form
  '
  oGridForm = oForm1
  Me.Left = oGridForm.Left + 100
  '
  '  Add an "Add Row" button to form
  '
  Button1 = New System.Windows.Forms.Button()
  Button1.Name = "Button1"
  Button1.Text = "Add Row"
  Button1.Left = (Me.ClientSize.Width - Button1.Width) / 2
  Button1.Top = (Me.ClientSize.Height - Button1.Height) / 2
  Controls.AddRange(New System.Windows.Forms.Control() {Button1})

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
  '
  '  Add a row...
  '
  If Not oGridForm Is Nothing Then
    oGridForm.GridAddRow()
  End If

End Sub



Wed, 16 Nov 2005 04:45:24 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Add row to datatable from datagrid without leaving current row with cursor

2. datagrid - catching whether editing or adding a new row

3. DataGrid Rows.Add brings form to fore

4. how to prevent adding new rows to a datagrid

5. Trouble with adding row to datagrid

6. Add Row to Bound Datagrid

7. Disabling Add Rows in a DataGrid in VB.net

8. asp.net DataGrid adding an extra Header Row

9. Adding a row to a DataGrid?

10. Add Row to DataGrid at RunTime?

11. Adding rows to DataGrid?

12. Allowing the user to add a DataGrid row?

 

 
Powered by phpBB® Forum Software