System.Windows.Forms.DataGrid.AddNewRow()

  • Thread starter Thread starter max
  • Start date Start date
M

max

I can generate this error by NOT assigning a tableName to the view:

at System.Windows.Forms.DataGrid.AddNewRow()
at System.Windows.Forms.DataGridAddNewRow.OnEdit()

Is there any possibility of getting an event from the AddNewRow()? I
would like to make some changes to the row prior to the user typing in
the DataGrid.

Thanks,
--max
 
Hi,

There is not an event but you can try something like this.

Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Data.SqlClient

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "


Dim da As SqlClient.SqlDataAdapter
Dim ds As New DataSet
Dim WithEvents cm As CurrencyManager

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim conn As SqlConnection
Dim strConn As String

strConn = "Server = (local);"
strConn &= "Database = NorthWind; Integrated Security = SSPI;"
conn = New SqlConnection(strConn)
da = New SqlDataAdapter("Select * from products", conn)
da.Fill(ds, "Products")

DataGrid1.DataSource = ds.Tables("Products")

cm = CType(Me.BindingContext(DataGrid1.DataSource), CurrencyManager)
Button1.Enabled = False
End Sub

Private Sub cm_CurrentChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cm.CurrentChanged
Button1.Enabled = True
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim cmd As New SqlCommandBuilder(da)

da.Update(ds.Tables("Products"))
ds.AcceptChanges()

Button1.Enabled = False

End Sub

Private Sub cm_PositionChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cm.PositionChanged
If cm.Position >= ds.Tables("Products").Rows.Count Then
' new row is added
Button1.Enabled = True
' you can try something like this
DataGrid1.Item(cm.Position, 1) = "Test"
End If
End Sub
End Class


Ken
 
Ken Tucker said:
Hi,

There is not an event but you can try something like this.

Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Data.SqlClient

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "


Dim da As SqlClient.SqlDataAdapter
Dim ds As New DataSet
Dim WithEvents cm As CurrencyManager

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim conn As SqlConnection
Dim strConn As String

strConn = "Server = (local);"
strConn &= "Database = NorthWind; Integrated Security = SSPI;"
conn = New SqlConnection(strConn)
da = New SqlDataAdapter("Select * from products", conn)
da.Fill(ds, "Products")

DataGrid1.DataSource = ds.Tables("Products")

cm = CType(Me.BindingContext(DataGrid1.DataSource),
CurrencyManager)
Button1.Enabled = False
End Sub

Private Sub cm_CurrentChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cm.CurrentChanged
Button1.Enabled = True
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim cmd As New SqlCommandBuilder(da)

da.Update(ds.Tables("Products"))
ds.AcceptChanges()

Button1.Enabled = False

End Sub

Private Sub cm_PositionChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cm.PositionChanged
If cm.Position >= ds.Tables("Products").Rows.Count Then
' new row is added
Button1.Enabled = True
' you can try something like this
DataGrid1.Item(cm.Position, 1) = "Test"
End If
End Sub
End Class


Ken
-------------------

Visual Basic?
Mat
 
Thanks Ken, I'll give it a try. I am very interested in finding out
how I can have access to the row before the user gets to enter data.
It seems to me the process should be easier. On the web.gui you can
get a OnChangedRow event.

Thanks again,
--max
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top