Datagrid: Display latest populated row

  • Thread starter Thread starter Homa
  • Start date Start date
H

Homa

Hi all,

I'm using a datagrid to display some log entries. The entry populates
downward, and I want to show the latest one as it comes in.

i.e., I want the datagrid to scroll down automatically as a new row is
added to the dataTable.

however, I can't find the right event/method to handle this.

Thanks for concern,
Homa Wong
 
After adding your row, you can set the currencymanager position to that row
and the datagrid will scroll to it.

this.BindingContext[this.dataGrid1.DataSource,
this.dataGrid1.DataMember].Position = myDataTable.Rows.Count;

Or, another way you can do this is to derive the DataGrid and expose a
public method that accesses the protected GridVScrolled method to create a
method that scrolls to a particular row. Here is a FAQ link on this.

How do I programmatically scroll the datagrid to a particular row?
http://www.syncfusion.com/faq/winforms/search/895.asp

=============================================
Clay Burch, .NET MVP
Syncfusion, Inc.
visit http://www.syncfusion.com for .NET Essentials
 
Thank you for your help. It is very useful.
I used the second approach to inherit the original DataGrid.
I added some more functionality so that it handles my requirement
better.
1. I added public void ScrollToBar(int rowNumber)
This basically does what the link
http://www.syncfusion.com/faq/winforms/search/895.asp does.

2. I added a property IsStickyBottom to change the behavior of the
DataGrid.
If this is set to true,
If the scrollbar is scrolled to the bottom (or when the
scrollbar is not yet visible)
When the DataSource's content changes (add/update/remove
row(s)), the scrollbar will stay at the bottom and the last row will
always visible.
EndIf
EndIf

Happy Coding,
Homa Wong

Here is my code, hope other people can benefit from it.

/*///----------------------------------------------------------------
created: 2004/08/17
created: 17:8:2004 13:08
filename: D:\Projects\Homa\cSharp
Library\Homa.Windows.Forms\DataGrid.cs
file path: D:\Projects\Homa\cSharp Library\Homa.Windows.Forms
file base: DataGrid
file ext: cs
author: Homa Wong

purpose: Enhance the System.Windows.Forms.DataGrid

new: public void ScrollToRow(int rowNumber)
StickyBottom
/*///----------------------------------------------------------------
/*///----------------------------------------------------------------
editDate: 2004/08/17
editor: Homa Wong

detail: New New class
Complete public void ScrollToRow(int rowNumber)
IsStickyBottom
/*///----------------------------------------------------------------
using System;
using WinForms = System.Windows.Forms;
namespace Homa.Windows.Forms
{
#region Comments
/// <summary>
/// This is an enhanced version of System.Windows.Forms.DataGrid with
row scrolling.
/// </summary>
/// <remarks>
/// <p>Two functionality added to this class:
/// public void ScrollToRow(int rowNumber) : scroll to a specific
row.</p>
///<p>public bool IsStickyBottom : if the DataSource changes (not the
reference changed,
/// but the content of DataSource changes), and the DataGrid is
displaying the last row
/// (the scroll bar's value == DataSource's count - VisibleRowCount),
then the grid will stick
/// at the bottom as the DataSource grows.</p>
/// </remarks>
#endregion
public class DataGrid : WinForms.DataGrid
{
#region Comments
/// <summary>
/// This is the default constructor.
/// </summary>
#endregion
public DataGrid() : base()
{
Invalidated +=new
System.Windows.Forms.InvalidateEventHandler(DataGrid_Invalidated);
}

private bool _isStickyBottom = false;
#region Comments
/// <summary>
/// Determine if the DataGrid will stick at the bottom when the
DataSource grows.
/// </summary>
/// <remarks>
/// If true, and if the bottom row is currently visible, then the
DataGrid will stick at the bottom when the DataSource grows.
/// If the bottom row is not visible, this has no effect.
///
/// If false, DataGrid behaves normally.
/// </remarks>
#endregion
public bool IsStickyBottom
{
get{return _isStickyBottom;}
set{_isStickyBottom = value;}
}

int _dataRowCount = 0;

#region Comments
/// <summary>
/// This function scroll the DataGrid to display a particular row.
/// </summary>
/// <remarks>If the rowNumber is greater then number of rows in
DataSource, it will scroll to the last row.</remarks>
/// <param name="rowNumber">Row index to scroll to.</param>
#endregion
public void ScrollToRow(int rowNumber)
{
WinForms.ScrollBar sbar = VertScrollBar;
WinForms.ScrollEventType sEventType;
if (rowNumber <= 0)
{
sEventType = WinForms.ScrollEventType.First;
}
else if (rowNumber >= _dataRowCount)
{
sEventType = WinForms.ScrollEventType.Last;
}
else
{
int difference = sbar.Value - rowNumber; // < 0 -- go down ; > 0
-- go up

// It is not confirmed that what will the ScrollEventType do to
the scrolling in this case.
// But I find the right ScrollEventType anyway because it will
break encapsulation otherwise.
if (difference > 0)
{
if (difference < sbar.SmallChange)
sEventType = WinForms.ScrollEventType.SmallDecrement;
else
sEventType = WinForms.ScrollEventType.LargeDecrement;
}
else
{
difference = - difference;
if (difference < sbar.LargeChange)
sEventType = WinForms.ScrollEventType.SmallIncrement;
else
sEventType = WinForms.ScrollEventType.LargeIncrement;
}
}
GridVScrolled(this, new WinForms.ScrollEventArgs(sEventType,
rowNumber));
}

private void DataGrid_Invalidated(object sender,
System.Windows.Forms.InvalidateEventArgs e)
{
#region Update _dataRowCount
System.Collections.IList dv = DataSource as
System.Collections.IList;
if (dv != null)
{
_dataRowCount = dv.Count;
}

System.ComponentModel.IListSource ds = DataSource as
System.ComponentModel.IListSource;
if (ds != null)
{
_dataRowCount = ds.GetList().Count;
}
#endregion

if (_isStickyBottom && VertScrollBar.Value == _dataRowCount -
VisibleRowCount)
{
ScrollToRow(_dataRowCount);
}

}
}
}


