Showing TimeStamp in dataGridView - winforms

I've tried implementing the first suggestion from C# Winforms DataGridView Time Column, i.e. showing a DateTime picker in a DataGridView.
However, when I launch the app and select a date from the dtPicker and then select the next row, the value disappears in the first cell where I had selected a date.
What changes are needed for the cell to keep displaying its value while creating additional rows in the DataGridView?

I found another solution, which solved the issue. It's much cleaner and easier to implement. The solution was in an article titled:
Embedding Calendar (DateTimePicker) Control Into DataGridView Cell
https://www.c-sharpcorner.com/UploadFile/0f68f2/embedding-calendar-datetimepicker-control-into-datagridvie586/
The code suggestions in the above article work well for every row in the DGV.

Related

WPF Data gird row selection change event from selection change event

I'm looking for a workaround to an issue I've noticed in a project that I'm working on. I have a datagrid bound to a list of a class I've created. Whenever a user types in a stock symbol in this datagrid I have the roweditending event set to pull in some information to populate the other fields in the datagrid.
This generally works fine, however I've noticed that the event doesn't fire in some instances. I'm going to try to explain this the best I can... upon data entry if I enter information into a cell and then click another row on the datagrid - focus doesn't shift to the new row but shifts to the row containing the cell I was just entering data into from that cell, then if I select another row or input element the row edit ending event doesn't appear to fire and as such my code to populate the rest of the row doesn't execute.
Everything else is fine, and it's a pretty subtle issue, only one path of input to make it happen, but I was wondering if anyone had run into this or found a work-around?
I've been looking at the selection change event, but I need to be able to grab the row that was selected. In the row edit ending event I use
Dim NewTrade As Trade = e.Row.DataContext
to get the instance of my class that the row represents and to add the additional fields.
I'm not sure I see a way to get the row and it's datacontext for the row I just shifted from using the SelectionChangedEventArgs.
Any ideas for a workaround?

LightSwitch refresh a ComboBox property (list) from an other control

On a SearchScreen, I have some properties to filter my table results.
ComboBox for the month.
ComboBox for the year.
ComboBox for a customer (based on a query with two parameters, DateBegin, DateEnd)
The third ComboBox need to be filtered to only show customers that are 'active' in the selected period from Month and Year Comboboxes.
I use two properties (Date) named 'prpDateBeginSelected' and 'prpDateEndSelected', linked to my query herself linked to the ComboBox of the customers.
I use the _Changed events on my Month and my Year to assignate the dates to the properties.
When I make my first selection after the screen was loaded, It work and the customer combobox is filtered in the good period.
But after, when I change the month or the Year, the customers comboBox don't reload.
I've tried to do a this.qCustomersByPeriod.Refresh(); in my code but no changes.
Thanks for help.
PS : If needed, I can make you a sample to show you the idea.
EDIT : A sample to help you understand me. LS_SearchFiltered.zip (58.6 Mo)
It's good that you provided your solution, because I got confused just from the explanation from the post. Indeed the problem with your solution was data binding. You've mistaken the data binding for QueryDataByCustomerAndPeriod.CustomerId it should be QueryCustomersByDate.SelectedItem.Id . Also instead of using generic prpCustomer, you need to use the selected item of QueryCustomersByDate and get the relevant Id. To clarify everything see the screenshots below. Hope this solves your problem.
A note to remember - check carefully your data biding in the View-Model - what links to what.
Problem in QueryDataByCustomerAndPeriod.CustomerId
Correct the binding in QueryDataByCustomerAndPeriod.CustomerId

Creating an Empty DataGrid

I was wondering what the best way was to create an empty datagrid.
For example after you have hit new in excel, You have a grid with empty rows and columns.
I am using c# with WPF and .net 4.0.
Thank you.
As the comments have suggested, a datagrid is not a spreadsheet, but a method to display / edit existing data. That said if you want something similar, feel free to populate a collection with default / 'empty' objects and bind that to your grid. It just means that after working with the data, you will have to define a method to capture only the edited rows. This still means that the column-bound properties of your class need to be known ahead of time.
A DataGrid is used to display a collection. If you want to create an individual row DataGrid is not really the right tool. You could but a single empty row in the DataGrid using a collection of only on row. There is a lot of guidance on Master Detail on MSDN. If you don't know how many columns at design time you could used a DataGrid to turn the row vertical with column 1 as name and column as value so now you have one record but with a collection of fields.

