Hi all,
I was wondering if anyone can help me with a really annoying problem I have
been having. I made a derived datagrid class that will select the row when
a user clicks anywhere on a cell (multi-select without modifier keys). I
got that working fine, but I also wanted to keep rows selected after a sort,
which I do by storing the row's id in an arraylist. The idea was to do the
sort and then go back and re-select the rows with that row id, which will
now be in a different location. The problem is that to walk through the
datagrid I have to set the current cell and for some reason that is messing
up the array's values. I am at a total loss. Anyone know why this is
happening and/or know a fix? Have I screwed the code up somewhere? Any
help would be dearly appreciated. Here is my code:
public class RowSelectDataGrid : DataGrid
{
private const int OFFSET = 5;
private bool multiselect;
private string id_field;
private ArrayList data_id_array;
// these are used for single selects
private int lastrow;
private int lastID;
/// <summary>
/// Constructor for the row select data grid
/// </summary>
/// <param name="id_field_name">Name of the field that holds a record id for
your dataset</param>
/// <param name="multi">Boolean value to enable the multiselect feature of
the datagrid</param>
public RowSelectDataGrid(string id_field_name, bool multi) : base()
{
this.data_id_array = new ArrayList();
this.lastrow = -1; // default value
this.lastID = -1; // default value
this.id_field = id_field_name;
this.multiselect = multi;
}
/// <summary>
/// Select the rows that contain ids in the selections array of ints
/// </summary>
/// <param name="selections">ArrayList of the record ids to be
selected</param>
public void SetSelectionList( ArrayList selections )
{
this.data_id_array.Clear();
foreach( int id in selections )
{
if( this.multiselect )
this.data_id_array.Add( id );
else // single select only has 1 entry max
this.data_id_array[0] = id;
}
RefreshSelects();
}
/// <summary>
/// Get or Set the multiselect datagrid option
/// </summary>
public bool MultiSelect
{
get
{
return this.multiselect;
}
set
{
// when this changes, the selections are cleared
this.data_id_array.Clear();
this.multiselect = value;
}
}
/// <summary>
/// Reprocess the entire datagrid line by line, selecting previously
/// specified rows
/// </summary>
public void RefreshSelects()
{
// Debugging -- this works, just selects the wrong row
// this.Select(this.lastrow);
// go through each line and select if it is in the list, unselect otherwise
int iRow;
DataGridCell checkCell = new DataGridCell();
checkCell.ColumnNumber = 1;
BindingManagerBase bm;
DataRowView drv;
DataTable myTable = ((DataSet) this.DataSource).Tables[this.DataMember];
for( iRow = 0; iRow < myTable.Rows.Count; iRow++ )
{
// position at the first column of the current row
checkCell.RowNumber = iRow;
this.CurrentCell = checkCell; // ***************** THIS IS THE PROBLEM
LINE -- HOW DOES IT CHANGE THE PRIVATE ARRAYLIST???
bm = BindingContext[this.DataSource, this.DataMember];
drv = (DataRowView) bm.Current;
// if this row has an id in the selection list, make sure it is selected
if( this.data_id_array.Contains( Int32.Parse(
drv[this.id_field].ToString() ) ) )
{
this.Select( iRow );
// if single select, update the row (don't need to update the value)
if( !this.multiselect )
this.lastrow = iRow;
}
// else
// this.UnSelect( iRow );
}
}
/*
* Note that we can just use the datagrid's select(int row) method to do
* multiple selects and unselect to remove them.
* You should capture a column header click so that you can make sure
* the correct rows are selected after the sort
* */
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
DataGrid.HitTestInfo hti = this.HitTest(e.X, e.Y);
if( hti.Type == HitTestType.Cell )
{
// set up the data row view
DataGridCell clickedCell = new DataGridCell();
clickedCell.RowNumber = hti.Row;
clickedCell.ColumnNumber = hti.Column;
this.CurrentCell = clickedCell;
BindingManagerBase bm = BindingContext[this.DataSource, this.DataMember];
DataRowView drv = (DataRowView) bm.Current;
// if click on the append row (couldn't get it to not show) do nothing
if( drv[this.id_field].ToString() == "" )
return;
// otherwise select or unselect the row and add or remove it from the array
else
{
// if the row is currently in the selection list
if( this.data_id_array.Contains( Int32.Parse(
drv[this.id_field].ToString() ) ) )
{
this.data_id_array.Remove( Int32.Parse( drv[this.id_field].ToString() ) );
this.UnSelect( hti.Row );
// for single selects, clear the last selected row
if( !this.multiselect )
{
this.lastrow = -1;
this.lastID = -1;
}
}
// if it is not in the selection list
else
{
this.data_id_array.Add( Int32.Parse( drv[this.id_field].ToString() ) );
this.Select( hti.Row );
// for single selects, unselect the old, remove it from the list
// and update lastrow
if( !this.multiselect )
{
if( this.lastrow != -1 )
{
this.UnSelect( this.lastrow );
this.data_id_array.Remove( this.lastID );
}
this.lastrow = hti.Row;
this.lastID = Int32.Parse( drv[this.id_field].ToString() );
}
}
}
}
// sort and then fix the selections (since rows have changed)
else if( hti.Type == HitTestType.ColumnHeader )
{
base.OnMouseDown(e);
// this.RefreshSelects();
}
else
{
base.OnMouseDown(e);
}
}
protected override void OnCurrentCellChanged(System.EventArgs e)
{
base.OnCurrentCellChanged(e);
if(Control.MouseButtons != MouseButtons.Left)
{
Rectangle rect = this.GetCellBounds(this.CurrentCell);
MouseEventArgs e1 = new MouseEventArgs(MouseButtons.Left, 0, OFFSET, rect.Y
+ OFFSET, 0);
OnMouseDown(e1);
}
}
}
/// <summary>
/// This section of is very basic, but effective. By overriding the Edit
function, a cell in the column
/// cannot become active for editing, allowing the datagrid events to be
handled.
/// </summary>
public class DataGridNoActiveCellColumn : DataGridTextBoxColumn
{
protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string
instantText,
bool cellIsVisible)
{
// Empty. Do NOT call the base class!
}
} 2 9888
Hi Chris,
Just some general observations:
1. The grid itself cannot modify data it knows nothing about. Therefore,
it's your code that actually corrupts the array list. Set breakpoints in
every function that modifies the list of identifiers and run the application
in the debugger to see how this happens.
2. The new row can be hidden by the following code:
CurrencyManager cm = (CurrencyManager)BindingContext[this.DataSource,
this.DataMember];
DataView view = (DataView)cm.List;
view.AllowNew = false;
--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE
"Chris Plowman" <ch***********@hilton.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl... Hi all, I was wondering if anyone can help me with a really annoying problem I
have been having. I made a derived datagrid class that will select the row
when a user clicks anywhere on a cell (multi-select without modifier keys). I got that working fine, but I also wanted to keep rows selected after a
sort, which I do by storing the row's id in an arraylist. The idea was to do
the sort and then go back and re-select the rows with that row id, which will now be in a different location. The problem is that to walk through the datagrid I have to set the current cell and for some reason that is
messing up the array's values. I am at a total loss. Anyone know why this is happening and/or know a fix? Have I screwed the code up somewhere? Any help would be dearly appreciated. Here is my code:
public class RowSelectDataGrid : DataGrid
{
private const int OFFSET = 5;
private bool multiselect;
private string id_field;
private ArrayList data_id_array;
// these are used for single selects
private int lastrow;
private int lastID;
/// <summary>
/// Constructor for the row select data grid
/// </summary>
/// <param name="id_field_name">Name of the field that holds a record id
for your dataset</param>
/// <param name="multi">Boolean value to enable the multiselect feature of the datagrid</param>
public RowSelectDataGrid(string id_field_name, bool multi) : base()
{
this.data_id_array = new ArrayList();
this.lastrow = -1; // default value
this.lastID = -1; // default value
this.id_field = id_field_name;
this.multiselect = multi;
} /// <summary>
/// Select the rows that contain ids in the selections array of ints
/// </summary>
/// <param name="selections">ArrayList of the record ids to be selected</param>
public void SetSelectionList( ArrayList selections )
{
this.data_id_array.Clear();
foreach( int id in selections )
{
if( this.multiselect )
this.data_id_array.Add( id );
else // single select only has 1 entry max
this.data_id_array[0] = id;
}
RefreshSelects();
} /// <summary>
/// Get or Set the multiselect datagrid option
/// </summary>
public bool MultiSelect
{
get
{
return this.multiselect;
}
set
{
// when this changes, the selections are cleared
this.data_id_array.Clear();
this.multiselect = value;
}
} /// <summary>
/// Reprocess the entire datagrid line by line, selecting previously
/// specified rows
/// </summary>
public void RefreshSelects()
{
// Debugging -- this works, just selects the wrong row
// this.Select(this.lastrow);
// go through each line and select if it is in the list, unselect
otherwise int iRow;
DataGridCell checkCell = new DataGridCell();
checkCell.ColumnNumber = 1;
BindingManagerBase bm;
DataRowView drv;
DataTable myTable = ((DataSet) this.DataSource).Tables[this.DataMember];
for( iRow = 0; iRow < myTable.Rows.Count; iRow++ )
{
// position at the first column of the current row
checkCell.RowNumber = iRow;
this.CurrentCell = checkCell; // ***************** THIS IS THE PROBLEM LINE -- HOW DOES IT CHANGE THE PRIVATE ARRAYLIST???
bm = BindingContext[this.DataSource, this.DataMember];
drv = (DataRowView) bm.Current;
// if this row has an id in the selection list, make sure it is selected
if( this.data_id_array.Contains( Int32.Parse( drv[this.id_field].ToString() ) ) )
{
this.Select( iRow );
// if single select, update the row (don't need to update the value)
if( !this.multiselect )
this.lastrow = iRow;
}
// else
// this.UnSelect( iRow );
}
} /*
* Note that we can just use the datagrid's select(int row) method to do
* multiple selects and unselect to remove them.
* You should capture a column header click so that you can make sure
* the correct rows are selected after the sort
* */
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
DataGrid.HitTestInfo hti = this.HitTest(e.X, e.Y);
if( hti.Type == HitTestType.Cell )
{
// set up the data row view
DataGridCell clickedCell = new DataGridCell();
clickedCell.RowNumber = hti.Row;
clickedCell.ColumnNumber = hti.Column;
this.CurrentCell = clickedCell;
BindingManagerBase bm = BindingContext[this.DataSource, this.DataMember];
DataRowView drv = (DataRowView) bm.Current;
// if click on the append row (couldn't get it to not show) do nothing
if( drv[this.id_field].ToString() == "" )
return;
// otherwise select or unselect the row and add or remove it from the
array else
{
// if the row is currently in the selection list
if( this.data_id_array.Contains( Int32.Parse( drv[this.id_field].ToString() ) ) )
{
this.data_id_array.Remove( Int32.Parse( drv[this.id_field].ToString() ) );
this.UnSelect( hti.Row );
// for single selects, clear the last selected row
if( !this.multiselect )
{
this.lastrow = -1;
this.lastID = -1;
}
}
// if it is not in the selection list
else
{
this.data_id_array.Add( Int32.Parse( drv[this.id_field].ToString() ) );
this.Select( hti.Row );
// for single selects, unselect the old, remove it from the list
// and update lastrow
if( !this.multiselect )
{
if( this.lastrow != -1 )
{
this.UnSelect( this.lastrow );
this.data_id_array.Remove( this.lastID );
}
this.lastrow = hti.Row;
this.lastID = Int32.Parse( drv[this.id_field].ToString() );
}
}
}
}
// sort and then fix the selections (since rows have changed)
else if( hti.Type == HitTestType.ColumnHeader )
{
base.OnMouseDown(e);
// this.RefreshSelects();
}
else
{
base.OnMouseDown(e);
}
} protected override void OnCurrentCellChanged(System.EventArgs e)
{
base.OnCurrentCellChanged(e);
if(Control.MouseButtons != MouseButtons.Left)
{
Rectangle rect = this.GetCellBounds(this.CurrentCell);
MouseEventArgs e1 = new MouseEventArgs(MouseButtons.Left, 0, OFFSET,
rect.Y + OFFSET, 0);
OnMouseDown(e1);
}
}
}
/// <summary>
/// This section of is very basic, but effective. By overriding the Edit function, a cell in the column
/// cannot become active for editing, allowing the datagrid events to be handled.
/// </summary>
public class DataGridNoActiveCellColumn : DataGridTextBoxColumn
{
protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText,
bool cellIsVisible)
{
// Empty. Do NOT call the base class!
}
}
thanks for the info -- I was able to fix the problem using a temp variable
in the function itself as a copy of the datagrid's local arraylist and using
that. At the end of the function I overwrite the now screwed up local
arraylist with the copy, restoring it to its original data. I also tracked
down the error is caused because setting the current cell of the datagrid
(this.CurrentCell = checkCell;) causes the OnCurrentCellChange event to
fire, which in turn calls the mousedown event! Pitiful oversight on my
part. Thanks for the help.
Thank you for the info about hiding the new row, I will definetly give that
a try.
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:e7**************@TK2MSFTNGP10.phx.gbl... Hi Chris,
Just some general observations:
1. The grid itself cannot modify data it knows nothing about. Therefore, it's your code that actually corrupts the array list. Set breakpoints in every function that modifies the list of identifiers and run the
application in the debugger to see how this happens.
2. The new row can be hidden by the following code:
CurrencyManager cm = (CurrencyManager)BindingContext[this.DataSource, this.DataMember]; DataView view = (DataView)cm.List; view.AllowNew = false;
-- Dmitriy Lapshin [C# / .NET MVP] X-Unity Test Studio http://x-unity.miik.com.ua/teststudio.aspx Bring the power of unit testing to VS .NET IDE
"Chris Plowman" <ch***********@hilton.com> wrote in message news:%2***************@TK2MSFTNGP11.phx.gbl... Hi all, I was wondering if anyone can help me with a really annoying problem I have been having. I made a derived datagrid class that will select the row when a user clicks anywhere on a cell (multi-select without modifier keys).
I got that working fine, but I also wanted to keep rows selected after a sort, which I do by storing the row's id in an arraylist. The idea was to do the sort and then go back and re-select the rows with that row id, which
will now be in a different location. The problem is that to walk through the datagrid I have to set the current cell and for some reason that is messing up the array's values. I am at a total loss. Anyone know why this is happening and/or know a fix? Have I screwed the code up somewhere? Any help would be dearly appreciated. Here is my code:
public class RowSelectDataGrid : DataGrid
{
private const int OFFSET = 5;
private bool multiselect;
private string id_field;
private ArrayList data_id_array;
// these are used for single selects
private int lastrow;
private int lastID;
/// <summary>
/// Constructor for the row select data grid
/// </summary>
/// <param name="id_field_name">Name of the field that holds a record id for your dataset</param>
/// <param name="multi">Boolean value to enable the multiselect feature
of the datagrid</param>
public RowSelectDataGrid(string id_field_name, bool multi) : base()
{
this.data_id_array = new ArrayList();
this.lastrow = -1; // default value
this.lastID = -1; // default value
this.id_field = id_field_name;
this.multiselect = multi;
} /// <summary>
/// Select the rows that contain ids in the selections array of ints
/// </summary>
/// <param name="selections">ArrayList of the record ids to be selected</param>
public void SetSelectionList( ArrayList selections )
{
this.data_id_array.Clear();
foreach( int id in selections )
{
if( this.multiselect )
this.data_id_array.Add( id );
else // single select only has 1 entry max
this.data_id_array[0] = id;
}
RefreshSelects();
} /// <summary>
/// Get or Set the multiselect datagrid option
/// </summary>
public bool MultiSelect
{
get
{
return this.multiselect;
}
set
{
// when this changes, the selections are cleared
this.data_id_array.Clear();
this.multiselect = value;
}
} /// <summary>
/// Reprocess the entire datagrid line by line, selecting previously
/// specified rows
/// </summary>
public void RefreshSelects()
{
// Debugging -- this works, just selects the wrong row
// this.Select(this.lastrow);
// go through each line and select if it is in the list, unselect otherwise int iRow;
DataGridCell checkCell = new DataGridCell();
checkCell.ColumnNumber = 1;
BindingManagerBase bm;
DataRowView drv;
DataTable myTable = ((DataSet) this.DataSource).Tables[this.DataMember];
for( iRow = 0; iRow < myTable.Rows.Count; iRow++ )
{
// position at the first column of the current row
checkCell.RowNumber = iRow;
this.CurrentCell = checkCell; // ***************** THIS IS THE PROBLEM LINE -- HOW DOES IT CHANGE THE PRIVATE ARRAYLIST???
bm = BindingContext[this.DataSource, this.DataMember];
drv = (DataRowView) bm.Current;
// if this row has an id in the selection list, make sure it is selected
if( this.data_id_array.Contains( Int32.Parse( drv[this.id_field].ToString() ) ) )
{
this.Select( iRow );
// if single select, update the row (don't need to update the value)
if( !this.multiselect )
this.lastrow = iRow;
}
// else
// this.UnSelect( iRow );
}
} /*
* Note that we can just use the datagrid's select(int row) method to do
* multiple selects and unselect to remove them.
* You should capture a column header click so that you can make sure
* the correct rows are selected after the sort
* */
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs
e) {
DataGrid.HitTestInfo hti = this.HitTest(e.X, e.Y);
if( hti.Type == HitTestType.Cell )
{
// set up the data row view
DataGridCell clickedCell = new DataGridCell();
clickedCell.RowNumber = hti.Row;
clickedCell.ColumnNumber = hti.Column;
this.CurrentCell = clickedCell;
BindingManagerBase bm = BindingContext[this.DataSource,
this.DataMember]; DataRowView drv = (DataRowView) bm.Current;
// if click on the append row (couldn't get it to not show) do nothing
if( drv[this.id_field].ToString() == "" )
return;
// otherwise select or unselect the row and add or remove it from the
array else
{
// if the row is currently in the selection list
if( this.data_id_array.Contains( Int32.Parse( drv[this.id_field].ToString() ) ) )
{
this.data_id_array.Remove( Int32.Parse(
drv[this.id_field].ToString() ) ); this.UnSelect( hti.Row );
// for single selects, clear the last selected row
if( !this.multiselect )
{
this.lastrow = -1;
this.lastID = -1;
}
}
// if it is not in the selection list
else
{
this.data_id_array.Add( Int32.Parse( drv[this.id_field].ToString() ) );
this.Select( hti.Row );
// for single selects, unselect the old, remove it from the list
// and update lastrow
if( !this.multiselect )
{
if( this.lastrow != -1 )
{
this.UnSelect( this.lastrow );
this.data_id_array.Remove( this.lastID );
}
this.lastrow = hti.Row;
this.lastID = Int32.Parse( drv[this.id_field].ToString() );
}
}
}
}
// sort and then fix the selections (since rows have changed)
else if( hti.Type == HitTestType.ColumnHeader )
{
base.OnMouseDown(e);
// this.RefreshSelects();
}
else
{
base.OnMouseDown(e);
}
} protected override void OnCurrentCellChanged(System.EventArgs e)
{
base.OnCurrentCellChanged(e);
if(Control.MouseButtons != MouseButtons.Left)
{
Rectangle rect = this.GetCellBounds(this.CurrentCell);
MouseEventArgs e1 = new MouseEventArgs(MouseButtons.Left, 0, OFFSET,
rect.Y + OFFSET, 0);
OnMouseDown(e1);
}
}
}
/// <summary>
/// This section of is very basic, but effective. By overriding the
Edit function, a cell in the column
/// cannot become active for editing, allowing the datagrid events to be handled.
/// </summary>
public class DataGridNoActiveCellColumn : DataGridTextBoxColumn
{
protected override void Edit(System.Windows.Forms.CurrencyManager
source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText,
bool cellIsVisible)
{
// Empty. Do NOT call the base class!
}
}
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Joe Bloggs |
last post by:
I am trying display the contents of a table in a web page, select
certain rows from that table and then display the fields that I have
selected (now table columns) as text in a Label object....
|
by: Jeff |
last post by:
IDE: VS 2003 :NET
OS: XP Pro
My app have a form with a tab-control on it. The tab-control have 2
tabpages. One of the tabpages displays a datagrid, and the other tabpage
displays details (order...
|
by: Steve Chatham |
last post by:
I use the following code:
Private Sub RbtnExport_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles RbtnExport.SelectedIndexChanged
Dim sFile As String =...
|
by: The Alchemist |
last post by:
I am having a problem with a dynamically-generated Datagrid. It is
important to point out that this problem does not exist with a
design-time created Datagrid, but only with a dynamically generated...
|
by: Neo Geshel |
last post by:
Greetings.
I have a serious problem. I have multiple sets of tables, several of
which are chained more than two tables deep. That is, I have a parent, a
child, and a great-grandchild table.
...
|
by: Girish |
last post by:
OK.. phew. Playing with data grids for the past few days has been fun and a
huge learning experience..
My problem. I have a requirement to display a gird with a gird. Within the
embedded grid,...
|
by: rupalirane07 |
last post by:
Both grids displays fine. But the problem is only parent datagrid sorting works fine but when i clik on child datagrid for sorting it gives me error:
NullReferenceException error
Any...
|
by: Brock |
last post by:
Thanks in advance... (you can see a screenshot of what my form looks
like currently at http://www.juggernautical.com/DataGrid.jpg - the
Datalist is super-imposed in 'design view' but the DataGrid...
|
by: =?Utf-8?B?Y3JlYXZlczA2MjI=?= |
last post by:
I have a nested datagrid in a xaml file, the parent datagrid loads the vendor
information and the details loads the documents for that vendor in a
datagrid. Everything is working fine until I click...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |