简体   繁体   中英

How do I add rows to a bound multi-column DataGrid (Silverlight 4.0)

How would I go about adding items to the DataGrid with the code below

<sdk:DataGrid AutoGenerateColumns="False" Height="294" HorizontalAlignment="Left" Margin="63,27,0,0" Name="Assignments_datagrid" VerticalAlignment="Top" Width="533" IsReadOnly="True" SelectionChanged="Assignments_datagrid_SelectionChanged">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Header="Assignment Name" Binding="{Binding Assignment_Name}"/>
                <sdk:DataGridTextColumn Header="Type" Binding="{Binding Type}"/>
                <sdk:DataGridTextColumn Header="Due Date" Binding="{Binding Assignment_Due_Date}"/>
                <sdk:DataGridTextColumn Header="Class Name" Binding="{Binding Course_Name}"/>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

hmmm, I'm not sure about what your problem is but here is what I suggest :

  • make sure your dataGrid's "CanUserAddRows" option is set to "True"

  • then you have an extra line at the bottom.

  • If you click the rowHeader of that line, you can add a new line. This will add a new Item to your source collection

!! Be careful that for this to work, you have to make sure that the class of your source item has a constructor with no parameter (I had some trouble with the "new row" stuff before figuring this out)

eg :

    Class Item {
        public Assignment_Name { get; set; }
        ...

        public Item() { }

        ...

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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