I'm trying to add some extra logging to my C# winforms application. I have a few data bound forms that all the database stuff is managed by a binding source and some typed datasets/adapters.
With this setup, it's kind of difficult to tell when something is changed, I'd have to manage each field and keep it's previous value. Is there a way I can hook into the dataset and tell when something is changed? I know datarow's have a RowState enumeration, would that be a good place to start? I looked into the binding source's DataMemberChanged event but it never fired so...
To get the original value of an updated Data value you can do this:
<DataTableRow>[<DataColumn>, DataRowVersion.Original]
To know what has changed look at the DataSet.GetChanges method. The example shows how to get the changes and go through them. I also have an old example here that uses a DataTable and shows how to do a comparison after a merge. It inspects the RowState and shows the changed values etc. It's near the bottom of the page and it's in VB since the OP was using that, not C#. I'm headed out now so I can't provide an equivalent translation but it should be pretty straightforward to glean some useful techniques from.
You can use the RowChanged event, the RowChanging event, or any of the other events raised by a DataTable.
Related
I'm using the ObjectListView control from here.
I'd like to detect when a user right clicks the headers and hides or shows a column. Basically a ColumnVisibilityChanged event. The reason I want to detect this is to save the visible columns between sessions.
The ColumnWidthChanged event fires when this occurs (not on the column that has been removed), so I could iterate through AllColumns and check the value of IsVisible. However, that seems hacky and I'd like to avoid it. Also, that would get run several times when it didn't need to.
Anyone know of a better way of detecting a column being hidden or shown?
Well I found a solution that wasn't quite what I was looking for, but possibly better.
ObjectListView has SaveState() and RestoreState() methods as described here.
I have a DetailsView control which gets data from a IEnumerable custom class.
But I can't get updated values from the control so I can process to update them manually in the database. How can I do that?
Regads,
Gustavo
I suspect it can't work with an IEnumerable... The DetailsView probably needs to be able to access the data source by index (or at least a way to access a specific item of the data source), so I think the source has to implement IList. But I can't find anything about it in the documentation, so that's just a guess...
It seems like some added clarification might be useful here, but some notes:
the DetailsView can work with an IEnumerable source (assuming you're setting DataSource to the enumerable), but it'll bind to the first item fetched from the source.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.aspx#binding_to_data
since it sounds like you're not binding to a data source, you'll need to hook up to an event (ItemUpdated in this case, I'd think) and then do the database update yourself
IMHO, if you can switch to binding against a 'real' data source life will be easier since you can let DetailsView do the heavy lifting for you, but if not, hopefully ItemUpdated will work for you :)
I'm hopefully missing something extremely obvious here, but for the life of me I can't figure this out.
I have an AutoCompleteBox control that is retrieving results by way of an asynchronous call, although I can't find a reliable way in which to know when a user has selected an option from the list of returned values. I've tried hooking into the SelectionChanged event, but that fires on every movement within the autocomplete's drop down list, when what I actually want to know is when a user has definitively said "Hey, that's the item I want!" by either clicking it, or selecting it with the return key.
Any advice would be greatly appreciated as this is starting to drive me a tad crazy now. =)
Thanks
P.S. The SelectionChanged event arguments provide AddedItems and RemovedItems, but don't give any direct indication of the type of selection I'm looking for.
There isn't a specific event that indicates that the used has specifically plumped for an option rather then merely browsing.
For you scenario the closest you can get is DropDownClosed or even LostFocus events then access the SelectedItem property.
If you are using the Telerik AutoCompleteBox, perhaps the "OnEntryAdded" event is what you are looking for. I see this question was asked over 4 years ago so perhaps this event didn't exist back then. I just updated to the latest update of Telerik controls (2014 Q2). Here's a quick example. I removed the properties that we're not discussing here to clean it up but, obviously, those will need to be added back in unless you're setting them in code (e.g. DataSourceID).
Source Page:
<telerik:RadAutoCompleteBox ID="RadAutoCompleteBox1" runat="server" OnEntryAdded="RadAutoCompleteBox1_EntryAdded" >
</telerik:RadAutoCompleteBox>
Test Code (in VB):
Protected Sub RadAutoCompleteBox1_EntryAdded(sender As Object, e As AutoCompleteEntryEventArgs)
MsgBox(e.Entry.Text) 'This is just to show you the DataValueField in your dev
MsgBox(e.Entry.Value) 'This is just to show you the DataTextField in your dev
racbCustomer.Entries.Clear() 'This will wipe out the selection(s)
End Sub
Im trying to emulate a 'find as you type' function like that of the address bar ("awesome bar") in FireFox. I want a suggestion box to appear below a textbox, and the suggestion box contains strings that contain what is in the textbox. I looked at the autocomplete feature of a normal WinForms textbox, but it seems to only search the beginning of the strings.
Has anyone here built or have experience with implementing something like this?
edit:
Some clarification- It is a WinForms project.
It needs to search inside a string, not just the beginning (which is what a normal textbox does if i recall correctly). And the suggestions should be displayed in a popup like a textbox autocomplete.
You need to handle the TextChanged event for your text entry field, and when the text changes, start a new thread running that will apply the new search. If the text changes before you get your results back, just kill the thread. If the thread returns results in time, display them.
You can get slightly more advanced (e.g. wait for a short time after the text changes so that the user can type a word without you triggering off loads of useless threads) but essentially that's it.
There was a discussion earlier on this topic where the author concluded that you are better off doing the whole thing yourself.
How can I dynamically change auto complete entries in a C# combobox or textbox?
I did something vaguely similar, but more like the iTunesĀ® search box than the Awesomebar. My control used the textbox to actively filter a grid; so it wasn't for autocompletion.
...but... basically I had a DataView of all eligible items, whenever the TextBox's Text changed I'd update the Filter to hide all non-matching items. It worked well and might suit your needs for filtering the data--but not sure how to go about using it as an AutoComplete source for the textbox.
I have done such a thing for an app of mine not too much time ago.
What I did is make my search function in a new thread, so every time I typed a new letter, it called the search function in another thread, so I could keep on typing.
I can post some code if you need, but this should be enough to get you started. :)
Hemmed and hawed about deleting this after I noticed the OP edit mentioned winforms, but I think it'll be useful to anyone who comes here looking for the same but for asp.net apps.
Just because nobody has mentioned it yet, for a webforms app you absolutely want to do this with ajax (.net controls or pure JS, your choice). The feature is often called "autocomplete" and the one thing you don't want it to be breaking the seamlessness by making server round trips at the page level.
I suggest you look at this and this.
I've used Search As You Type in C# and How do I make a Textbox Postback on KeyUp?
Basically you use the keyup action to call a postback thats attached to the trigger to the update panel. then you do your update in the textbox_changed event with the dataview or whatever your backend looks like.
I have been binding textboxes in a winform with C# to a dataset. Whenever the data doesn't validate with what the database except it silently forces the focus to remain on the textbox. How can I catch the validation error and tell the user about it (and free the focus)? The BindingSource event OnDataError isn't fired.
I had a similar problem once. The focus remained in the textbox which was binded to some numeric database field when the user modified text in a textbox and then deleted it so the text property was an empty string. I solved it with something like:
textbox.DataBindings["Text"].NullValue = "";
It solved the problem for empty inputs. I don't know if it would be of any use in your case, but I'd be also interested in more general solution.
Here's also some related question on SO:
Data-bound TextBox: can't exit
Never rely on just what "Visual studio has done for me" if you don't fully understand what it's doing. I would strongly urge you to take the time and figure out how to do what it is you want to do all by yourself (meaning without designer generated code). To get you started, there are some events on the TextBox that can help you out. Start here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validated.aspx
Specifically the validating and validated events should be what you're looking for.