简体   繁体   中英

Edit Cell in DataGrid (WPF)

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

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

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