Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I got stuck with validating Textbox which is in DataGrid column.

Textbox should accept only numeric values.

XAML code:
HTML
<UserControl x:Class="TestGrid.GridControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
           >

    <StackPanel >
        <DataGrid GridLinesVisibility="Vertical" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"
                                      MinHeight="100" MaxHeight="300" Width="Auto" Name="McDataGrid" Grid.Row="1" Grid.Column="2" AutoGenerateColumns="False" CanUserAddRows="False"
                              CanUserDeleteRows="False" ItemsSource="{Binding Path=TestData}" DataContext="{Binding}"
                  >
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Binding="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False">
                    <DataGridCheckBoxColumn.Header>
                        <CheckBox Name="selectallchkbox3" IsChecked="{Binding DataContext.SelectAll1, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}},
                                            UpdateSourceTrigger=PropertyChanged }"/>

                    </DataGridCheckBoxColumn.Header>
                </DataGridCheckBoxColumn>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Name,Mode=OneWay}" IsReadOnly="True" Width="250"/>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=InstruQuantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
</UserControl>


And Model view code:
C#
namespace ViewModel
{
    public class Authors: ViewModel.ViewProperty
    {
        public Authors()
        {
            LoadCollectionData();
        }

        private DataTable authors = new DataTable();

        public DataTable TestData
        {
            get
            {
                return authors;
            }
        }

        public void LoadCollectionData()
        {
            DataColumn col = new DataColumn();
            col.ColumnName = "Name";
            col.DataType = typeof(string);
            authors.Columns.Add(col);

            DataColumn col2 = new DataColumn();
            col2.ColumnName = "InstruQuantity";
            col2.DataType = typeof(string);
            authors.Columns.Add(col2);

            DataColumn colcheck = new DataColumn("IsSelected", typeof(bool));
            authors.Columns.Add(colcheck);

            DataRow row1 = authors.NewRow();
            row1[0] = "Ganga";
            row1[1] = "10";
            authors.Rows.Add(row1);

            DataRow row2 = authors.NewRow();
            row2[0] = "Bhagya";
            row2[1] = "20";
            authors.Rows.Add(row2);
        }

        private bool _SelectAll;
        public bool SelectAll1
        {
            get { return _SelectAll; }
            set {
                    _SelectAll = value;
                    OnPropertyChanged("SelectAll1");
                    SelectAll();
                }
        }

        public void SelectAll()
        {
            if (SelectAll1 == true)
            {
                foreach (DataRow row in authors.Rows)
                {
                    row["IsSelected"] = true;
                }
            }
            else
            {
                foreach (DataRow row in authors.Rows)
                {
                    row["IsSelected"] = false;
                }
            }
        }
    }
}


Please anyone can help me, how to validate Quantity Textbox??
Posted
Updated 30-Nov-11 18:38pm
v4
Comments
RaisKazi 21-Nov-11 3:58am    
Do you mean MVVM? Your Title seems to be having typo error.
Ganga Patangi 1-Dec-11 0:34am    
yeh its MVVM..

1 solution

You may have to create your custom control like NumericTextBox and use it instead of text box. Another way handle the preview text changed event and accept numeric chars alone.

Refer the following thread.

http://stackoverflow.com/questions/1268552/how-do-i-get-a-textbox-to-only-accept-numeric-input-in-wpf[^]

Hope it helps!
 
Share this answer
 
Comments
Ganga Patangi 1-Dec-11 0:43am    
Thanks VallarasuS.. I got solution using this link http://stackoverflow.com/questions/1346707/validation-in-textbox-in-wpf

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900