Paste Excel data into WPF data Grid (or related object) - c#

I'm trying to figure out a way to allow a user to paste data from the clipboard (specifically excel) into a WPF program. I figured I would need to use a DataGrid, but I can't seem to get it to go (and I was hoping to create an event handler to trigger after a user pastes something in). The data the user will paste in will be a single column with a fixed number of rows (ie 15 x 1), and will just be numbers.
Any thoughts? Thanks a lot!

Ctrl-c in Excel puts a CSV text (actually tab separated values into clipboard).
How exactly you implement the paste/import operation depends on the context. One option would be to handle paste action with an event handler on the grid and trigger the corresponding action in your view model. Another way would be to define a button or some other element so user could explicitly trigger paste (data import) operation.
However that action in the view model would take the data in the clipboard and parse it to create one instance of appropriate object for each row and add it to the ObservableCollection binded to the grid ItemsSource property.

Related

How to distinguish modified item in binding list?

I am developing a windows application using C# and .net 4.5 under visual studio 2013 IDE.
In my application when the user attempt to enter a new data the program creates an object of appropriate class let's call the class DataClass and the to be objectOfDataClass.
The objectOfDataClass is added to a BindingList<DataClass> let it be bindingListOfDataClass which is bounded to a DataGridView as a data viewer.
The user enters data in a text boxes then he/she press a button to add text boxes value as a list item to bindingListOfDataClass and then he/she can view all data records on the DataGridView.
Then the user has a choice to click save button to save the data to a file or database.
All of these functions are working very well without any problem. But I want to add another function to mark or distinguish the new data entry or the modified cells on the datagridview and the bindingList by changing the style format of that cell (i.e: changing the background color or font style ..etc) before the user click the save button so he/she is notified to confirm any changes before saving the new entries.
The comparison should be between the saved data (the data on the file or database) and the data shown in the datgridveiw.
Now I am looking for a best solution which must achieves higher performance and preserve memory. so any suggestion please?
Exactly here you need to distinguish newly added items and already saved items
from the list So you can easily use flag for this....
When you click on Add Button you can false your flag & then true..Before items saved you can change color or something else where flag false..

Can't refresh datagrid

I have a DataGrid, which loads the data on start up. There are several buttons on which the user can click. Each button updates the same column. The problem is that when the new value of that column is saved, the old value is still shown in the data grid. It must be refreshed. I have tried several ways to do it, like: t_KlantenDataGrid.Items.Refresh() and CollectionViewSource.GetDefaultView(t_KlantenDataGrid.ItemsSource).Refresh(). None of them works.
The code which loads the data:
OV.AOVDataSet aOVDataSet = ((AOV.AOVDataSet)(this.FindResource("aOVDataSet")));
// Load data into the table t_Klanten. You can modify this code as needed.
AOV.AOVDataSetTableAdapters.t_KlantenTableAdapter aOVDataSett_KlantenTableAdapter = new AOV.AOVDataSetTableAdapters.t_KlantenTableAdapter();
aOVDataSett_KlantenTableAdapter.Fill(aOVDataSet.t_Klanten);
t_KlantenViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("t_KlantenViewSource")));
t_KlantenViewSource.View.MoveCurrentToFirst();
I use Entity Framework. Why doesn't those two solutions work for me. Are there any other solutions to refresh the DataGrid?
If you see the old value with that type of data binding, it means you don't update the initial source, but only the temporary one (in your case t_KlantenViewSource). Calling Refresh method on the data added by FindResource cause app to reload it from pre-compiled resource, which won't change in this case.
In other words, the problem is you use pre-compiled resource
On the same time, you fill the dataset with actual data, but your dataset is in memory, view source - inside application file.
You can try:
binding ItemsSource of the entire datagrid or separate column to the TableAdapter dataset
change resource compiling behaviour to Content, thus the app will reload it from external file each time you call Refresh method.
ADDITION:
You can find a solution for your case, just to save time. Check paragraph "performing updates" Other way is to provide NotifyPropertyChanged Event for each bound parameter, but I can't say it is better in this case, assuming you update columns via unique buttons.
To be short, you need a method like this on your button clicked / property changed:
aOVDataSett_KlantenTableAdapter.Update(aOVDataSet.t_Klanten)
after button click first set datagrid datasource to null then assign the datas
datagrid.datasource=null
Thank you all for your answers. As Jasmine suggested, I reloaded the dataset and rebounded the datagrid. Maybe it's not the best way to do it but it was the only solution I had then.

Is there a quick way of adding an event to many controls at once in WPF?

