Detecting Row Change in a Datagrid

W

Wayne Wengert

I am using VB with a Windows NET application. I have a datagrid in which the
user can add rows using the "*" row. I want to detect whenever the user has
added a row. I found the following code at syncfusion.com but it is in C#
and I am having a problem converting it to VB. If anyone can show the VB
equivalent and/or point me to a better way to detect a row change I'd
appreciate it.


Wayne

===================== Code ===============================
private System.Windows.Forms.DataGrid dataGrid1;
private BindingManagerBase bindingManager;

private void Form1_Load(object sender, System.EventArgs e)

{
// Creating connection and command sting
string conStr = @"Provider=Microsoft.JET.OLEDB.4.0;data
source=C:\northwind.mdb";
string sqlStr = "SELECT * FROM Employees";

// Create connection object
OleDbConnection conn = new OleDbConnection(conStr);

// Create data adapter object
OleDbDataAdapter da = new OleDbDataAdapter(sqlStr,conn);

// Create a dataset object and fill with data using data adapter's Fill
method
DataSet ds = new DataSet();
da.Fill(ds, "Employees");
dataGrid1.DataSource = ds.Tables["Employees"];
bindingManager = this.BindingContext[dataGrid1.DataSource];
bindingManager.PositionChanged += new System.EventHandler(RowChanged);
}
private void RowChanged(object sender, System.EventArgs e)
{
Console.WriteLine("RowChanged " + bindingManager.Position.ToString() );
bool lastRow = bindingManager.Count >
((DataTable)dataGrid1.DataSource).Rows.Count;
if(lastRow)
Console.WriteLine("lastRow");
}
 
K

Ken Tucker [MVP]

Hi,

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 strConn As String

Dim strSQL As String

Dim da As OleDbDataAdapter

Dim conn As OleDbConnection

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn &= "Data Source = Northwind.mdb;"

conn = New OleDbConnection(strConn)

da = New OleDbDataAdapter("Select * From Categories", conn)

da.Fill(ds, "Categories")

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

cm = CType(Me.BindingContext(ds.Tables("Categories")), CurrencyManager)

AddHandler ds.Tables("Categories").DefaultView.ListChanged, AddressOf
OnListChanged

End Sub



Private Sub cm_PositionChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cm.PositionChanged

Debug.WriteLine(cm.Position)

If cm.Position >= ds.Tables(0).Rows.Count Then MessageBox.Show("New Row")

End Sub





Ken
 
R

Rob Windsor [MVP]

There are lot's of C# to VB.NET converters out there. I used one at
http://authors.aspalliance.com/aldotnet/examples/translate.aspx to convert
your code snippet, the result is below.

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada

===

Private dataGrid1 As System.Windows.Forms.DataGrid
Private bindingManager As BindingManagerBase

Private Sub Form1_Load(sender As Object, e As System.EventArgs)
' Creating connection and command sting
Dim conStr As String =
"Provider=Microsoft.JET.OLEDB.4.0;datasource=C:\northwind.mdb"
Dim sqlStr As String = "SELECT * FROM Employees"

' Create connection object
Dim conn As New OleDbConnection(conStr)

' Create data adapter object
Dim da As New OleDbDataAdapter(sqlStr, conn)

' Create a dataset object and fill with data using data adapter's Fill
method
Dim ds As New DataSet()

da.Fill(ds, "Employees")
dataGrid1.DataSource = ds.Tables("Employees")
bindingManager = Me.BindingContext(dataGrid1.DataSource)
AddHandler bindingManager.PositionChanged, AddressOf RowChanged
End Sub

Private Sub RowChanged(sender As Object, e As System.EventArgs)
Console.WriteLine(("RowChanged " + bindingManager.Position.ToString()))
Dim lastRow As Boolean = bindingManager.Count >
CType(dataGrid1.DataSource, DataTable).Rows.Count

If lastRow Then
Console.WriteLine("lastRow")
End If
End Sub

===




Wayne Wengert said:
I am using VB with a Windows NET application. I have a datagrid in which the
user can add rows using the "*" row. I want to detect whenever the user has
added a row. I found the following code at syncfusion.com but it is in C#
and I am having a problem converting it to VB. If anyone can show the VB
equivalent and/or point me to a better way to detect a row change I'd
appreciate it.


Wayne

