Skip to content
Advertisement

How do I clear specific columns in DataGrid?

i am using WPF, connected to SQL DataBase via LinqToSql. I have filled DataGrid using ItemsSource.

I would like to clear everything in my DataGrid, except first two rows(ID,first and second name), and Headers ofcourse, by pressing a button. All items i would like to(either set to null or 0) remove, are floats, but I can’t find the right logic to do that.

Here is the picture of my DataGrid–>enter image description here

public partial class Spisak : Microsoft.Office.Interop.Excel.Window
{
    DiplomskiDataContext diplomski = new DiplomskiDataContext();
    public Spisak()
    {
        InitializeComponent();

        dataGridRadnici.ItemsSource = diplomski.Radniks.ToList();
    }
}

This is my code behind, and here is Xaml code:

<DataGrid HorizontalAlignment="Left" Name="dataGridRadnici" AutoGenerateColumns="False" Height="260" Margin="10,120,0,0" VerticalAlignment="Top" Width="550">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Ime" Binding="{Binding Ime}"></DataGridTextColumn>
            <DataGridTextColumn Header="Prezime" Binding="{Binding Prezime}"></DataGridTextColumn>
            <DataGridTextColumn Header="BrSati" Binding="{Binding BrojSati, Converter={StaticResource ThreePlaceConverter}}"></DataGridTextColumn>
            <DataGridTextColumn Header="Prekovremeni" Binding="{Binding BrojPrekovremenih, Converter={StaticResource ThreePlaceConverter}}"></DataGridTextColumn>
            <DataGridTextColumn Header="Gorivo" Binding="{Binding BrojDana, Converter={StaticResource ThreePlaceConverter}}"></DataGridTextColumn>
            <DataGridTextColumn Header="Bonus" Binding="{Binding Bonus}"></DataGridTextColumn>
            <DataGridTextColumn Header="VracanjeDuga" Binding="{Binding VracanjeDuga}"></DataGridTextColumn>
            <DataGridTextColumn Header="UkZarada" Binding="{Binding UkupnaPlata, Converter={StaticResource ThreePlaceConverter}}"></DataGridTextColumn>
            <DataGridTextColumn Header="UkSaGorivom" Binding="{Binding UkupnoSaGorivom, Converter={StaticResource ThreePlaceConverter}}"></DataGridTextColumn>
        </DataGrid.Columns>
        <DataGrid.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Izmeni" Click="MenuItem_Click"></MenuItem>
                <MenuItem Header="Obrisi" Click="MenuItem_Click_1"></MenuItem>
            </ContextMenu>
        </DataGrid.ContextMenu>
    </DataGrid>

Advertisement

Answer

Change the type of the columns that you want to be able to blank out from float to Nullable<float> (float?) and simply set the properties to null:

var sourceList = dataGridRadnici.ItemsSource as List<Radnik>; //or whatever your data is called
foreach (var item in sourceList)
{
    item.BrojPrekovremenih = null; // or 0
    //set the other properties the same way...
}

You also need to implement the INotifyPropertyChanged interface and raise property change notifications in your data class.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement