C# .NET - WPF,Datagrid, fixed row number, - Asked By Raman on 15-Oct-10 02:28 AM

Hi,

I am using a datagrid in which I need to allow user only to add 256 rows. How can I do it?

Thanks,
Raman
Michael Detras replied to Raman on 15-Oct-10 03:05 AM
Hi, you can try adding a trigger:

    <Trigger Property="Items.Count" Value="256">
      <Setter Property = "CanUserAddRows" Value="False"/>
    </Trigger>

This is just a guess so I'm not sure if this will work. Hope this helps.
Raman replied to Michael Detras on 15-Oct-10 03:56 AM
thanks for the reply michael.
But WPF datagrid doesnot have property "Items".
Any other idea? :)
Michael Detras replied to Raman on 15-Oct-10 04:05 AM
I checked it again and DataGrid does have an Items property. It inherited it from ItemsControl. Are you using WPF or Silverlight? As far as I can remember, the Silverlight version of the DataGrid does not have an Items property.
Raman replied to Michael Detras on 15-Oct-10 09:50 AM
I tried adding the xaml code you provided as content of datagrid but it throws compile time error which says "Cannot find the type 'Items'". Am I doing something wrong?
Michael Detras replied to Raman on 15-Oct-10 12:14 PM
Not sure what is wrong. Could you post some code?
Raman replied to Michael Detras on 21-Oct-10 08:37 AM

<DataGrid Name="dgColorConfiguration"

AutoGenerateColumns="False"

CanUserResizeRows="False"

Grid.Row="1" RowHeaderWidth="20"

CanUserDeleteRows="False"

ItemsSource="{Binding CopiedColorPalette}"

HorizontalAlignment="Stretch"

VerticalAlignment="Stretch"

ColumnWidth="*"

IsSynchronizedWithCurrentItem="True">
   <Trigger Property="Items.Count" Value="256">
    <Setter Property = "CanUserAddRows" Value="False"/>
    </Trigger>

</DataGrid>

   <        
Michael Detras replied to Raman on 21-Oct-10 08:46 AM
Use it like this:

<DataGrid>
  <DataGrid.Style>
    <Style TargetType="DataGrid">
      <Style.Triggers>
         <Trigger Property="Items.Count" Value="256">
          <Setter Property = "CanUserAddRows" Value="False"/>
        </Trigger>
      </Style.Triggers>
    </Style>
  </DataGrid.Style>
</DataGrid>

Hope this helps.