Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want data in the rest of wpf DataGrid to be read only and only new row should be editable

I have managed to get DataGrid to show new row for adding new item. Problem i face now is i want data in the rest of wpf DataGrid to be read only and only new row should be editable.

Currently this is how my DataGrid looks.

<DataGrid AutoGenerateColumns="False" Name="DataGridTest" CanUserAddRows="True" Grid.Row="2" ItemsSource="{Binding TestBinding}" >     <DataGrid.Columns>                 <DataGridTextColumn Header="Line" IsReadOnly="True" Binding="{Binding Path=Test1}" Width="50"></DataGridTextColumn>         <DataGridTextColumn Header="Account" IsReadOnly="True"  Binding="{Binding Path=Test2}" Width="130"></DataGridTextColumn>                    </DataGrid.Columns> </DataGrid> 

But since I have kept the columns read only, a new row also adds as read only which is what I don't want.

like image 462
Mandar Jogalekar Avatar asked Apr 27 '13 11:04

Mandar Jogalekar


People also ask

How add rows and columns to WPF DataGrid programmatically?

To programatically add a column: DataGridTextColumn textColumn = new DataGridTextColumn(); textColumn. Header = "First Name"; textColumn. Binding = new Binding("FirstName"); dataGrid.

Can user add rows DataGrid WPF?

WPF DataGrid (SfDataGrid) provides built-in row called AddNewRow. It allows user to add a new row to underlying collection.

What is the difference between grid and DataGrid in WPF?

A Grid is a control for laying out other controls on the form (or page). A DataGrid is a control for displaying tabular data as read from a database for example.


1 Answers

Try this MSDN blog

Also, try the following example:

Xaml:

   <DataGrid AutoGenerateColumns="False" Name="DataGridTest" CanUserAddRows="True" ItemsSource="{Binding TestBinding}" Margin="0,50,0,0" >         <DataGrid.Columns>             <DataGridTextColumn Header="Line" IsReadOnly="True" Binding="{Binding Path=Test1}" Width="50"></DataGridTextColumn>             <DataGridTextColumn Header="Account" IsReadOnly="True"  Binding="{Binding Path=Test2}" Width="130"></DataGridTextColumn>         </DataGrid.Columns>     </DataGrid>     <Button Content="Add new row" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/> 

CS:

 /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window {     public MainWindow()     {         InitializeComponent();     }      private void Button_Click_1(object sender, RoutedEventArgs e)     {         var data = new Test { Test1 = "Test1", Test2 = "Test2" };          DataGridTest.Items.Add(data);     } }  public class Test {     public string Test1 { get; set; }     public string Test2 { get; set; } } 
like image 81
Elyor Avatar answered Sep 19 '22 18:09

Elyor