Textbox binding default value for empty text - c#

I want a TextBox in Listview DataTemplate, which is bound to an int or double property takes 0 or 0.0 automatically when it is made empty i.e. when the whole text of the TextBox is deleted.

You could put in value converters which convert empty strings to 0/0.0 in ConvertBack. After the binding propagates back to the source the binding engine usually fetches the value again so you should end up with that as the Text.

Related

How to validate RadAutoCompleteBox in WPF?

I bound the ItemSource to RadAutoCompleteBox and the data is bound to the RadAutoCompleteBox. But I am struggling with validating the RadAutoCompleteBox.
If any invalid data (other than the ItemSource) is entered, it takes the selectedItem as null.
If I don't enter any value into the box, it also takes the selectedItem as null. I want to differentiate between them.
Any Help is appreciated.
You can use the RadAutoCompleteBox SearchText property. The SearchText does not return the AutoCompleteBox items, instead it returns the plain text you are entering in the AutoCompleteBox.

Compare display value with binding source value in DataGridView.CellFormatting?

I am now using a plain DataGridView component to display data with my own implementation of IBindingSource interface. The columns are binded to properties.
Now I am trying to compare the current "display value" in the grid (i.e. the actual previous value) and the latest value in the IBindingSource to determine background of the cell. Is it correct to compare the Value on the event with the value like Grid.Rows[rowIndex].Cells[columnIndex] ? Or is there any other way to do this?
Thanks in advance.

Invalid TextBox values gets reset by ComboBox's DataBinding

Situation: A C# Windows Forms application with a TextBox and a ComboBox. AutoValidate is set to EnableAllowFocusChange.
The TextBox represents and is shown as a percentage value e.g. "10 %" which is stored as an int. Both input controls are data bound, the TextBox with a parsing and formatting ConvertEventHandler as well as a Validating CancelEventHandler.
When entering an invalid input like "abc" and leaving the control: My Validation is performed and fails (e.Cancel = true, ErrorProvider ..). And my parsing fails (e.Value stays "abc").
Problem: When I now change the value of the ComboBox and leave it (lost focus/perform validation) or do a ValidateChildren, my format function is called with the last valid percentage value and the wrong input is lost.
Stacktrace: The problem is triggered by a ReportPropertyChanged of the ComboBox and leads to Binding.PushData, FormatObject and OnFormat -> Which calls my format function with the original value.
I want my TextBox to stay invalid and no magical reset. What can I do to prevent a value reset? Or what did I do wrong?
Thanks!

Can you have different item-text and item-values when using ObjectDataProvider?

When setting the source of an items control (ComboBox, for example), is it possible to have the SelectedValue property be different than the text that is displayed. I have a ComboBox that I want to display only the time portion of a DateTime, and have the value of each item be the DateTime object itself.
Thanks!
The combobox has an ItemStringFormat property that dictates how items are displayed in the dropdown and textbox parts of the control.
In the case of this example from the MSDN the SelectedValue will be a datetime objects, while the control itself will display the formatted strings.
See here for how to create the format string that will work for your problem. (Something like "HH:mm:ss" I'd guess)

How to retrieve a changed value of databound textbox within datagrid

ASP.NET 1.1 - I have a DataGrid on an ASPX page that is databound and displays a value within a textbox. The user is able to change this value, then click on a button where the code behind basically iterates through each DataGridItem in the grid, does a FindControl for the ID of the textbox then assigns the .Text value to a variable which is then used to update the database. The DataGrid is rebound with the new values.
The issue I'm having is that when assigning the .Text value to the variable, the value being retrieved is the original databound value and not the newly entered user value. Any ideas as to what may be causing this behaviour?
Code sample:
foreach(DataGridItem dgi in exGrid.Items)
{
TextBox Text1 = (TextBox)dgi.FindControl("TextID");
string exValue = Text1.Text; //This is retrieving the original bound value not the newly entered value
// do stuff with the new value
}
So the code sample is from your button click event?
Are you sure you are not rebinding your datasource on postback?
When are you attempting to retrieve the value from the TextBox? i.e. when is the code sample you provided being executed?
If you aren't already, you'll want to set up a handler method for the ItemCommand event of the DataGrid. You should be looking for the new TextBox value within that method. You should also make sure your DataGrid is not being re-databound on postback.
I would also highly recommend reading through Scott Mitchell's excellent article series on using the DataGrid control and all of it's functions:
https://web.archive.org/web/20210608183626/https://aspnet.4guysfromrolla.com/articles/040502-1.aspx

Categories

Resources