1

I am new in WPF. I used to work in Winforms.

In Winforms I had the DataGridView that allows me to change, when I want a cell value.

Simply using:

dataGridView[columnIndex, rowIndex].Value = "New Value";

It works.

How can I accomplish this using DataGrid from WPF? I was looking thorught stack over flow and could figure out an easy way to do this.

Thank you

Ricardo Polo Jaramillo
  • 12,110
  • 13
  • 58
  • 83
  • 2
    I have never touched a DadaGrid from code behind, is there a reason your not just changing the value in the ItemSource? – sa_ddam213 Dec 17 '12 at 01:06
  • How can I do it? I dont have info from a DataBase. I just want some content updated based on funtions. Imagine a table who shows what computers have connection based on ping. I want to change the value of cells on runtime but I dont have a DB. – Ricardo Polo Jaramillo Dec 17 '12 at 01:07
  • 1
    You dont need a DB, just a list or something, i will try create a simple example for you, hold tight :) – sa_ddam213 Dec 17 '12 at 01:12
  • Thank you. I dont think a list could work for my particullar case because I need to edit an specify row, because this edit is made for each row independently (I have a thread per row checking something) – Ricardo Polo Jaramillo Dec 17 '12 at 01:29
  • The list will hold all the rows, and the columns will be properties of the object in the list. its the same as winforms in a way but YOU own the list and can modify. – sa_ddam213 Dec 17 '12 at 01:32

1 Answers1

1

Ok the simplest way to handle DataGrid is by binding to an ItemSource.

The example below shows how to bind your list and how changes upadte the DataGrid.

public partial class MainWindow : Window
{
    private ObservableCollection<ConnectionItem> _connectionitems = new ObservableCollection<ConnectionItem>();

    public MainWindow()
    {
        InitializeComponent();
        ConnectionItems.Add(new ConnectionItem { Name = "Item1", Ping = "150ms" });
        ConnectionItems.Add(new ConnectionItem { Name = "Item2", Ping = "122ms" });
    }

    public ObservableCollection<ConnectionItem> ConnectionItems
    {
        get { return _connectionitems; }
        set { _connectionitems = value; }
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        // to change a value jus find the item you want in the list and change it
        // because your ConnectionItem class implements INotifyPropertyChanged
        // ite will automaticly update the dataGrid

        // Example
        ConnectionItems[0].Ping = "new ping :)";
    }
}

public class ConnectionItem : INotifyPropertyChanged
{
    private string _name;
    private string _ping;

    public string Name
    {
        get { return _name; }
        set { _name = value; NotifyPropertyChanged("Name"); }
    }

    public string Ping
    {
        get { return _ping; }
        set { _ping = value; NotifyPropertyChanged("Ping"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    /// <summary>
    /// Notifies the property changed.
    /// </summary>
    /// <param name="property">The info.</param>
    public void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

Xaml:

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication4"
        xmlns:properties="clr-namespace:WpfApplication4.Properties"
        Title="MainWindow" Height="300" Width="400" Name="UI" >
    <Grid>
        <DataGrid Name="dataGridView" ItemsSource="{Binding ElementName=UI,Path=ConnectionItems}" Margin="0,0,0,40" />
        <Button Content="Change" Height="23" HorizontalAlignment="Left" Margin="5,0,0,12" Name="button1" VerticalAlignment="Bottom" Width="75" Click="button1_Click" />
    </Grid>
</Window>

i added a button to show how the data updates when you change something in your list, The class ConnectionItem is where you will store all your info for the datagrid.

Hope this helps

sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
  • Thanks this help me a lot. Now I have it working. A little more question, I see that the DataGrid show your whole Collection, with all the propertys of your objects. What can I do if I need some columns show only in certains moments? – Ricardo Polo Jaramillo Dec 17 '12 at 05:56
  • it depends on how and when you want to shoe/hide them, there are some good articles here: http://stackoverflow.com/questions/6857780/how-to-hide-wpf-datagrid-columns-depending-on-a-property http://www.c-sharpcorner.com/uploadfile/dpatra/hideun-hide-columns-using-context-menu-in-datagrid-in-wpf/ or in code behind its quite easy ` dataGridView.Columns.FirstOrDefault(c => c.Header.ToString() == "Ping").Visibility = Visibility.Hidden;` – sa_ddam213 Dec 17 '12 at 06:32