473,725 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Linq-SQL canonical editable datagridview sample code

Marc,
>I don't know if there is an easier way, but how about:
Thank you very much. I have issue on implementing add row properly using
this.

User presses down arrow in last row in grid starting adding new row.
Then user changes its mind desiding that new row should not added and
presses up arrow.
DataGridView does not show this unfinished row anymore.

However entity remains in DataContext and is added to database on
SubmitChanges.
How to prevent this ghost entity addition ?

Andrus.
Jun 27 '08 #1
18 6053
Probably you'd need to implement ICancelAddNew, but taking a peek,
BindingList<Tal ready does this, calling RemoveItem correctly...
hmmm... I'll investigate...
Jun 27 '08 #2
I've looked at this with LINQ-to-SQL, and I cannot reproduce the
issue; if I add and remove a row (in any combination of immediate and
delayed cancel), then no change is applied to the database.

So: are you using LINQ-to-SQL, or are you using DbLinq? If the latter,
then I suspect it has "issues" noticing this trivial change (trivial
meant in the literal sense - not intended to be patronising).

You might be able to write the list to track insertions itself, but
this should be the job of the data-context. Of course, if this
actually is LINQ-to-SQL, please let me know and I'll retry...

Marc
Jun 27 '08 #3
Marc,
>Probably you'd need to implement ICancelAddNew, but taking a peek,
BindingList<Ta lready does this, calling RemoveItem correctly...
I observed the following:

1. Pressing down arrow in last line of DataGridView calls AddNewCore() which
adds new entity to
DataContext.

2. Pressing Up Arrow doest *not* call RemoveItem. So added fake entity will
me saved on SubmitChanges() .

I'm wondering how Linq-SQL performs this correctly since RemoveItem() is not
called.

I'm using DbLinq but this should not depend on Linq provider.

Andrus.
Jun 27 '08 #4
Marc,
I've looked at this with LINQ-to-SQL, and I cannot reproduce the
issue; if I add and remove a row (in any combination of immediate and
delayed cancel), then no change is applied to the database.
I'm planning to fix this in the following way:

1. AddNewCode() assigns new entity to property only, will not add to
DataContext
2. Override ICancelAddNew EndNew() method and add new entity to DataContext
in this method.

Will this work OK ?

Andrus.
Jun 27 '08 #5
Probably - but you can't *fully* verify the cancel conditions (the
index etc) since they aren't available as protected... I added
Console.WriteLi ne to RemoveItem, and it seemed to be working fine...

Marc
Jun 27 '08 #6
Marc,
Probably - but you can't *fully* verify the cancel conditions (the
index etc) since they aren't available as protected... I added
Console.WriteLi ne to RemoveItem, and it seemed to be working fine...
I found that this occurs only when I move up-down in my
CustomDataGridV iewComBobox column.

Down arrow in this column invokes AddNewCore().
Up arrow does *not* invoke RemoveItem.
Pressing down arrow again causes InvalidOperatio nException in
base.AddNewCore ()

It seems that my combobox column class blocks bindinglist RemoveItem call.
I checked my class overridden methods and it seems that I'm calling base
methods in most cases.
Which combobox columns method calls bindinglist RemoveItem() ?
I implemented combobox column using MSDN sample code.

Any idea how to debug / resolve this issue? Exception which I got is below.

Andrus.

