Click here to Skip to main content
15,881,856 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
How to add rows dynamically in Datagrid? Because I want to add empty row without details and enter details from UI and save.

My Datagrid .xaml code goes here

Thanks in advance..
XML
<UserControl x:Class="SilverlightApplication52.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <Grid x:Name="LayoutRoot" Background="White">
        <sdk:DataGrid AutoGenerateColumns="False" Height="176" HorizontalAlignment="Left" Margin="50,35,0,0" Name="DataGrid1" VerticalAlignment="Top" Width="312" >
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn x:Name="FirstName"
                                        Header="First Name"
                                        Binding="{Binding Path=FirstName}"
                                        IsReadOnly="False"
                                        Width="150"/>
                <sdk:DataGridTextColumn x:Name="LastName"
                                        Header="Last Name"
                                        Binding="{Binding Path=LastName}"
                                        IsReadOnly="False"
                                         Width="150"/>

                <sdk:DataGridTextColumn x:Name="Contactid"
                                            Header="Contact Id"
                                            Binding="{Binding Path= Contactid}"
                                            IsReadOnly="True"
                                            Width="150"
                                            Visibility="Collapsed"/>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>
        <Button Content="New" Height="23" HorizontalAlignment="Left" Margin="23,242,0,0" Name="btn_new" VerticalAlignment="Top" Width="75" Click="btn_new_Click" />
        <Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="114,242,0,0" Name="btn_save" VerticalAlignment="Top" Width="75" Click="btn_save_Click" />
        <Button Content="Delete" Height="23" HorizontalAlignment="Left" Margin="207,242,0,0" Name="btn_delete" VerticalAlignment="Top" Width="75" Click="btn_delete_Click" />
    </Grid>
</UserControl>
Posted
Updated 5-Dec-11 21:42pm
v3

To Add empty Row in DataGrid simple add one value to the Collection bound to this Datagrid.

for Example,

If you have bound a DataTable to your DataGrid then adding new Row in To DataTable will Automatically add new Empty Row in Datagrid.
 
Share this answer
 
A similar question was asked here[^]. The answers should surely help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900