ClayB said:
After adding your row, you can set the currencymanager position to that row
and the datagrid will scroll to it.

this.BindingContext[this.dataGrid1.DataSource,
this.dataGrid1.DataMember].Position = myDataTable.Rows.Count;

Or, another way you can do this is to derive the DataGrid and expose a
public method that accesses the protected GridVScrolled method to create a
method that scrolls to a particular row. Here is a FAQ link on this.

How do I programmatically scroll the datagrid to a particular row?
http://www.syncfusion.com/faq/winforms/search/895.asp

=============================================
Clay Burch, .NET MVP
Syncfusion, Inc.
visit http://www.syncfusion.com for .NET Essentials



Homa said:
Hi all,

I'm using a datagrid to display some log entries. The entry populates
downward, and I want to show the latest one as it comes in.

i.e., I want the datagrid to scroll down automatically as a new row is
added to the dataTable.

however, I can't find the right event/method to handle this.

Thanks for concern,
Homa Wong
 
Thank you for your help. It is very useful.
I used the second approach to inherit the original DataGrid.
I added some more functionality so that it handles my requirement
better.
1. I added public void ScrollToBar(int rowNumber)
This basically does what the link
http://www.syncfusion.com/faq/winforms/search/895.asp does.

2. I added a property IsStickyBottom to change the behavior of the
DataGrid.
If this is set to true,
If the scrollbar is scrolled to the bottom (or when the
scrollbar is not yet visible)
When the DataSource's content changes (add/update/remove
row(s)), the scrollbar will stay at the bottom and the last row will
always visible.
EndIf
EndIf

Happy Coding,
Homa Wong

Here is my code, hope other people can benefit from it.

