Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DataView.RowFilter ArgumentOutOfRangeException

7 views
Skip to first unread message

Ian Williamson

unread,
Sep 26, 2003, 2:11:24 PM9/26/03
to
Well, I am baffled by this one since it only happens
about 50% of the time.

I have a DataGrid whose underlying table I assign to a
DataView and then apply a RowFilter. This view then gets
passed to another member which overlays a panel and
displays a single row.

When I use the following code:

DataView view = myDataGrid.Tables[0].DefaultView;
view.RowFilter = "ID=" + myDataGrid.Tables[0].Rows
[this.CurrentRowIndex]["ID"];

I get an ArgumentOutOfRangeException... sometimes. The
ID column is declared as an Identity and the RowState is
Unchanged.

The funny thing is, the DataView gets filtered just
fine. It now contains one row which is the one expected.

The most frustrating part is I cannot trap this error.
Wrapping it in a try/catch block does nothing.

I sure hope someone has seen this behaviour before and
knows what to do since I am at a complete loss.

Cheers, Ian Williamson

Michel Renaud

unread,
Sep 26, 2003, 2:29:26 PM9/26/03
to
This looks VERY similar to a problem I had earlier this
week. I have a form with two grids. The top grid has a
list of items that can be added to the bottom grid. When
I do that, I remove the item from the top grid. Even
though it seemed to work fine with some quick tests, when
I started playing with it more, I started getting
ArgumentOutOfRange exceptions. I also had the same
problem you did: I couldn't trap the exception in a
try..catch block.

After testing various things for hours, I came to the
conclusion that the datagrid seems to sometimes "randomly"
get lost when its contents are altered-more specically the
number of rows in its datasource (I'm also using a
Dataview in the grids, although the problem doesn't seem
to be limited to views.)

The solution I found is kind of dirty, but it works:

- Set the grid's datasource to "nothing"
- Perform changes to the view (eg. change the filter)
- Set the grid's datasource back to the view

In my case the solution, while I don't really like it, is
acceptable: It seems to be quick enough that the user
doesn't notice anything. I also have to preserve the
CurrentRowIndex before setting the datasource to nothing
so that I can re-set it afterwards (otherwise it'll jump
back to the first record at the top of the grid.) Of
course I need to ensure the row index is actually valid
after I've removed the item from the view.

Unless I missed something obvious, this seems to be a bug
in the DataGrid's implementation. I'm not sure how to
report this to Microsoft.

Hope this helps...

Michel

>.
>

Ilya Tumanov [MS]

unread,
Sep 26, 2003, 4:32:31 PM9/26/03
to
Ian,

The reason for this is quite simple - row index in the grid has nothing to
do with row index in the DataTable.
This is because grid is bound not to the table itself, but to the
underlying DataView:

DataTable DataView(sorted/filtered) Grid
R1 R3 R3 <<
Selected, index 0
R2 R2 R2
R3 R1 R1

As you can see, selected row R3 in the Grid is at index 0.
Say, you'd like to delete this row.
If you use Grid's index on a DataTable, you would delete row R1 instead.
This would also messed up indexes, causing all kind of problems including
exceptions you see.

To fix the problem, you should not use grid's index to access records in
the DataTable.
You can, however, use this index to access records in the DataView which
servers as a grid's DataSource.

If you bound you grid directly to the DataTable, use table's default view.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
>Content-Class: urn:content-classes:message
>From: "Ian Williamson" <bmcp...@nospam.shaw.ca>
>Sender: "Ian Williamson" <bmcp...@nospam.shaw.ca>
>Subject: DataView.RowFilter ArgumentOutOfRangeException
>Date: Fri, 26 Sep 2003 11:11:24 -0700
>Lines: 31
>Message-ID: <1321501c38459$98b88090$a601...@phx.gbl>
>MIME-Version: 1.0
>Content-Type: text/plain;
> charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
>Thread-Index: AcOEWZi4HFuu9LL0TFWHrdLettQzIw==
>Newsgroups: microsoft.public.dotnet.framework.compactframework
>Path: cpmsftngxa06.phx.gbl
>Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.compactframework:34525
>NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
>X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