Sorting DataTable with WPF DataGrid?

Okay, this is driving me nuts. I've spent hours trying to figure out what should be a simple solution, but I'm having no luck.
I have a [WPF Toolkit] DataGrid on an XAML page that has a DataTable as its ItemsSource. I also have a button on my page that gets the DataGrid's SelectedIndex (selected row) and uses it as a variable in a function that reads the bound DataTable's row at that index and returns a value. Everything works fine until I click a column header to sort it. It sorts the DataGrid but does not sort the DataTable with it, so my SelectedIndex has changed but the index of the DataTable has not, thus it returns the wrong value.
I've looked for Column Header click events - no luck; I tried to get the header of the column by which the grid is currently sorted - nada; I tried to use a "Click" EventSetter on the DataGridTextColumn template - not supported.
I'm completely at a loss. If WPF is supposed to be an improvement over Windows Forms, why has some of the simple functionality been removed? (It's also dumb that you have to bind data just to add rows, just saying.) I can use a Windows Forms DataGrid and won't have any trouble, but then I can't style it.
Maybe I'm not performing the check properly or something...? Below is my retrieval/output code. Anyone have any ideas??? Your help will be greatly appreciated!
DataRow selectedRow = my_data.Tables[0].Rows[my_grid.SelectedIndex];
MessageBox.Show(selectedRow["ItemName"]);
The sorting is applied to the DefaultView of the datatable.So it will not be applied to the Datatable directly.To access the sorted table use
DataTable.DefaultView.ToTable()

Totals Row in a DataGridView

I am developing a winform application application. I wanted to show sum of columns in last row of each column. This row must always be visible.
At moment I am thinking about adding another datagridview just beneath my datagridview with records, and would show the sum in that bottom datagridview.
If there is a better way to achieve this task?
No, need of adding another datagridview
Solution 1: Please refer to this solution
Solution 2: If the above link is not exactly what you want then,
You can try to manually add last, summary, row in which you can display information that you need. For example, you can try to do the following:
Read data from database and fill System.Data.DataTable
Add one column to the newly created DataTable – that column might be set
to true for the last, summary, row
Programmatically add one extra row that contains suitable summary data
Do the data binding to DataGridView control
Using appropriate event, bold or otherwise graphically distinct summary
row (row that have extra column value
set to true)
You can do it in the same way as you suggest, like placing a datagridview for displaying the sum. you can also handle the Horizontal Scroll with this, if there are more columns.
Another method is there in this link
Another way you can Add Rows to your datasource itself to display the sum.
Even if this question is quite old-ish I'd like to propose an extension to Niraj Doshis answer. His answer holds true for a data bound DataGridView. I recently had the problem to calculate the summary in a user-editable DataGridView, whose solution differs in the details. Anyway It's quite straight-forward too. I am writing my functions on a higher abstraction level, which outlines the workflow, but elides the implementation details.
First of all you'll have to initialize the DataGridView, see
private void InitializeDataGridView()
{
SetColumnTypes();
AddEmptyRow();
AddSummaryRow();
}
I set DataGridView.AllowUsersToAddRows to false for the new row would be located beneath the summary row. Hence I am adding an empty row, which the user may fill with his data. The summary row is set to ReadOnly since we do not want our user to edit it. Whenever a CellEndEdit is raised I am updating the DataGridView with the following method
private void UpdateDataGridView()
{
RemoveSummaryRow();
RemoveEmptyRows();
UpdateRowNumbers();
AddEmptyRow();
AddSummaryRow();
}
First I remove the summary and all empty rows (you'll have to take care. If you are trying to delete the row you just edited an exception will be thrown. I've not yet figured out how to do this, but I just made up the solution. I'll amend when I came up with the solution.) Afterwards I'm setting a running number in each of the rows. This is not really required, but a detail of my implementation. At the end I add an empty row again which the user may use to add further data and then calculate and add the summary row.
As I said before this is not yet a ready-made solution, but rather I concept which works but with some quirks and bugs.

Resources