/*///----------------------------------------------------------------
created: 2004/08/17
created: 17:8:2004 13:08
filename: D:\Projects\Homa\cSharp
Library\Homa.Windows.Forms\DataGrid.cs
file path: D:\Projects\Homa\cSharp Library\Homa.Windows.Forms
file base: DataGrid
file ext: cs
author: Homa Wong

purpose: Enhance the System.Windows.Forms.DataGrid

new: public void ScrollToRow(int rowNumber)
StickyBottom
/*///----------------------------------------------------------------
/*///----------------------------------------------------------------
editDate: 2004/08/17
editor: Homa Wong

detail: New New class
Complete public void ScrollToRow(int rowNumber)
IsStickyBottom
/*///----------------------------------------------------------------
using System;
using WinForms = System.Windows.Forms;
namespace Homa.Windows.Forms
{
#region Comments
/// <summary>
/// This is an enhanced version of System.Windows.Forms.DataGrid with
row scrolling.
/// </summary>
/// <remarks>
/// <p>Two functionality added to this class:
/// public void ScrollToRow(int rowNumber) : scroll to a specific
row.</p>
///<p>public bool IsStickyBottom : if the DataSource changes (not the
reference changed,
/// but the content of DataSource changes), and the DataGrid is
displaying the last row
/// (the scroll bar's value == DataSource's count - VisibleRowCount),
then the grid will stick
/// at the bottom as the DataSource grows.</p>
/// </remarks>
#endregion
public class DataGrid : WinForms.DataGrid
{
#region Comments
/// <summary>
/// This is the default constructor.
/// </summary>
#endregion
public DataGrid() : base()
{
Invalidated +=new
System.Windows.Forms.InvalidateEventHandler(DataGrid_Invalidated);
}

private bool _isStickyBottom = false;
#region Comments
/// <summary>
/// Determine if the DataGrid will stick at the bottom when the
DataSource grows.
/// </summary>
/// <remarks>
/// If true, and if the bottom row is currently visible, then the
DataGrid will stick at the bottom when the DataSource grows.
/// If the bottom row is not visible, this has no effect.
///
/// If false, DataGrid behaves normally.
/// </remarks>
#endregion
public bool IsStickyBottom
{
get{return _isStickyBottom;}
set{_isStickyBottom = value;}
}

int _dataRowCount = 0;

#region Comments
/// <summary>
/// This function scroll the DataGrid to display a particular row.
/// </summary>
/// <remarks>If the rowNumber is greater then number of rows in
DataSource, it will scroll to the last row.</remarks>
/// <param name="rowNumber">Row index to scroll to.</param>
#endregion
public void ScrollToRow(int rowNumber)
{
WinForms.ScrollBar sbar = VertScrollBar;
WinForms.ScrollEventType sEventType;
if (rowNumber <= 0)
{
sEventType = WinForms.ScrollEventType.First;
}
else if (rowNumber >= _dataRowCount)
{
sEventType = WinForms.ScrollEventType.Last;
}
else
{
int difference = sbar.Value - rowNumber; // < 0 -- go down ; > 0
-- go up

// It is not confirmed that what will the ScrollEventType do to
the scrolling in this case.
// But I find the right ScrollEventType anyway because it will
break encapsulation otherwise.
if (difference > 0)
{
if (difference < sbar.SmallChange)
sEventType = WinForms.ScrollEventType.SmallDecrement;
else
sEventType = WinForms.ScrollEventType.LargeDecrement;
}
else
{
difference = - difference;
if (difference < sbar.LargeChange)
sEventType = WinForms.ScrollEventType.SmallIncrement;
else
sEventType = WinForms.ScrollEventType.LargeIncrement;
}
}
GridVScrolled(this, new WinForms.ScrollEventArgs(sEventType,
rowNumber));
}

private void DataGrid_Invalidated(object sender,
System.Windows.Forms.InvalidateEventArgs e)
{
#region Update _dataRowCount
System.Collections.IList dv = DataSource as
System.Collections.IList;
if (dv != null)
{
_dataRowCount = dv.Count;
}

System.ComponentModel.IListSource ds = DataSource as
System.ComponentModel.IListSource;
if (ds != null)
{
_dataRowCount = ds.GetList().Count;
}
#endregion

if (_isStickyBottom && VertScrollBar.Value == _dataRowCount -
VisibleRowCount)
{
ScrollToRow(_dataRowCount);
}

}
}
}


ClayB said:
After adding your row, you can set the currencymanager position to that row
and the datagrid will scroll to it.

this.BindingContext[this.dataGrid1.DataSource,
this.dataGrid1.DataMember].Position = myDataTable.Rows.Count;

Or, another way you can do this is to derive the DataGrid and expose a
public method that accesses the protected GridVScrolled method to create a
method that scrolls to a particular row. Here is a FAQ link on this.