System.InvalidO perationExcepti on was unhandled
Message="Operat ion is not valid due to the current state of the object."
Source="System. Windows.Forms"
StackTrace:
at
System.Windows. Forms.DataGridV iew.DataGridVie wDataConnection .ProcessListCha nged(ListChange dEventArgs
e)
at
System.Windows. Forms.DataGridV iew.DataGridVie wDataConnection .currencyManage r_ListChanged(O bject
sender, ListChangedEven tArgs e)
at
System.Windows. Forms.CurrencyM anager.OnListCh anged(ListChang edEventArgs e)
at System.Windows. Forms.CurrencyM anager.List_Lis tChanged(Object
sender, ListChangedEven tArgs e)
at
System.Componen tModel.BindingL ist`1.OnListCha nged(ListChange dEventArgs e)
at System.Componen tModel.BindingL ist`1.InsertIte m(Int32 index, T
item)
at System.Collecti ons.ObjectModel .Collection`1.A dd(T item)
at System.Componen tModel.BindingL ist`1.AddNewCor e()
at MyAppl.TableLis t`1.AddNewCore( ) in I:\MyAppl\Table List.cs:line 92
at
System.Componen tModel.BindingL ist`1.System.Co mponentModel.IB indingList.AddN ew()
at System.Windows. Forms.CurrencyM anager.AddNew()
at
System.Windows. Forms.DataGridV iew.DataGridVie wDataConnection .AddNew()
at
System.Windows. Forms.DataGridV iew.DataGridVie wDataConnection .OnNewRowNeeded ()
at System.Windows. Forms.DataGridV iew.OnRowEnter( DataGridViewCel l&
dataGridViewCel l, Int32 columnIndex, Int32 rowIndex, Boolean
canCreateNewRow , Boolean validationFailu reOccurred)
at System.Windows. Forms.DataGridV iew.SetCurrentC ellAddressCore( Int32
columnIndex, Int32 rowIndex, Boolean setAnchorCellAd dress, Boolean
validateCurrent Cell, Boolean throughMouseCli ck)
at System.Windows. Forms.DataGridV iew.ProcessDown KeyInternal(Key s
keyData, Boolean& moved)
at System.Windows. Forms.DataGridV iew.ProcessDown Key(Keys keyData)
at
System.Windows. Forms.DataGridV iew.ProcessData GridViewKey(Key EventArgs e)
at System.Windows. Forms.DataGridV iew.ProcessKeyP review(Message& m)
at System.Windows. Forms.Control.P rocessKeyPrevie w(Message& m)
at System.Windows. Forms.Control.P rocessKeyMessag e(Message& m)
at System.Windows. Forms.ComboBox. ChildWndProc(Me ssage& m)
at
System.Windows. Forms.ComboBox. ComboBoxChildNa tiveWindow.WndP roc(Message& m)
at System.Windows. Forms.NativeWin dow.DebuggableC allback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows. Forms.UnsafeNat iveMethods.Disp atchMessageW(MS G&
msg)
at
....
Jun 27 '08 #7
Sorry - I don't think I'm going to be able to debug that just from a
stacktrace, and I'm not in a desparate hurry to pick through your full
bespoke code... sorry again...

Marc
Jun 27 '08 #8
Marc,

I discovered that this occurs when all following conditions are met:

1. DataGridView contains single row
2. DataGridViewCol umn column is DataGridViewCom boBoxColumn
3. EditMode is EditOnEnter

In this case pressing up arrow in new row does *not* call BindingList
RemoveItem() method.

Is this .NET bug ? Any idea how to fix it ?

Andrus.
Jun 27 '08 #9
Following code reproduces DataGridView crash on edit.
Press down arrow, up arrow, down arrow. Exception occurs.

Any idea how to fix ?

Andrus.

using System;
using System.Windows. Forms;
using System.Componen tModel;

class Supplier {
public string Id { get; set; }
}

