I want to allow only positive decimal value in DataGridTextColumn and if user enter .369 the system will show it 0.369 how it possible. I am new in WPF
            Asked
            
        
        
            Active
            
        
            Viewed 2,825 times
        
    2 Answers
0
            
            
        Try this
XAML:
   <Window x:Class="SandBox.Window4"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:SandBox"
            Title="Window4" Height="300" Width="300">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <DataGrid x:Name="dg" Grid.Row="0" >
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Quantity" Width="100">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Path=Qty, Mode=TwoWay}" x:Name="tb" TextChanged="tb_TextChanged">
                                </TextBox>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>
This is the CodeBehind:
   using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.Collections.ObjectModel;
    namespace SandBox
    {
        /// <summary>
        /// Interaction logic for Window4.xaml
        /// </summary>
        public partial class Window4 : Window
        {
            public ObservableCollection<ItemClass> Collection = new ObservableCollection<ItemClass>();
            public Window4()
            {
                InitializeComponent();
                //Collection.Add(new ItemClass("123"));
                //Collection.Add(new ItemClass("456"));
                Collection.Add(new ItemClass("123"));
                Collection.Add(new ItemClass("456"));
                dg.ItemsSource = Collection;
            }
            private void tb_TextChanged(object sender, TextChangedEventArgs e)
            {
                int result = 0;
                TextBox txt = sender as TextBox;
                if (txt != null)
                {
                    if (!int.TryParse(txt.Text, out result))
                    {
                        if(result >= 0)
                        {
                            txt.Text = txt.Text.Substring(0, txt.Text.Length - 1);
                            txt.CaretIndex = txt.Text.Length;
                        }
                    }
                }
            }
        }
        public class ItemClass
        {
            public string Qty { get; set; }
            public ItemClass(string qty)
            {
                Qty = qty;
            }
        }
    }
 
    
    
        Clinton Ward
        
- 2,441
- 1
- 22
- 26
0
            
            
        For this you might have to set appropriate StringFormat in TextColumn's Binding.
Try this...
   <Controls:DataGridTextColumn Header="Quantity"
                                Binding="{Binding Quantity, 
                                         StringFormat='{}{0:0.####}'}"/>
So now if user enter .897 and presses enter, the cell value will be shown as "0.897".
 
    
    
        WPF-it
        
- 19,625
- 8
- 55
- 71
- 
                    its not working there are some focus problem when I enter .36 it writes .63 can you tell me the solution – Andy Oct 16 '12 at 13:03
- 
                    Do you have `TextAlignment` or `FlowDirection` as right to left like its done for `Urdu` or `Arabic` languages? – WPF-it Oct 16 '12 at 13:38
- 
                    Nadeem, This is working for me. I am pretty sure your language settings are causing text to misalign. Please change your Region and languages (Control Panel -> Regional and Language Options) to something like US or UK and then relauch your application. Dont forget to set **both settings**, on Region tab and Languages (Click **Details button**) Tab. – WPF-it Oct 18 '12 at 08:00
- 
                    
