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" <(E-Mail Removed)>
>Sender: "Ian Williamson" <(E-Mail Removed)>
>References: <1321501c38459$98b88090$(E-Mail Removed)>
<(E-Mail Removed)>
>Subject: RE: DataView.RowFilter ArgumentOutOfRangeException
>Date: Fri, 26 Sep 2003 13:46:19 -0700
>Lines: 124
>Message-ID: <1d9301c3846f$3cc97da0$(E-Mail Removed)>
>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
>
>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
>
>
>>-----Original Message-----
>>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" <(E-Mail Removed)>
>>>Sender: "Ian Williamson" <(E-Mail Removed)>
>>>Subject: DataView.RowFilter ArgumentOutOfRangeException
>>>Date: Fri, 26 Sep 2003 11:11:24 -0700
>>>Lines: 31
>>>Message-ID: <1321501c38459$98b88090$(E-Mail Removed)>
>>>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
>>>
>>>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
>>>
>>>
>>>
>>>
>>
>>.
>>
>