WPF dataset/database row edit in new window - c#

I know that there is a lot of questions about databinding in WPF C# but I'm already out of my mind.
There is a simple way to do a databinding to a dataGrid in wpf, it is a built-in feature.
But I want to click on the particular row, then click edit button and edit this row and some additional info in new popup window, not into datagrid itself.
Selecting particular row is a other story but now the main problem for me is to load data from this particular row in database to textboxes in new window.
I can do this by
DataTable obj = contactsTableAdapter.GetOne(id);
DataRow r = obj.Rows[0];
String sur = r[5].ToString();
but this is wrong and one way solution.
I want to use a proper databinding between this selected row and my textboxes, where the user can modify data and click button to call Update method to propagate changes to database.
I'm not sure how to load that particular selected row, to what type of object, and how to bind it to textbox, and how to do this in a clean way, without doing some hacks.
I'm pretty sure that c# provides some obvious solution but I have lost my way somewhere.
I'm waiting for some help impatiently, and thanks for any solution!

If you don't want to use MVVM here is a simple solution.
Your button click method should have access to datagrid
MyItemType item = (MyItemType)myDataGrid.SelectedItem;
Pass the item to your new form\window\whaterver.
NOTE: You might want to null check the selecteditem. You can put the edit buttons in your DataGrid Row.

Related

Get DataGridCell Row and Column of marked cells and Building a button over that

I have a program in MVVM. In this program i have a view with a DataGrid. In this datagrid i have to show something like this:
The user mark some cells and after MouseUp, should be created a button over this selected cells. The first Problem here is, how to get the Column and the Row. It is in the SelectedCellsChanged-Event Argument. With help from the WPF MVVM Light i can give my ViewModel the Event and the Arg, but my workmate had already problems with visual elements in a ViewModel(He was get a error in the visual tree). And i know that a visual element should not be in a VM. Can anyone here give me a way to get this Column and Row-Spawn?
My second problem that i can't imagine me how to create the buttons in the correct cell. If i think right is it possible to build a new VM for every Button(with Property StartRowSpawn, EndRowSpawn, StartColumnSpawn, EndColumnSpawn) or for each cell?
Can you give me tip, how to solve that?
I thinked about a grid too. Get position while MouseDown and MouseUp and then find the Grid.Column and Grid.Row for that.
Edit
Okay i done my first problem with Interfaces, but i already have my second problem. Can anyone help me there?
I would rethink why you need to do this(or maybe you can explain more why to need buttons like this). What I do when I need to change the design of data in a data grid is use an alternate datatemplate or the details datatemplate for the datagrid row to chnage the format of the infomration, then you can use the same view model. You can bind the column spans to properties in your viewmodel but it gets difficult if you want the button quantities to be dynamic.

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.

Creating a table and bind it to an array

I'm pretty new at this programming thing...and I need to make a program which lists some customers and they need to be clickable so I can press a button to delete the selected item.
How do I create a table which will show all my objects from an array, and would make it easy to mark one item and click on a delete button?
I'd suggest using a DataGridView, which you can bind to your data source (you might have to store the information in an ObservableCollection if it needs to be dynamic). Then just have your button delete the selected row (make sure to disable the button if no rows are selected).
Like an alternative to #Nick answer I can add ListView control in Details view mode. It's much lightweight then DataGrid, but also provides less functoonality. It's a metter of choice. If you don't need complicated stuff, but just: select row, delete row, may be search row, ListView could be pretty suitable option. But, I repeat, keep atention on your app requierements.
Regards.

How to make popup window auto populate with contents in cell

I have a data grid displaying results from a sql database. What I want to do is make it so that when I double click on a cell in the datagrid, a new window appears where that cell can be edited. I also would like to have all the info for that cell to be autogenerated into editable fields when the cell is double clicked. Any help or links to tutorials is appreciated. Thanks.
Try this DataGridView.CellDoubleClick Event
Did you consider using data binding? Simply put, you can bind your items list to DataGrid, and current item (eg. selected/double clicked) to your edit form's controls (TextBox, Label and whatever else you might have there).
Here are good starting points for windows forms: #1, #2. They cover problem you're facing. You can find data binding tutorials for ASP/WPF easily too, but especially first link covers entire concept pretty well.

adding a control to tabs generated dynamically in tabcontrol in C# .net

I am new to C# .NET.
Can some one help me out with the below problem:
I have a TabControl in my WindowsForm application, where the tab pages are generated dynamically. The content to be displayed on each tab would be fetched from my database. I need some kind of control (which can display the fetched data) that I can add on each tab page (which would be same for all tabs) such that I can associate some kind of event, say click, on that added control.
Can anyone tell me how to do this programmatically & write the click event for all the controls added?
Please refer the below link! You will get more detail in this regard.
Creating a tab control with a dynamic number of tabs in Visual Studio C#
I'm not sure I completely understand your problem but my initial thoughts are that you could dynamically create a datagrid or something similar for each tab that you are dynmically creating. You could then bind the datasource for the grid and then add the grid as a control to your tabpage.
Something like...
DataGridView gv = new DataGridView();
gv.DataSource = //whatever your source is
this.tabPage1.Controls.Add(gv);
You would then have all the events associated with the grid to work with.
I'm thinking data binding is going to be your best bet for displaying this information. You can create a list of objects and use a DataTemplate to format the data. You can apply the DataTemplate to a quite a few objects. I generally use the ItemsControl and ListBox
http://msdn.microsoft.com/en-us/library/ms750612.aspx
good luck

Categories

Resources