Tinashe Kunaka
Tinashe Kunaka

Reputation: 23

wpf and c# writeable datagrid

I want to make a DataGridView sheet that can allow a user to input data.

Below is my xaml code for my datagrid

<DataGrid AutoGenerateColumns="True" 
    Height="710" 
    HorizontalAlignment="Left" 
    Name="flowgrid" 
    VerticalAlignment="Top" 
    Width="1000" Margin="181,91,0,0" SelectionChanged="dataGrid1_SelectionChanged">

    <DataGrid.Columns >
        <DataGridTextColumn Header="Account Details" 
            x:Name="value1" IsReadOnly="True"   MinWidth="180" />
        <DataGridTextColumn Header="Due Date"   MinWidth="100" />
        <DataGridTextColumn Header="Standard Amount"   MinWidth="100" />
        <DataGridTextColumn Header="Current"   MinWidth="100" />
        <DataGridTextColumn Header="Week 1"  MinWidth="100" />
        <DataGridTextColumn Header="Week 2"  MinWidth="100" />
        <DataGridTextColumn Header="Week 3"  MinWidth="100" />
        <DataGridTextColumn Header="Week 4"  MinWidth="100" />
        <DataGridTextColumn Header="After"  MinWidth="150" />
    </DataGrid.Columns>
</DataGrid>

Upvotes: 0

Views: 183

Answers (2)

Daniel
Daniel

Reputation: 75

You have to set the IsReadOnly property of the cells you want the user to edit (or of the whole DataGrid) to False:

IsReadOnly="False"

Also, if you want to let the user add new rows, you can set the CanUserAddRows property to True:

CanUserAddRows="True"

To input the data into the dataGrid I do it like this: 1- Empty the dataGrid in the xaml file: 2- Create a Datatable object with the headers and the data 3- set it like ItemSource of the datagrid

XAML:

<DataGrid Name="dataGrid"></DataGrid>

VB:

Dim inputDataDB As DataTable
inputDataDB = getInputData()

dataGrid.ItemsSource = inputDataDB .DefaultView

Hope it Help

Upvotes: 1

mm8
mm8

Reputation: 169420

For you to be able to edit items in a DataGrid, you need to set its ItemsSource property to an IList.

You cannot add items directly to the Items property like this:

flowgrid.Items.Add(new Account());

Instead you should set the ItemsSource property:

flowgrid.ItemsSource = new List<Acccount>() { ... };

Upvotes: 0

Related Questions