How do I programmatically scroll the datagrid to a particular row?
http://www.syncfusion.com/faq/winforms/search/895.asp

=============================================
Clay Burch, .NET MVP
Syncfusion, Inc.
visit http://www.syncfusion.com for .NET Essentials



Homa said:
Hi all,

I'm using a datagrid to display some log entries. The entry populates
downward, and I want to show the latest one as it comes in.

i.e., I want the datagrid to scroll down automatically as a new row is
added to the dataTable.

however, I can't find the right event/method to handle this.

Thanks for concern,
Homa Wong
 
Thank you for your help. It is very useful.
I used the second approach to inherit the original DataGrid.
I added some more functionality so that it handles my requirement
better.
1. I added public void ScrollToBar(int rowNumber)
This basically does what the link
http://www.syncfusion.com/faq/winforms/search/895.asp does.

2. I added a property IsStickyBottom to change the behavior of the
DataGrid.
If this is set to true,
If the scrollbar is scrolled to the bottom (or when the
scrollbar is not yet visible)
When the DataSource's content changes (add/update/remove
row(s)), the scrollbar will stay at the bottom and the last row will
always visible.
EndIf
EndIf

Happy Coding,
Homa Wong

Here is my code, hope other people can benefit from it.

/*///----------------------------------------------------------------
created: 2004/08/17
created: 17:8:2004 13:08
filename: D:\Projects\Homa\cSharp
Library\Homa.Windows.Forms\DataGrid.cs
file path: D:\Projects\Homa\cSharp Library\Homa.Windows.Forms
file base: DataGrid
file ext: cs
author: Homa Wong

purpose: Enhance the System.Windows.Forms.DataGrid

new: public void ScrollToRow(int rowNumber)
StickyBottom
/*///----------------------------------------------------------------
/*///----------------------------------------------------------------
editDate: 2004/08/17
editor: Homa Wong

detail: New New class
Complete public void ScrollToRow(int rowNumber)
IsStickyBottom
/*///----------------------------------------------------------------
using System;
using WinForms = System.Windows.Forms;
namespace Homa.Windows.Forms
{
#region Comments
/// <summary>
/// This is an enhanced version of System.Windows.Forms.DataGrid with
row scrolling.
/// </summary>
/// <remarks>
/// <p>Two functionality added to this class:
/// public void ScrollToRow(int rowNumber) : scroll to a specific
row.</p>
///<p>public bool IsStickyBottom : if the DataSource changes (not the
reference changed,
/// but the content of DataSource changes), and the DataGrid is
displaying the last row
/// (the scroll bar's value == DataSource's count - VisibleRowCount),
then the grid will stick
/// at the bottom as the DataSource grows.</p>
/// </remarks>
#endregion
public class DataGrid : WinForms.DataGrid
{
#region Comments
/// <summary>
/// This is the default constructor.
/// </summary>
#endregion
public DataGrid() : base()
{
Invalidated +=new
System.Windows.Forms.InvalidateEventHandler(DataGrid_Invalidated);
}

private bool _isStickyBottom = false;
#region Comments
/// <summary>
/// Determine if the DataGrid will stick at the bottom when the
DataSource grows.
/// </summary>
/// <remarks>
/// If true, and if the bottom row is currently visible, then the
DataGrid will stick at the bottom when the DataSource grows.
/// If the bottom row is not visible, this has no effect.
///
/// If false, DataGrid behaves normally.
/// </remarks>
#endregion
public bool IsStickyBottom
{
get{return _isStickyBottom;}
set{_isStickyBottom = value;}
}

int _dataRowCount = 0;

#region Comments
/// <summary>
/// This function scroll the DataGrid to display a particular row.
/// </summary>
/// <remarks>If the rowNumber is greater then number of rows in
DataSource, it will scroll to the last row.</remarks>
/// <param name="rowNumber">Row index to scroll to.</param>
#endregion
public void ScrollToRow(int rowNumber)
{
WinForms.ScrollBar sbar = VertScrollBar;
WinForms.ScrollEventType sEventType;
if (rowNumber <= 0)
{
sEventType = WinForms.ScrollEventType.First;
}
else if (rowNumber >= _dataRowCount)
{
sEventType = WinForms.ScrollEventType.Last;
}
else
{
int difference = sbar.Value - rowNumber; // < 0 -- go down ; > 0
-- go up

// It is not confirmed that what will the ScrollEventType do to
the scrolling in this case.
// But I find the right ScrollEventType anyway because it will
break encapsulation otherwise.
if (difference > 0)
{
if (difference < sbar.SmallChange)
sEventType = WinForms.ScrollEventType.SmallDecrement;
else
sEventType = WinForms.ScrollEventType.LargeDecrement;
}
else
{
difference = - difference;
if (difference < sbar.LargeChange)
sEventType = WinForms.ScrollEventType.SmallIncrement;
else
sEventType = WinForms.ScrollEventType.LargeIncrement;
}
}
GridVScrolled(this, new WinForms.ScrollEventArgs(sEventType,
rowNumber));
}

private void DataGrid_Invalidated(object sender,
System.Windows.Forms.InvalidateEventArgs e)
{
#region Update _dataRowCount
System.Collections.IList dv = DataSource as
System.Collections.IList;
if (dv != null)
{
_dataRowCount = dv.Count;
}

System.ComponentModel.IListSource ds = DataSource as
System.ComponentModel.IListSource;
if (ds != null)
{
_dataRowCount = ds.GetList().Count;
}
#endregion

if (_isStickyBottom && VertScrollBar.Value == _dataRowCount -
VisibleRowCount)
{
ScrollToRow(_dataRowCount);
}

}
}
}

