简体   繁体   English

WPF和C#可写DataGrid

[英]wpf and c# writeable datagrid

I want to make a DataGridView sheet that can allow a user to input data. 我想制作一个可以允许用户输入数据的DataGridView工作表。

Below is my xaml code for my datagrid 以下是我的datagrid的xaml代码

<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>

You have to set the IsReadOnly property of the cells you want the user to edit (or of the whole DataGrid) to False : 您必须将要用户(或整个DataGrid)编辑的单元格的IsReadOnly属性设置为False

IsReadOnly="False"

Also, if you want to let the user add new rows, you can set the CanUserAddRows property to True : 另外,如果要让用户添加新行,可以将CanUserAddRows属性设置为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 要将数据输入到dataGrid中,我需要这样做:1-清空xaml文件中的dataGrid:2-创建带有标题和数据的Datatable对象3-将其设置为datagrid的ItemSource

XAML: XAML:

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

VB: VB:

Dim inputDataDB As DataTable
inputDataDB = getInputData()

dataGrid.ItemsSource = inputDataDB .DefaultView

Hope it Help 希望对你有帮助

For you to be able to edit items in a DataGrid , you need to set its ItemsSource property to an IList . 为了能够在DataGrid编辑项目,需要将其ItemsSource属性设置为IList

You cannot add items directly to the Items property like this: 您不能像这样直接将项目添加到Items属性:

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

Instead you should set the ItemsSource property: 相反,您应该设置ItemsSource属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM