'Windows Forms - Change Datagrid Cell Value when Button in another Form is clicked

I have a value from Timer Form, which i want to set in a specific Datagridview Cell in the Main Form. When the new value is set, i want to update the Datagrid, so the right value is in shown in the Datagrid.

Thanks for your effort!

enter image description here

enter image description here



Solution 1:[1]

Setup an event in Timer form and pass the value, here is a basic example. Adjust to work with your code in regards to your DataGridView and if the value being passed is not a string, change it.

public partial class TimerForm : Form
{
    public delegate void OnWhatEver(string sender);
    public event OnWhatEver WhatEverHandler;
    public TimerForm()
    {
        InitializeComponent();
    }

    private void WhatEverButton_Click(object sender, EventArgs e)
    {
        WhatEverHandler?.Invoke($"{sender:f}");
    }
}

Main Form, one button, one label

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }

    private void OpenTimerForm_Click(object sender, EventArgs e)
    {
        TimerForm form = new TimerForm();
        form.WhatEverHandler += OnWhatEverEvent;
        form.ShowDialog();
    }

    private void OnWhatEverEvent(string sender)
    {
        label1.Text = sender;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Karen Payne