ClayB said:
After adding your row, you can set the currencymanager position to that row
and the datagrid will scroll to it.

this.BindingContext[this.dataGrid1.DataSource,
this.dataGrid1.DataMember].Position = myDataTable.Rows.Count;

Or, another way you can do this is to derive the DataGrid and expose a
public method that accesses the protected GridVScrolled method to create a
method that scrolls to a particular row. Here is a FAQ link on this.

How do I programmatically scroll the datagrid to a particular row?
http://www.syncfusion.com/faq/winforms/search/895.asp

=============================================
Clay Burch, .NET MVP
Syncfusion, Inc.
visit http://www.syncfusion.com for .NET Essentials



Homa said:
Hi all,

I'm using a datagrid to display some log entries. The entry populates
downward, and I want to show the latest one as it comes in.

i.e., I want the datagrid to scroll down automatically as a new row is
added to the dataTable.

however, I can't find the right event/method to handle this.

Thanks for concern,
Homa Wong
 
Thank you for your help. It is very useful.
I used the second approach to inherit the original DataGrid.
I added some more functionality so that it handles my requirement
better.
1. I added public void ScrollToBar(int rowNumber)
This basically does what the link
http://www.syncfusion.com/faq/winforms/search/895.asp does.

2. I added a property IsStickyBottom to change the behavior of the
DataGrid.
If this is set to true,
If the scrollbar is scrolled to the bottom (or when the
scrollbar is not yet visible)
When the DataSource's content changes (add/update/remove
row(s)), the scrollbar will stay at the bottom and the last row will
always visible.
EndIf
EndIf

Happy Coding,
Homa Wong

Here is my code, hope other people can benefit from it.