Ian Williamson

unread,
Sep 26, 2003, 4:46:19 PM9/26/03
to
Thanks for your reply Ilya. I am familiar with how the
record pointers work and the Filter expression is valid
in this instance.

This is the only instance that the Grid has a Filter/Sort
applied do it during the application.

The problem turned out to be a DataBinding on a Textbox
that was part of the other panel that this view was being
applied to. I was not releasing the binding prior to
applying new Filter and hence the error.

Chalk one up to tunnel vision I guess.

Cheers, Ian

>.
>

Ian Williamson

unread,
Sep 26, 2003, 4:47:44 PM9/26/03
to
Thanks for the quick reply Michel. After getting a beer
and some curry in my belly I was properly fortified to
tackle this little issue.

It turned out to not be a bug with CF, but a bug with
me. See my reply to Ilya's response if you are curious.

Cheers, Ian

>.
>

Ian Williamson

unread,
Sep 26, 2003, 5:13:40 PM9/26/03
to
Actually, after all is said and done, I had to both Clear
the databindings and apply what you suggested.

Thanks again.

>.
>

Ilya Tumanov [MS]

unread,
Sep 26, 2003, 8:31:35 PM9/26/03
to
Ian,

I'm glad it works now.

However, it's not necessary to release DataBinding prior to changing
DataView's Sort and/or Filter.
Please take a look at this small sample which filters data in the datagrid
as you type.
It also can add, update and delete rows.

It uses SQL Server CE and Northwind database to populate DataTable.
You'll need to add reference to System.Data.SqlServerCe.dll and
System.Data.Common.dll
and include Northwind.sdf in your project so it would be deployed to the
device.
Or you can change the code to use data source of your choice.

Best regards,

Ilya

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Data.Common;
using System.Data.SqlServerCe;
using System.Text;
using System.IO;
using System.Threading;

namespace Customers
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid Grid;
private System.Windows.Forms.Button QuitButton;
private System.Windows.Forms.MainMenu mainMenu1;

private System.Windows.Forms.Button DeleteButton;
private System.Windows.Forms.Button InserButton;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button DataSetButton;
private System.Windows.Forms.Button UpdateButton;

private SqlCeConnection connection;
private DataTable table;

private string myPath = "Data Source = " +
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetN
ame().CodeBase) + "\\Northwind.sdf";

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

if ( connection == null )
{
connection = new SqlCeConnection ( myPath );
connection.Open();
}


