Search code examples
wpfdatagridcell

Dynamically change the cell text of a data grid wpf


In my WPF project, I have a datagrid which has cells bound to different things.

Here is the xaml:

<DataGrid x:Name="Tasks" CanUserDeleteRows="True">
     <DataGrid.Columns>
          <DataGridTextColumn Header="Column1" Binding="{Binding C1}"/>
          <DataGridTextColumn Header="Column2" Binding="{Binding C2}"/>
          <DataGridTextColumn Header="Column3" Binding="{Binding C3}"/>
          <DataGridTextColumn Header="Column4" Binding="{Binding C4}"/>
          <DataGridTextColumn Header="Column5" Binding="{Binding C5}"/>
          <DataGridTextColumn Header="Column6" Binding="{Binding C6}"/>
          <DataGridTextColumn Header="Column7" Binding="{Binding C7}"/>
          <DataGridTextColumn Header="Status" Binding="{Binding Status, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
     </DataGrid.Columns>
     <DataGrid.ContextMenu>
          <ContextMenu>
               <MenuItem Header="Add task" Click="ADDtask_Click" FontSize="11">
               </MenuItem>
          </ContextMenu>
     </DataGrid.ContextMenu>
</DataGrid>

I have obviously simplified it and renamed all the columns except for one, the Status column.

I have this C# code:

public Guid Identifier { get; }
public string Name { get; set; }
public string Keywords { get; set; }
public string Colour { get; set; }
public string Size { get; set; }
public string Category { get; set; }
public string Profile { get; set; }
public string Status { get; set; }
public string Mode { get; set; }

public Result(string c1, string c2, string c3, string c4, string c5, string c6, string c7, string status)
{
        this.Identifier = Guid.NewGuid();
        this.Name = c1;
        this.Keywords = c2;
        this.Colour = c3;
        this.Size = c4;
        this.Category = c5;
        this.Profile = c6;
        this.Mode = c7;
        this.Status = status;
}

private void Start_Task_Click(object sender, RoutedEventArgs e)
{
        System.Windows.Controls.Button button = (System.Windows.Controls.Button)sender;
        Result task = (Result)button.DataContext;

        if(...)
        {
            ????
        }
        else
        {
            return;
        }
}

public string idlestatus = "idle";

// this adds the columns to the datagrid
private void Button_Click1(object sender, RoutedEventArgs e) 
{
    Tasks.Items.Add(new Result(textbox1.Text, textbox2.Text, textbox3.Text, textbox4.Text, textbox5.Text, textbox6.Text, textbox7.Text, idlestatus));
}

Where I have placed the ???, I would like to make an if statement and if it is true, change the cells text to something. I have tried doing something like this:

task.Status.Replace("idle", "...");

But it hasn't seemed to work. Any help would be appreciated.


Solution

  • SOLVED

    The string I wanted to change, I needed to put

    private void NotifyPropertyChanged(string p)
        {
            Debug.WriteLine(p + ": notify propertychanged");
            PropertyChangedEventHandler handler = PropertyChanged;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(p));
        }
        public string Status
        {
            get
            {
                return _Status;
            }
            set
            {
                _Status = value;
                NotifyPropertyChanged("Status");
            }
        }