===================== Code ===============================
private System.Windows.Forms.DataGrid dataGrid1;
private BindingManagerBase bindingManager;

private void Form1_Load(object sender, System.EventArgs e)

{
// Creating connection and command sting
string conStr = @"Provider=Microsoft.JET.OLEDB.4.0;data
source=C:\northwind.mdb";
string sqlStr = "SELECT * FROM Employees";

// Create connection object
OleDbConnection conn = new OleDbConnection(conStr);

// Create data adapter object
OleDbDataAdapter da = new OleDbDataAdapter(sqlStr,conn);

// Create a dataset object and fill with data using data adapter's Fill
method
DataSet ds = new DataSet();
da.Fill(ds, "Employees");
dataGrid1.DataSource = ds.Tables["Employees"];
bindingManager = this.BindingContext[dataGrid1.DataSource];
bindingManager.PositionChanged += new System.EventHandler(RowChanged);
}
private void RowChanged(object sender, System.EventArgs e)
{
Console.WriteLine("RowChanged " +
bindingManager.Position.ToString() );
 
C

Cor Ligthert

Hi Wayne,

In addition to Ken, where Ken gives you ask you asked a translation from C#
to VB.net has VB.net something more than C#.

I show you it bellow in a way you more recognise as VB.net (while there is
nothing wrong with the method from Ken), just to show you the alternative.

I hope this helps you to give some alternative ideas?

Cor

\\\
Private WithEvents dt As DataTable
Private Sub Form5_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.SqlDataAdapter1.Fill(Me.DataSet11.Tables("Employees"))
dt = Me.DataSet11.Tables("Employees")
Me.DataGrid1.DataSource = Me.DataSet11.Tables("Employees")
End Sub
Private Sub dt_RowChanging(ByVal sender As Object, _
ByVal e As System.Data.DataRowChangeEventArgs) Handles dt.RowChanging
Dim cm As CurrencyManager = DirectCast _
(Me.BindingContext(Me.DataSet11.Tables("Employees")), _
CurrencyManager)
Debug.WriteLine(cm.Position)
If cm.Position >= DataSet11.Tables("Employees").Rows.Count Then
MessageBox.Show("New Row")
End If
End Sub
///
 
W

Wayne Wengert

Thanks guys;

I'll try to apply your suggestions and let you know how things work.

Wayne

Wayne Wengert said:
I am using VB with a Windows NET application. I have a datagrid in which the
user can add rows using the "*" row. I want to detect whenever the user has
added a row. I found the following code at syncfusion.com but it is in C#
and I am having a problem converting it to VB. If anyone can show the VB
equivalent and/or point me to a better way to detect a row change I'd
appreciate it.


Wayne

===================== Code ===============================
private System.Windows.Forms.DataGrid dataGrid1;
private BindingManagerBase bindingManager;

private void Form1_Load(object sender, System.EventArgs e)

{
// Creating connection and command sting
string conStr = @"Provider=Microsoft.JET.OLEDB.4.0;data
source=C:\northwind.mdb";
string sqlStr = "SELECT * FROM Employees";

// Create connection object
OleDbConnection conn = new OleDbConnection(conStr);

// Create data adapter object
OleDbDataAdapter da = new OleDbDataAdapter(sqlStr,conn);

// Create a dataset object and fill with data using data adapter's Fill
method
DataSet ds = new DataSet();
da.Fill(ds, "Employees");
dataGrid1.DataSource = ds.Tables["Employees"];
bindingManager = this.BindingContext[dataGrid1.DataSource];
bindingManager.PositionChanged += new System.EventHandler(RowChanged);
}
private void RowChanged(object sender, System.EventArgs e)
{
Console.WriteLine("RowChanged " +
bindingManager.Position.ToString() );
 
W

Wayne Wengert

Just as a follow-up - It looks like I can't use this approach since I am not
using a dataset as the source. I create an unbound datagrid and then build a
table in code and set that as the datasource for the datagrid.

I do appreciate all the help.

Wayne

Wayne Wengert said:
I am using VB with a Windows NET application. I have a datagrid in which the
user can add rows using the "*" row. I want to detect whenever the user has
added a row. I found the following code at syncfusion.com but it is in C#
and I am having a problem converting it to VB. If anyone can show the VB
equivalent and/or point me to a better way to detect a row change I'd
appreciate it.


Wayne

===================== Code ===============================
private System.Windows.Forms.DataGrid dataGrid1;
private BindingManagerBase bindingManager;

private void Form1_Load(object sender, System.EventArgs e)

{
// Creating connection and command sting
string conStr = @"Provider=Microsoft.JET.OLEDB.4.0;data
source=C:\northwind.mdb";
string sqlStr = "SELECT * FROM Employees";

// Create connection object
OleDbConnection conn = new OleDbConnection(conStr);

// Create data adapter object
OleDbDataAdapter da = new OleDbDataAdapter(sqlStr,conn);

// Create a dataset object and fill with data using data adapter's Fill
method
DataSet ds = new DataSet();
da.Fill(ds, "Employees");
dataGrid1.DataSource = ds.Tables["Employees"];
bindingManager = this.BindingContext[dataGrid1.DataSource];
bindingManager.PositionChanged += new System.EventHandler(RowChanged);
}
private void RowChanged(object sender, System.EventArgs e)
{
Console.WriteLine("RowChanged " +
bindingManager.Position.ToString() );
 
K

Ken Tucker [MVP]

Wayne,

Sure you can. Just replace ds.Tables(0) with your datatables name.


If cm.Position >= YourDataTable.Rows.Count Then MessageBox.Show("New Row")


Ken
-------------
Wayne Wengert said:
Just as a follow-up - It looks like I can't use this approach since I am
not
using a dataset as the source. I create an unbound datagrid and then build
a
table in code and set that as the datasource for the datagrid.

I do appreciate all the help.

Wayne

Wayne Wengert said:
I am using VB with a Windows NET application. I have a datagrid in which the
user can add rows using the "*" row. I want to detect whenever the user has
added a row. I found the following code at syncfusion.com but it is in C#
and I am having a problem converting it to VB. If anyone can show the VB
equivalent and/or point me to a better way to detect a row change I'd
appreciate it.


Wayne

===================== Code ===============================
private System.Windows.Forms.DataGrid dataGrid1;
private BindingManagerBase bindingManager;

private void Form1_Load(object sender, System.EventArgs e)

{
// Creating connection and command sting
string conStr = @"Provider=Microsoft.JET.OLEDB.4.0;data
source=C:\northwind.mdb";
string sqlStr = "SELECT * FROM Employees";

// Create connection object
OleDbConnection conn = new OleDbConnection(conStr);

// Create data adapter object
OleDbDataAdapter da = new OleDbDataAdapter(sqlStr,conn);

// Create a dataset object and fill with data using data adapter's Fill
method
DataSet ds = new DataSet();
da.Fill(ds, "Employees");
dataGrid1.DataSource = ds.Tables["Employees"];
bindingManager = this.BindingContext[dataGrid1.DataSource];
bindingManager.PositionChanged += new System.EventHandler(RowChanged);
}
private void RowChanged(object sender, System.EventArgs e)
{
Console.WriteLine("RowChanged " +
bindingManager.Position.ToString() );
bool lastRow = bindingManager.Count >
((DataTable)dataGrid1.DataSource).Rows.Count;
if(lastRow)
Console.WriteLine("lastRow");
}
 
C

Cor Ligthert

Hi Wayne,

I have sended this message yesterday however I do not see it, so maybe it is
twice now

It was for me more work to use that fill than a table, and I see I made
errors because I was to focused on that Employee file and not what should be
the result. This I made without to much looking to those. (Although I have
used the sample from Ken as template).

That it fires twice is something you may correct yourself.

I hope this goes better?

Cor

Private WithEvents dv As DataView
Private dt As New DataTable
Private Sub Form7_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dt.Columns.Add("Wayne")
dt.Columns.Add("Ken")
For i As Integer = 0 To 4
Dim dr As DataRow = dt.NewRow
dr(0) = (i + 1).ToString
dr(1) = Chr(i + 65)
dt.Rows.Add(dr)
Next
dv = New DataView(dt)
Me.DataGrid1.DataSource = dv
End Sub

Private Sub dv_ListChanged(ByVal sender As Object, _
ByVal e As System.ComponentModel.ListChangedEventArgs) Handles
dv.ListChanged
If dt.Rows.Count < dv.Count Then
MessageBox.Show("New Row " & dv.Count.ToString)
End If
End Sub
End Class
 

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

Top