By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

Detecting Row Change in a Datagrid

P: n/a
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");
}

Nov 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

------------------

"Wayne Wengert" <wa***************@wengert.com> wrote in message
news:Ow**************@TK2MSFTNGP09.phx.gbl...
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");
}

Nov 20 '05 #2

P: n/a
There are lot's of C# to VB.NET converters out there. I used one at
http://authors.aspalliance.com/aldot...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:\no rthwind.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" <wa***************@wengert.com> wrote in message
news:Ow**************@TK2MSFTNGP09.phx.gbl...
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");
}

Nov 20 '05 #3

P: n/a
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("Emplo yees"))
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
///


Nov 20 '05 #4

P: n/a
Thanks guys;

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

Wayne

"Wayne Wengert" <wa***************@wengert.com> wrote in message
news:Ow**************@TK2MSFTNGP09.phx.gbl...
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");
}

Nov 20 '05 #5

P: n/a
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" <wa***************@wengert.com> wrote in message
news:Ow**************@TK2MSFTNGP09.phx.gbl...
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");
}

Nov 20 '05 #6

P: n/a
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" <wa***************@wengert.com> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...
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" <wa***************@wengert.com> wrote in message
news:Ow**************@TK2MSFTNGP09.phx.gbl...
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");
}


Nov 20 '05 #7

P: n/a
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

Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.