datagrid Add

  • Thread starter Thread starter Randy Fraser
  • Start date Start date
R

Randy Fraser

Hi All

I am trying to (with a button click) add a new row to a datagrid and then
set focus on the first column of the new row. I have waisted almost a day
on this now and still can't get it to work. Any help would be greatly
appreciated.

Here is some code

Private Sub btnAdd_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnAdd.Click

Dim nrecords As Integer = ds.Tables(0).Rows.Count

addRecord()

'Here I need to set focus on the new cell in the datagrid.

End Sub



Protected Overridable Sub addRecord()

ds.Tables(0).DefaultView.AllowNew = True

bNewRow = True

bmb.AddNew()

bmb.Position = bmb.Count - 1

ds.Tables(0).DefaultView.AllowNew = False

Me.SetButtons(1)

End Sub



Thanks

Randy
 
Hi Randy,

Normaly you have only to do this when that dataset is the datasource of your
datagrid and than it is the last row.

ds.Tables(0).rows.add(ds.tables(0).newrow)
DataGrid1.Select(ds.tables(0).Rows.Count - 1)
DataGrid1.CurrentRowIndex = ds.tables(0).Rows.Count - 1

However show us what is your datasource because when it is the dataview with
a sort in it, it is of course not this.

I hope this helps already a little bit.

Cor
 
I think the problem might be because I am using a Minding manager bound to a
comboBox and the datagrid. Maybe this is not the proper way of doing this.

It seems to work OK in every other aspect other than adding new records. I
tried your code and it worked better but only would select the current row.
I would like to go one step further and select the textbox in the first
column of the grid. The cboSearch Box will not clear even if I tell it that
its text = "" and/or its selectedIndex = -1. I think I am going to have to
loose that binding.

Here is my bind method:

Private Sub Bind()

Dim i As Integer

Dim lst As New DataTable

bmb = Me.BindingContext.Item(ds.Tables("ItemLocations"))

MyBase.DisplayPosition()

grdCatalog.ReadOnly = False

grdCatalog.TabStop = True

'Attach dataset's DefaultView to the datagrid control

'grdCatalog.DataSource = ds.Tables("ItemLocations").DefaultView

'no adding of new rows thru dataview...

lst = ds.Tables("ItemLocations")

grdCatalog.DataSource = ds.Tables("ItemLocations")

With cboSearch

..DataSource = ds.Tables("ItemLocations")

..DisplayMember = "LocationID"

..ValueMember = "LocationID"

..SelectedIndex = -1

..SelectedIndex = -1

End With

MyBase.bLoading = False

If ds.Tables(0).Rows.Count = 0 Then

MyBase.SetButtons(6) 'Empty dataset

Else

MyBase.SetButtons(0)

End If

bnewRow = False

End Sub
 
Back
Top