none
DataGrid ReadOnly

    General discussion

  • I have a DataGrid that bindes to a List I add new item to List  DataGride shows some cells But I Can not edit where it is not ReadOnly

    Friday, April 27, 2012 3:18 PM

All replies

  • Mohammad,

    to be editable the data object that is displayed should follow some rules:

    If your data object has a property with no property setter,
    the DataGrid is intelligent enough to notice this detail and disable column editing
    just as if you had set DataGridColumn.IsReadOnly to true. Similarly, if your
    property isn’t a simple text, numeric, or date type, the DataGrid makes it readonly
    (although you can remedy this situation by switching to the
    DataGridTemplateColumn).

    Patrick

    Saturday, April 28, 2012 1:05 AM
  • Dear Patrick

    My DataGrids IsReadOnly Property is set to False and I have 5 columns Both simple data and template columns are not editable

    thank you very much

    Mohammad

    Saturday, April 28, 2012 10:53 AM
  • Mohammad,

    so you should post your data definition and DataGrid definition so that we can help you.

    Patrick

    Saturday, April 28, 2012 10:59 AM
  • Hi Patrick

    First I thank you for your attention. My xaml code is as below

            <sdk:DataGrid AutoGenerateColumns="False" Name="dgTarmimTankhah" Grid.Row="2" Grid.Column="1" VerticalAlignment="Top" Margin="82,3" HeadersVisibility="All" KeyDown="dgTarmimTankhah_KeyDown" CellEditEnded="dgTarmimTankhah_CellEditEnded" LoadingRow="dgTarmimTankhah_LoadingRow" SelectionChanged="dgTarmimTankhah_SelectionChanged" IsReadOnly="False">
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTemplateColumn Header="Col1" Width="80">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Sanad}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                    <sdk:DataGridTemplateColumn  Header="col2" Width="100">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Date}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                        <sdk:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <dp:PDatePicker  x:Name="txtDate" SelectedPersianDate="{Binding Date, Mode=TwoWay}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellEditingTemplate>
                    </sdk:DataGridTemplateColumn>
                    <sdk:DataGridTemplateColumn Header="col3" Width="80">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding MoeinID, Converter={StaticResource Kol_Moein_Converter}}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                        <sdk:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <input:AutoCompleteBox ItemsSource="{Binding KolMoeinList, Source={StaticResource Kol_Moein_Source}}"
                                                       Text="{Binding MoeinID, Mode=TwoWay, Converter={StaticResource Kol_Moein_Converter}}"
                                                       SelectedItem="{Binding MoeinID, Mode=TwoWay, Converter={StaticResource Kol_Moein_Index}}"
                                                       IsDropDownOpen="True" FilterMode="StartsWith"  VerticalAlignment="Center" HorizontalAlignment="Center" FlowDirection="LeftToRight"/>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellEditingTemplate>
                    </sdk:DataGridTemplateColumn>
                    <sdk:DataGridTextColumn Header="col4" Binding="{Binding CheckNo, Mode=TwoWay}" Width="*"/>
                    <sdk:DataGridTemplateColumn Header="col5" Width="150">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Bed, Converter={StaticResource RialForm}}" VerticalAlignment="Center"/>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                        <sdk:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Bed, Mode=TwoWay}" VerticalAlignment="Center"/>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellEditingTemplate>
                    </sdk:DataGridTemplateColumn>
                </sdk:DataGrid.Columns>
            </sdk:DataGrid>
    

    note: my first column is ReadOnly

    Code below insert an item to List but I cannot edit that

            private void bInsert_Click(object sender, RoutedEventArgs e)
            {
                Asnad01 a = new Asnad01();
                SanadTarmim st = new SanadTarmim();
                Per p = cmbPers.SelectedItem as Per;
                if (p != null)
                {
                    Classes.ToDay sDate = new Classes.ToDay();
                    st.Nou = 2;
                    st.SalID = MainPage.SalId;
                    st.Date = sDate.s;
                    st.Shrh = "some text";
                    st.SubSysID = MainPage.SubsysID;
                    st.PersID = p.ID;
                    st.SecID = (short)_secid;
                    st.CheckNo = "";
                    List1.Add(st);
                    bReject.IsEnabled = true;
                    dgTarmimTankhah.ItemsSource = List1.Where(b => b.PersID == p.ID).OrderBy(o => o.Sanad);
                }
            }
    

    Saturday, April 28, 2012 2:00 PM
  • Moammad,

    you should also post the definition of the class that is displayed in the data grid, especially the properties definitions: Sanad, Date, MoeinID, CheckNo and Bed.

    Patrick

    Sunday, April 29, 2012 1:06 AM
  • 
    
            public class SanadTarmim
            {
                public int Asnad01ID { get; set; }
                public short? Sanad { get; set; }
                public string Date { get; set; }
                public Decimal? Bed { get; set; }
                public Decimal? Bes { get; set; }
                public string CheckNo { get; set; }
                public short MoeinID { get; set; }
                public short Stat { get; set; }
                public short SecID { get; set; }
                public int? PersID { get; set; }
                public short SubSysID { get; set; }
                public short Nou { get; set; }
                public string Shrh { get; set; }
                public short SalID { get; set; }
            }
    Sunday, April 29, 2012 12:16 PM
  • Mohammad,

    you don't give me all your source code, so I can't test all of your solution.

    But I have created a new Silverlight project with the following MainPage.xaml:

    <UserControl
      x:Class="SilverlightApplication1.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
      mc:Ignorable="d"
      d:DesignHeight="300" d:DesignWidth="400">
    
      <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
          <RowDefinition />
          <RowDefinition Height="5" />
          <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
    
        <sdk:DataGrid AutoGenerateColumns="False"
                      Name="dgTarmimTankhah" 
                      HeadersVisibility="All"
                      IsReadOnly="False">
          <sdk:DataGrid.Columns>
            <sdk:DataGridTemplateColumn Header="Col1" Width="80">
              <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                  <TextBlock Text="{Binding Sanad}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </DataTemplate>
              </sdk:DataGridTemplateColumn.CellTemplate>
            </sdk:DataGridTemplateColumn>
            <sdk:DataGridTemplateColumn  Header="col2" Width="100">
              <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                  <TextBlock Text="{Binding Date}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                </DataTemplate>
              </sdk:DataGridTemplateColumn.CellTemplate>
              <sdk:DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                  <sdk:DatePicker x:Name="txtDate"
                                  SelectedDate="{Binding Date, Mode=TwoWay}"
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Center"/>
                </DataTemplate>
              </sdk:DataGridTemplateColumn.CellEditingTemplate>
            </sdk:DataGridTemplateColumn>
            <sdk:DataGridTemplateColumn Header="col3" Width="80">
              <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                  <TextBlock Text="{Binding MoeinID}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                </DataTemplate>
              </sdk:DataGridTemplateColumn.CellTemplate>
              <sdk:DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                  <sdk:AutoCompleteBox ItemsSource="{Binding KolMoeinList}"
                                       Text="{Binding MoeinID, Mode=TwoWay}"
                                       SelectedItem="{Binding MoeinID, Mode=TwoWay}"
                                       IsDropDownOpen="True" FilterMode="StartsWith"
                                       VerticalAlignment="Center"
                                       HorizontalAlignment="Center"
                                       FlowDirection="LeftToRight"/>
                </DataTemplate>
              </sdk:DataGridTemplateColumn.CellEditingTemplate>
            </sdk:DataGridTemplateColumn>
            <sdk:DataGridTextColumn Header="col4" Binding="{Binding CheckNo, Mode=TwoWay}" Width="*"/>
            <sdk:DataGridTemplateColumn Header="col5" Width="150">
              <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                  <TextBlock Text="{Binding Bed}" VerticalAlignment="Center"/>
                </DataTemplate>
              </sdk:DataGridTemplateColumn.CellTemplate>
              <sdk:DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                  <TextBox Text="{Binding Bed, Mode=TwoWay}" VerticalAlignment="Center"/>
                </DataTemplate>
              </sdk:DataGridTemplateColumn.CellEditingTemplate>
            </sdk:DataGridTemplateColumn>
          </sdk:DataGrid.Columns>
        </sdk:DataGrid>
    
        <Button Content="Add item" Click="Button_Click" Grid.Row="2" />
      </Grid>
    </UserControl>

    And the following MainPage.xaml.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    
    namespace SilverlightApplication1
    {
    
      public partial class MainPage : UserControl
      {
        private List<SanadTarmim> List1;
    
        public MainPage()
        {
          InitializeComponent();
          List1 = new List<SanadTarmim>();
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
    //      Asnad01 a = new Asnad01();
          SanadTarmim st = new SanadTarmim();
    //      Per p = cmbPers.SelectedItem as Per;
    //      if (p != null)
    //      {
    //        Classes.ToDay sDate = new Classes.ToDay();
            st.Nou = 2;
    //        st.SalID = MainPage.SalId;
    //        st.Date = sDate.s;
            st.Date = DateTime.Now.ToString();
            st.Shrh = "some text";
    //        st.SubSysID = MainPage.SubsysID;
    //        st.PersID = p.ID;
    //        st.SecID = (short)_secid;
            st.CheckNo = "";
            List1.Add(st);
    //        bReject.IsEnabled = true;
    //        dgTarmimTankhah.ItemsSource = List1.Where(b => b.PersID == p.ID).OrderBy(o => o.Sanad);
            dgTarmimTankhah.ItemsSource = List1.OrderBy(o => o.Sanad);
    //      }
        }
      }
    
    
      public class SanadTarmim
      {
        public int Asnad01ID { get; set; }
        public short? Sanad { get; set; }
        public string Date { get; set; }
        public Decimal? Bed { get; set; }
        public Decimal? Bes { get; set; }
        public string CheckNo { get; set; }
        public short MoeinID { get; set; }
        public short Stat { get; set; }
        public short SecID { get; set; }
        public int? PersID { get; set; }
        public short SubSysID { get; set; }
        public short Nou { get; set; }
        public string Shrh { get; set; }
        public short SalID { get; set; }
      }
    
    }

    With this code I can add items and I can edit them!

    Perhaps the problem is with your converters, but I can test this without more information (code) from you. If you don't want to publish more post, try to simplify your XAML, for example by removing temporarily the converters.

    Patrick

    Sunday, April 29, 2012 1:19 PM