//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.QuitButton = new System.Windows.Forms.Button();
this.DeleteButton = new System.Windows.Forms.Button();
this.InserButton = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.DataSetButton = new System.Windows.Forms.Button();
this.UpdateButton = new System.Windows.Forms.Button();
this.Grid = new System.Windows.Forms.DataGrid();
//
// QuitButton
//
this.QuitButton.Location = new System.Drawing.Point(168, 248);
this.QuitButton.Size = new System.Drawing.Size(64, 20);
this.QuitButton.Text = "Quit";
this.QuitButton.Click += new
System.EventHandler(this.QuitButton_Click);
//
// DeleteButton
//
this.DeleteButton.Location = new System.Drawing.Point(8, 248);
this.DeleteButton.Size = new System.Drawing.Size(64, 20);
this.DeleteButton.Text = "Delete";
this.DeleteButton.Click += new
System.EventHandler(this.DeleteButton_Click);
//
// InserButton
//
this.InserButton.Location = new System.Drawing.Point(88, 224);
this.InserButton.Size = new System.Drawing.Size(64, 20);
this.InserButton.Text = "Insert";
this.InserButton.Click += new
System.EventHandler(this.InserButton_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 192);
this.textBox1.Size = new System.Drawing.Size(224, 22);
this.textBox1.Text = "";
this.textBox1.TextChanged += new
System.EventHandler(this.textBox1_TextChanged);
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 168);
this.label1.Size = new System.Drawing.Size(200, 16);
this.label1.Text = "Click on Load...";
this.label1.TextAlign =
System.Drawing.ContentAlignment.TopCenter;
//
// DataSetButton
//
this.DataSetButton.Location = new System.Drawing.Point(8, 224);
this.DataSetButton.Size = new System.Drawing.Size(64, 20);
this.DataSetButton.Text = "Load";
this.DataSetButton.Click += new
System.EventHandler(this.DataSetButton_Click);
//
// UpdateButton
//
this.UpdateButton.Location = new System.Drawing.Point(88, 248);
this.UpdateButton.Size = new System.Drawing.Size(64, 20);
this.UpdateButton.Text = "Update";
this.UpdateButton.Click += new
System.EventHandler(this.UpdateButton_Click);
//
// Grid
//
this.Grid.Location = new System.Drawing.Point(0, 8);
this.Grid.Size = new System.Drawing.Size(240, 152);
this.Grid.Text = "dataGrid1";
//
// Form1
//
this.Controls.Add(this.UpdateButton);
this.Controls.Add(this.DataSetButton);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.InserButton);
this.Controls.Add(this.DeleteButton);
this.Controls.Add(this.QuitButton);
this.Controls.Add(this.Grid);
this.Menu = this.mainMenu1;
this.Text = "DataView Demo";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>


static void Main()
{
Application.Run(new Form1());
}

private void QuitButton_Click(object sender, System.EventArgs e)
{
this.Close();
}

private void textBox1_TextChanged(object sender, System.EventArgs e)
{
table.DefaultView.RowFilter = textBox1.Text == null ? "" :
String.Format ("[LastName] LIKE '{0}%'", textBox1.Text);
}

private void DeleteButton_Click(object sender, System.EventArgs e)
{
table.DefaultView.Delete(Grid.CurrentRowIndex);
}

private void InserButton_Click(object sender, System.EventArgs e)
{
table.LoadDataRow(new object[] { 0, "Doe", "Jonh" }, true);
}

private void UpdateButton_Click(object sender, System.EventArgs e)
{
string name = "John";

if (table.DefaultView[Grid.CurrentRowIndex]["FirstName"] !=
DBNull.Value &&
(string)table.DefaultView[Grid.CurrentRowIndex]["FirstName"] == name)
{
name = "Jane";
}

table.DefaultView[Grid.CurrentRowIndex]["FirstName"] = name;

Grid.Refresh();
}

private void DataSetButton_Click(object sender, System.EventArgs e)
{
SqlCeDataAdapter da = new SqlCeDataAdapter ("Select EmployeeID,
LastName, FirstName from Employees", connection );

table = new DataTable();

da.Fill(table);

Grid.DataSource = table;

this.label1.Text = "Using DataTable";
}
}
}

This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
>Content-Class: urn:content-classes:message
>From: "Ian Williamson" <bmcp...@nospam.shaw.ca>
>Sender: "Ian Williamson" <bmcp...@nospam.shaw.ca>

>References: <1321501c38459$98b88090$a601...@phx.gbl>
<y6XsU1G...@cpmsftngxa06.phx.gbl>
>Subject: RE: DataView.RowFilter ArgumentOutOfRangeException
>Date: Fri, 26 Sep 2003 13:46:19 -0700
>Lines: 124
>Message-ID: <1d9301c3846f$3cc97da0$a001...@phx.gbl>


>MIME-Version: 1.0
>Content-Type: text/plain;
> charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300

>Thread-Index: AcOEbzzHAWLOZ/kFR7ON37FyNtD6tQ==


>Newsgroups: microsoft.public.dotnet.framework.compactframework
>Path: cpmsftngxa06.phx.gbl
>Xref: cpmsftngxa06.phx.gbl

microsoft.public.dotnet.framework.compactframework:34534
>NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
>X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

0 new messages