class Form1 : Form {
[STAThread]
static void Main() {
Application.Run (new Form1());
}

public Form1() {
DataGridView grid = new DataGridView();
// if this line is commented out, all is OK:
grid.EditMode = DataGridViewEdi tMode.EditOnEnt er;
ComboBoxColumn comboBoxColumn = new ComboBoxColumn( );
ComboBoxCell ComboBoxCell = new ComboBoxCell();
comboBoxColumn. CellTemplate = ComboBoxCell;
grid.Columns.Ad d(comboBoxColum n);
BindingList<Sup plierl = new BindingList<Sup plier>();
l.Add(new Supplier());
grid.DataSource = l;
Controls.Add(gr id);
}

class ComboBoxColumn : DataGridViewCom boBoxColumn { }

class ComboBoxCell : DataGridViewCom boBoxCell {
public override Type EditType {
get {
return typeof(ComboBox EditingControl) ;
}
}

}

class ComboBoxEditing Control : ComboBox, IDataGridViewEd itingControl {
protected int rowIndex;
protected DataGridView dataGridView;
protected bool valueChanged = false;

protected override void OnTextChanged(E ventArgs e) {
base.OnTextChan ged(e);
NotifyDataGridV iewOfValueChang e();
}

protected virtual void NotifyDataGridV iewOfValueChang e() {
valueChanged = true;
if (dataGridView != null) {
dataGridView.No tifyCurrentCell Dirty(true);
}
}

public Cursor EditingPanelCur sor {
get {
return Cursors.IBeam;
}
}

public DataGridView EditingControlD ataGridView {
get {
return dataGridView;
}
set {
dataGridView = value;
}
}

public object EditingControlF ormattedValue {
set {
if (value.ToString () != Text) {
Text = value.ToString( );
NotifyDataGridV iewOfValueChang e();
}
}

get {
return Text;
}
}

public object
GetEditingContr olFormattedValu e(DataGridViewD ataErrorContext s
context) {
return Text;
}

public void PrepareEditingC ontrolForEdit(b ool selectAll) { }

public bool RepositionEditi ngControlOnValu eChange {
get {
return false;
}
}

public int EditingControlR owIndex {
get {
return rowIndex;
}

set {
rowIndex = value;
}
}

public void ApplyCellStyleT oEditingControl (DataGridViewCe llStyle
dataGridViewCel lStyle) {
DropDownStyle = ComboBoxStyle.D ropDown;
}

public bool EditingControlW antsInputKey(Ke ys keyData, bool
dataGridViewWan tsInputKey) {
return !dataGridViewWa ntsInputKey;
}

public bool EditingControlV alueChanged {

get {
return valueChanged;
}
set {
valueChanged = value;
}
}
}
}
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1390
by: Scott Nonnenberg [MSFT] | last post by:
LINQ and C# 3.0 "Couldn't attend PDC but still want to talk to the C# team? This chat is your chance! Join the C# team to discuss the .NET Language Integrated Query Framework (LINQ) and newly announced C# 3.0 features like extension methods, lambda expressions, type inference, and anonymous types. You've read some of the documentation, maybe played around with the preview - now talk to members of the team!" Now this is some exciting...
0
1403
by: Marshal | last post by:
I've just had a chance to review LINQ, DLinq, and XLinq, (which I only heard about last week after the PDC). The various LINQs actually seem to live up to expectations - Using query semantics to retrieve a subset of data from arbitrary data-architectures (though I hope the sub-LINQ distinctions can be held coherent over time by the same governing architectureess). I summized pre-familiarily that we would accomplish LINQ support by...
7
1510
by: Chris | last post by:
I am a little confused. I have been reading about LINQ and it seems to imply LINQ is available in C# 3 but not in Visual Studio until the next release. I am a VB.net programmer but would still like to know how, if possible, do I go about using LINQ. When they say it won't be available until Visual Studio 2007 does that mean there won't be out of the box support for it (e.g. controls built for it) but you can use the classes if you have the...
18
4164
by: martin | last post by:
I'm just wondering... Will it be possible to use C# 3.0 and Linq to objects without having our users download the .NET Framework 2.0 client? I really would like to use the C# 3.0 features, but I don't know that it's worth making our customers download an extra 30 mb worth of framework to use. Thanks in advance, Martin
3
1747
by: David Veeneman | last post by:
I've been hearing a lot about LINQ in connection with Orcas, the next release of VS.NET. Micorosoft touts LINQ as the Next Big Breakthrough, but it looks to me like further muddying of application layering. I use an object-oriented development approach, and I don't see how LINQ can be integrated. Am I missing something? Are there any good articles on using LINQ within OO development? Or is that, like 'army intelligence', an oxymoron?...
4
2644
by: Arthur Dent | last post by:
Hello all, I am trying to figure out how to do something in LINQ, IF I can even do it in LINQ. Online samples and such for LINQ is still a bit on the sketchy side. Here is what I want to do... I have some string, whatever, that doesn't matter. I want to find all the characters which occur in the string more than once.
22
10371
by: paululvinius | last post by:
Hi! Testing som Linq-expressions and tried to measure performance and compare it to pre-Linq programming. The folloing two methods are functional equal but the non-Linq one is twice as fast. public List<ConferenceRoomOldWay(int minimumSeatingCapacity) {
5
3696
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
After having played around with LINQ and reading the literature on it I have not been successful in getting this to work so I am looking for some guidance: I want to find a hunk in a collection whose Start field is the largest in the collection but less than an arbitrary cutoff ("selectedRow"). The result of the query should just be that single object of type Hunk. (No two objects will have the same value for their Start fields.) Here is...
9
2503
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how about vs2008? is it different name space or framework for linq xml or linq sql? ( 2. do we need to have references to what linq's dlls. or namespaces? system core? 3. what name spaces are needed?
1
2769
by: hrishy | last post by:
Hi Well wouldn't it be a lot easier to query and join a xml source with a relational source with LINQ capabilites in Python. Hmm what am i missing here is there a site that takes all LINQ examples anddoes them using list comprehensions and makes them sound easy ? wasn't python supposed to make everything easy ? regards Hrishy
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.