/*///----------------------------------------------------------------
created: 2004/08/17
created: 17:8:2004 13:08
filename: D:\Projects\Homa\cSharp
Library\Homa.Windows.Forms\DataGrid.cs
file path: D:\Projects\Homa\cSharp Library\Homa.Windows.Forms
file base: DataGrid
file ext: cs
author: Homa Wong

purpose: Enhance the System.Windows.Forms.DataGrid

new: public void ScrollToRow(int rowNumber)
StickyBottom
/*///----------------------------------------------------------------
/*///----------------------------------------------------------------
editDate: 2004/08/17
editor: Homa Wong

detail: New New class
Complete public void ScrollToRow(int rowNumber)
IsStickyBottom
/*///----------------------------------------------------------------
using System;
using WinForms = System.Windows.Forms;
namespace Homa.Windows.Forms
{
#region Comments
/// <summary>
/// This is an enhanced version of System.Windows.Forms.DataGrid with
row scrolling.
/// </summary>
/// <remarks>
/// <p>Two functionality added to this class:
/// public void ScrollToRow(int rowNumber) : scroll to a specific
row.</p>
///<p>public bool IsStickyBottom : if the DataSource changes (not the
reference changed,
/// but the content of DataSource changes), and the DataGrid is
displaying the last row
/// (the scroll bar's value == DataSource's count - VisibleRowCount),
then the grid will stick
/// at the bottom as the DataSource grows.</p>
/// </remarks>
#endregion
public class DataGrid : WinForms.DataGrid
{
#region Comments
/// <summary>
/// This is the default constructor.
/// </summary>
#endregion
public DataGrid() : base()
{
Invalidated +=new
System.Windows.Forms.InvalidateEventHandler(DataGrid_Invalidated);
}

private bool _isStickyBottom = false;
#region Comments
/// <summary>
/// Determine if the DataGrid will stick at the bottom when the
DataSource grows.
/// </summary>
/// <remarks>
/// If true, and if the bottom row is currently visible, then the
DataGrid will stick at the bottom when the DataSource grows.
/// If the bottom row is not visible, this has no effect.
///
/// If false, DataGrid behaves normally.
/// </remarks>
#endregion
public bool IsStickyBottom
{
get{return _isStickyBottom;}
set{_isStickyBottom = value;}
}

int _dataRowCount = 0;

#region Comments
/// <summary>
/// This function scroll the DataGrid to display a particular row.
/// </summary>
/// <remarks>If the rowNumber is greater then number of rows in
DataSource, it will scroll to the last row.</remarks>
/// <param name="rowNumber">Row index to scroll to.</param>
#endregion
public void ScrollToRow(int rowNumber)
{
WinForms.ScrollBar sbar = VertScrollBar;
WinForms.ScrollEventType sEventType;
if (rowNumber <= 0)
{
sEventType = WinForms.ScrollEventType.First;
}
else if (rowNumber >= _dataRowCount)
{
sEventType = WinForms.ScrollEventType.Last;
}
else
{
int difference = sbar.Value - rowNumber; // < 0 -- go down ; > 0
-- go up

// It is not confirmed that what will the ScrollEventType do to
the scrolling in this case.
// But I find the right ScrollEventType anyway because it will
break encapsulation otherwise.
if (difference > 0)
{
if (difference < sbar.SmallChange)
sEventType = WinForms.ScrollEventType.SmallDecrement;
else
sEventType = WinForms.ScrollEventType.LargeDecrement;
}
else
{
difference = - difference;
if (difference < sbar.LargeChange)
sEventType = WinForms.ScrollEventType.SmallIncrement;
else
sEventType = WinForms.ScrollEventType.LargeIncrement;
}
}
GridVScrolled(this, new WinForms.ScrollEventArgs(sEventType,
rowNumber));
}

private void DataGrid_Invalidated(object sender,
System.Windows.Forms.InvalidateEventArgs e)
{
#region Update _dataRowCount
System.Collections.IList dv = DataSource as
System.Collections.IList;
if (dv != null)
{
_dataRowCount = dv.Count;
}

System.ComponentModel.IListSource ds = DataSource as
System.ComponentModel.IListSource;
if (ds != null)
{
_dataRowCount = ds.GetList().Count;
}
#endregion

if (_isStickyBottom && VertScrollBar.Value == _dataRowCount -
VisibleRowCount)
{
ScrollToRow(_dataRowCount);
}

}
}
}

ClayB said:
After adding your row, you can set the currencymanager position to that row
and the datagrid will scroll to it.

this.BindingContext[this.dataGrid1.DataSource,
this.dataGrid1.DataMember].Position = myDataTable.Rows.Count;

Or, another way you can do this is to derive the DataGrid and expose a
public method that accesses the protected GridVScrolled method to create a
method that scrolls to a particular row. Here is a FAQ link on this.

How do I programmatically scroll the datagrid to a particular row?
http://www.syncfusion.com/faq/winforms/search/895.asp

=============================================
Clay Burch, .NET MVP
Syncfusion, Inc.
visit http://www.syncfusion.com for .NET Essentials



Homa said:
Hi all,

I'm using a datagrid to display some log entries. The entry populates
downward, and I want to show the latest one as it comes in.

i.e., I want the datagrid to scroll down automatically as a new row is
added to the dataTable.

however, I can't find the right event/method to handle this.

Thanks for concern,
Homa Wong
 
Back
Top