I have 400+ textboxes on a page. The page is meant to be an exact replica of a paper form. The user is going to edit all of the fields and then save the data to the DB. The data is being pulled down into a DataTable from a SQL DB.
I'm planning on saving the data via the same DataTable or just via a bulk update. Not 100% on that. But, I need to get access to that data. And maybe I'm not doing this next best part the best and if I'm not, I'd appreciate it if I was informed of a better way.
When the DataTable gets the data, I assign each field into the appropriate control. Below is an example of how the data is being added.
foreach (DataRow CDR in ClaimDataTable.Rows){
if (CDR["BoxNumber"].ToString() == "1.1")
this.Box1_1.Text = CDR["DataValue"].ToString();
if (CDR["BoxNumber"].ToString() == "1.2")
this.Box1_2.Text = CDR["DataValue"].ToString();
if (CDR["BoxNumber"].ToString() == "1.3")
this.Box1_3.Text = CDR["DataValue"].ToString();
I wrote some code to automatically create that code. So I didn't manually write all 400+ lines.
What I was thinking, was that I could add a LostFocus event to each TextBox. Then when the control loses focus, I would create a class with a box name and the box value. Add that to a list and when they're ready to save, just loop through the list and do the bulk update with the BoxNumber and the box data.
Would that be feasible? Or is there a better method?
Thanks!
Look into event routing.
You should not create and access individual text-boxes but let the framework create them via datatemplating a collection, your above code is ze horror.
When the DataTable gets the data, I assign each field into the
appropriate control.
WPF is data binding and one does not generally load controls as you mentioned. Why is the code not binding to each control to a property which adheres to INotifyPropertyChanged?
Have the page's DataContext (DC) point to an instantiated class which adheres to InotifyPropertyChanged interface. The setting of the DC in that fashion will allow all controls on the page to use its datacontext by default.
In the class to be instantiated for the DC create a member property which calls PropertyChanged when the value is set using the INotifyPropertyChanged system.
Bind each TextBox Text property to that property on the class.
Then each control which needs that value will display it automatically after it has been set such as in this example.
<TextBlock Name="tbHeader"
Text="{Binding CDRData}" />
Then one only has to write the value to the property named CDRData once and all textboxes bound get the value.

Navigation grid inside of a FormView

We have a list of elements we use to drive a form view. Typically the resultset is between 5-15 records. Right now in the form view we have the typical first/prior/next/last for navigation. The problem is that sometimes the user needs to go directly to a specific record to edit it. The records here are job tasks that have some pretty short descriptions.
The idea was proposed to put a gridview inside of the formview that lists all of the records for that form, and a user can just select view or edit on that record and it will navigate directly to that record and put it into the proper mode. We could put this outside the form view as well, that doesn't really matter.
The question is, regardless of the dirving force, how to I tell the formview to go to record X driven from something like an external grid.
I know that the formview has the DataKeyNames field but is there a way to say "Go to record who's PK is 17" given that it's in the current dataset for the formview?
If so, does anyone have any sample C# code for it? I know we could just populate the existing formview with a single record, but we also want to keep the normal navigation buttons in place as well, in the event as well (sometimes there's cases where there's hundreds of job tasks, in which case we'd supress the grid view -- sounds wrong but there's more to the business case).
This short solution was to embed the grid in the FormView ItemTemplate and reference the same data source for both.
To made the row editable, you need a simple callback for RowDataBound on the grid in which you will retrive the link button (or whatever control you're using to trigger the edit) and then set the command argument to the rowindex. After that, you need to have the callback for the link button (or trigger) that wil retrieve the command argument (again the row index) and then set the FormView PageIndex to that value, and then set the ChangeMode for the FormView to FormViewMode.Edit.
So to recap, for read only view it's a grid with all of the items, but when handle the edit or insert, you get the traditional form view.

creating a custom user interface in WPF

I have a SQL database holding a number of numeric and text values that get updated regularly. The exact number/type/names of these data points can change depending on the source of the database writes.
I would like to create a user interface editor, where the user can add database points to the UI and arrange them and format them as they want. If a new point is added to the database they can right click on the UI and say "add this point" and choose from a list of database points.
I'm looking for some pointers on where to start on creating this editor application, could something clever be done using XAML to dynamically create std WPF controls at runtime?
Doug,
Apologies, by database points I simply mean rows in the database that represent an item to be displayed in the ui.
Ray / Sushant,
Thanks for taking the time to answer, I'll have a go at both these approaches.
Si
Here is an easy way to do this:
Create a DataPoint class, including "Name", "Type" and "Value" fields
Create a DataPointView UserControl that exposes a Name property and a read-only DataPoint property. When the Name property is set, load the DataPoint from the database. Optionally use a timer to periodically reload the DataPoint (or subscribe to update notifications from your database).
Create a UIEditor class deriving from Window that exposes a CurrentForm property that is initially a blank Canvas
Add handlers for ApplicationCommands.Open, ApplicationCommands.Save, etc to use XamlParser and XamlWriter to load/save the layout from/to a file on disk (or a database)
In your UIEditor XAML, include a ContentPresenter bound to the CurrentForm property to hold the UI being edited. Also any other controls desired (Save button, Open button, palette, etc).
In your DataPointView's XAML, display the data point's name and value.
In your UIEditor class subscribe to mouse preview events OnPreviewLeftButtonDown, etc. Whenever a mouse move event follows a mouse down event on a DataPointView, capture the mouse and begin adjusting the DataPointView's Left and Top coordintates - this allows the user to drag the DataPointView around the Canvas.
In your DataPointView's XAML, include a ContextMenu whose ItemsSource is bound to "{Binding AvailablePoints, RelativeSource={RelativeSource FindAncestor,my:UIEditor,1}}", and make sure the AvailablePoints property of your UIEditor class returns a list of MenuItems with names of available data points and appropriate command an command parameter.
In your handler for the command bound in the context menu, add a new DataPointView to your CurrentForm Canvas and set its Name from the name given in the CommandParameter
Set Focusable=true on the DataPointView objects, and handle ApplicationCommands.Delete by deleting the focused DataPointView.
With this code written:
You can allow your users to edit your UI by showing a UIEditor window.
You can display your UI without the editing features by simply loading it from disk using Application.LoadComponent and displaying it in a window.
use WPF DataGrid available as WPF ToolKit in .NET 3.5 or it is standard for .NET 4.0. It has the following features:
Highly customizable Data columns
Use Editable Rows that can be persisted
Columns can be added/deleted/rearranged on the fly.
Can directly load column names from database
and a lot more.
I think that would be perfect.
Please mark the answer if it seems helpful. Thanks.

Categories

Resources