简体   繁体   English

如何从c #Windows窗体中的文本框的文本内容更新datagridview列

[英]How to update the column of datagridview from the text contents of textbox in c# Windows form

I have a datagridview with contents from a table. 我有一个datagridview与表中的内容。 In that I have a column for Remarks which will be 1-2 lines. 在那里我有一个备注栏,它将是1-2行。 When I click on the remarks column, I want to open another form that contains the text box. 当我单击备注列时,我想打开另一个包含文本框的表单。 I have linked the text box with the table using the table adapter. 我已使用表适配器将文本框与表链接。 Now when I close the form with the text box, I want to show that in the datagridview column. 现在当我用文本框关闭表单时,我想在datagridview列中显示它。 Please help me 请帮我

The way I've done this in the past is to pass a Action delegate to the second form that references a method from the first form. 我过去这样做的方法是将Action委托传递给第二个表单,该表单引用第一个表单中的方法。

The method you pass in contains the logic that updates your DataGridView. 传入的方法包含更新DataGridView的逻辑。

Then in your second form closing event you call this delegate (after checking that it is not null) passing the value from your textbox. 然后在您的第二个表单关闭事件中,您调用此委托(在检查它不是null之后)从您的文本框传递值。

Below is some quick prototype code to show how to do this. 下面是一些快速原型代码,以说明如何执行此操作。 My method from Form1 just shows a message box, but you can easily change this to update your DataGridView datasource. 我在Form1中的方法只显示一个消息框,但您可以轻松更改它以更新DataGridView数据源。

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 编辑

Just occurred to me that the call to the delegate _showMessage("hippo"); 刚刚发生了对代表_showMessage("hippo");的调用_showMessage("hippo"); is blocking. 阻止。

Your form will not close until the delegate completes - potentially a long time. 在代表完成之前,您的表单不会关闭 - 可能需要很长时间。 In my message box example, the form doesn't 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);
    }
}

如果您的DataGridView附加到具有TableAdapter的表,则以太必须自己更新单元,然后调用update将数据推回到表中,或者您可以从对话框更新表,然后刷新DataGridView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM