1
votes

I'm writing C# WPF application. I'm new to it and tried to load a List to the DataGrid and failed. Then I added a WinForm to my WPF solution with DataGridView. when I loaded the list to the DataGridView.DataSource I didn't had the '*' row to add new row even though the AllowUserToAddRows property is True, I converted the List to BindingList and still there's now new row. I've searched everywhere but everyone says that the BindingList solved them the problem. Is it because its a WinForm in WPF application? How do i fix that?

2
have you checked the readonly property of the datagridview?Mohammad Chamanpara
please show the relevant codeWiiMaxx
Forget winforms. Post the relevant code and XAML.Federico Berasategui
Yes Mohammad, the ReadOnly is false, I'll check OmegaMan answer and if it doesn't work i'll post the code. Thanks for now.Erez Konforti

2 Answers

0
votes

Focus on using WPF, the winforms plugin works, but one finds due to the idiosyncrasis of the process it is truly not optimal.


Here is a basic example I have created which binds to a common list array. You can use an ObservableCollection or a simple generic list. In WPF one primarily defines the look/feel in xaml and then binds data to that. The ItemsSource is standard way of getting items into the datagrid to be shown.

Window.Resources>
    <model:People x:Key="People">
        <model:Person First="Joe"   Last="Smith"   Phone="303-555 5555" />
        <model:Person First="Mary"  Last="Johnson" Phone="720-555 5555" />
        <model:Person First="Frank" Last="Wright"  Phone="202-555 5555" />
    </model:People>
</Window.Resources>
<DataGrid AutoGenerateColumns="False"
          ItemsSource="{StaticResource People}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="First" Binding="{Binding First}" />
        <DataGridTextColumn Header="The Name" Binding="{Binding Last}" />
        <DataGridTextColumn Header="Phone Number" Binding="{Binding Phone}"/>
    </DataGrid.Columns>
    <DataGrid.RowDetailsTemplate>
       <DataTemplate>
           <TextBlock Text="{Binding Phone}" />
       </DataTemplate>
    </DataGrid.RowDetailsTemplate>
</DataGrid>

The result is the list with a rows template which opens upon click:

WPF DataGrid

Now most likely you will not use a Static Resource defined in Xaml to bind to just as I have shown, instead one places oa VM (ViewModel) class instance which implements INotifyPropertyChanged and creates a People property.

The VM can be accessed from the page's data context after it is most likely instantiated in code behind such as

public MyPage()
{
     InitializeComponent();
     DataContext = new MyPageViewModel(); // Contains logic for the 'People' Property of a list of people.

}

}

Then one changes the binding in my example above to bind to the page's datacontext which is inheirted from the page because nothing was set for the datagrid control.

<DataGrid AutoGenerateColumns="False"
          ItemsSource="{Binding People}">

For a complete MVVM example see my blog Xaml: ViewModel Main Page Instantiation and Loading Strategy for Easier Binding

0
votes

Ok I fixed it, the default constructor of the binding object was not public. And on the way I got it to bind with the WPF DataGrid with ItemsSource (which is weird becaue when i tried it before i had the columns headers, and rows in the number of the items in the list, but no data in the rows.. but if it works dont touch it :) ) Thanks everyone.