How to update datagridview column from text content of text field in C # Windows form

I have a datagridview with contents from a table. In this case, I have a column for notes, which will be 1-2 rows. When I click on the notes column, I want to open another form containing a text box. I linked the text box to the table using a table adapter. Now when I close the form in the text box, I want to show it in the datagridview column. Please help me

+4
source share
2 answers

The way I did this in the past is to pass the Action delegate to the second form, which refers to the method from the first form.

The method you pass contains logic that updates your DataGridView.

Then in the second closing event, you call this delegate (after checking that it is not null), passing the value from your text field.

Below is a quick prototype code to show how to do this. My method from Form1 just shows the message box, but you can easily change it to update the DataGridView data source.

public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 form = new Form2(); Action<string> showMessage = ShowMessage; form.ClosingMethod(showMessage); form.Show(); } private void ShowMessage(string message) { MessageBox.Show(message); } } public partial class Form2 : Form { private Action<string> _showMessage; public Form2() { InitializeComponent(); } public void ClosingMethod(Action<string> showMessage) { _showMessage = showMessage; } private void Form2_FormClosing(object sender, FormClosingEventArgs e) { if (_showMessage != null) { _showMessage("hippo"); } } } 

Edit

It just occurred to me that the delegate call _showMessage("hippo"); is blocked.

Your form will not be closed until the delegation is complete - possibly for a long time. In my example message box, the form does not close until the OK button is clicked.

To get around this, you can call your delegate asynchronously, as shown below:

 private void Form2_FormClosing(object sender, FormClosingEventArgs e) { if (_showMessage != null) { _showMessage.BeginInvoke("hippo", null, null); } } 
+4
source

If your DataGridView is connected to the table using the TableAdapter, you need to update the cell yourself and then call the update to return the data to the table, or you can update the table from the dialog box and then update the DataGridView.

0
source

Source: https://habr.com/ru/post/1304520/


All Articles