如何将文本从一个表单中的 DataGridView 传输到另一个表单中的私有 TextBox? [英] How can I transfer text from a DataGridView in one Form to a private TextBox in another Form?

查看:34
本文介绍了如何将文本从一个表单中的 DataGridView 传输到另一个表单中的私有 TextBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[摘要]
我的程序中有一个带有多个私有 TextBoxesInvoice form.当用户更改这些 TextBox 的文本时,就会出现搜索表单.
我想将搜索表单中的 DataGridView(链接到我的数据库)中的一些值传输到 Invoice 表单中的 TextBoxes(例如,当我按 Enter 时).

[Summary]
I have an Invoice form with several private TextBoxes in my program. When the user changes the text of those TextBoxes, the search Form appears.
I would like to transfer some values from my DataGridView (which is linked to my database) in the search Form to the TextBoxes in the Invoice Form (when I press Enter for example).

[说明]
在我的发票表单中,我有 merchandise code TextBox 和 merchandise name TextBox.当用户改变这些TextBox的文本时,就会出现搜索Form,输入的文本会被转移到searchform的TextBox中.
searchform 只有一个 TextBox 和一个 DataGridView.当在搜索表单的 TextBox 中输入内容时,DataGridView 将搜索并显示结果.DataGridView 具有与发票表单的文本框匹配的列.

[Description]
In my Invoice form I have merchandise code TextBox and merchandise name TextBox. When the user changes the text of those TextBoxes, the search Form appears and the entered text will be transferred to the TextBox of searchform.
The searchform only has a TextBox and a DataGridView. When something is entered in the TextBox of the search Form, the DataGridView will search and show the results. The DataGridView has Columns that match the Invoice Form's TextBoxes.

现在我想设法做到这一点:当我在searchform上输入时,DataGridView当前行的信息被转移到Invoice中对应的TextBoxes表格.
商品代码 Column to merchandise code TextBox 和 merchandise name Column to merchandise name TextBox.

Now I want to manage to do this: when I enter on searchform, the information of the current row of the DataGridView is transferred to the corresponding TextBoxes in the Invoice Form.
merchandise code Column to merchandise code TextBox and merchandise name Column to merchandise name TextBox.

我可以用以下代码说明这一点:
(我知道如何在 DataGridView 中获取所选行的值,我的问题只是标题...)

I could illustrate this in the following code:
(I know how to get the values of selected row in DataGridView my question is just the title...)

if (e.KeyCode == Keys.Enter)
{
    SqlCommand sqlcmd = new SqlCommand("SELECT ID FROM X WHERE ID=" +
                        dataGridView1.CurrentRow.Cells[0].Value + "", sqlcon);
    SqlDataReader sqldr = sqlcmd.ExecuteReader();
    while (sqldr.Read())
    {
        InvoiceForm.CodeTextBox = sqldr[codecolumn].Tostring
        InvoiceForm.NameTextBox = sqldr[Namecolumn].Tostring
        InvoiceForm.BlahTextBox = sqldr[Blahcolumn].Tostring                               
    }
}

我已经尝试了上面的代码,但它说:

I have tried the above code but its says:

codeTextBox 是私有的...由于保护无法这样做级别...

codeTextBox is private... not able to do so because of protection level...

假设我不想将这些 TextBox 的保护级别更改为 public.

Let's assume I don't want to change the protection level of those TextBoxes to public.

推荐答案

在不实现接口的情况下,有两种简单的方法可以从另一个类中引用现有的 Form 类.

Without implementing an Interface, there are two simple methods to reference an existing Form class from another class.

在被调用者的构造函数中传递调用者类(this)的引用:

Passing the reference of the caller class (this) in the constructor of the callee:

var f2 = new Form2(this);
f2.Show();

使用所有者被调用者的属性 (Form2).使用 Show(Owner) 设置所有者ShowDialog(Owner) 方法.this 是调用者的实例:

Using the Owner property of the callee (Form2). The Owner is set using the Show(Owner) or ShowDialog(Owner) methods. this is the instance of the caller:

var f2 = new Form2();
f2.Show(this);

你也可以在被调用者中有一个公共属性(Form2),用于设置当前调用者(this):

You could also have a public Property in the callee (Form2), used to set the current caller (this):

var f2 = new Form2();
f2.MyCaller = this;
f2.Show();

几乎没用,因为前两种方法已经使用标准特征实现了相同的结果.当然,还有其他方法,但在这种情况下几乎是矫枉过正.

Pretty much useless since the two former methods already achieve the same result using the standard features. There are other means, of course, but pretty much overkill in this context.

在这里,我使用 Owner 属性来访问实例化您的 Search Form 类的 Form 类的实例.该示例使用调用者类(您的 InvoiceForm)的公共方法,被调用者(您的 Search 表单)使用该方法将用户选择的值传回.

Here, I'm using the Owner property to access the instance of the Form class that instantiated your Search Form class. The example uses a public method of the caller class (your InvoiceForm), which the callee (your Search Form) uses to pass back the values a user selected.

使用 Form.Show(this) 还意味着显示的 Form 将是 parented(不要与 Parent 属性混淆,虽然)与显示它的表单并将留在它上面.
您也可以使用 ShowDialog(this) 方法,如果它更适合您的情况.在这种情况下,表单将显示为模态对话框.

Using Form.Show(this) also implies that the Form shown will be parented (not to be confused with the Parent property, though) with the Form that showed it and will stay on top of it.
You could also use the ShowDialog(this) method, if it's preferable in your case. The Form will be shown as modal dialog in this case.

我正在使用此公共方法制作两个示例:

I'm making two examples using this public method:

  1. 带有类参数的公共方法,其中包含可以在 InvoiceForm 控件中设置的所有值.这可能是传递这些值的首选方法,因为它可以更轻松地在不同的上下文中扩展和重用.
  2. 带有字符串参数的公共方法,对应于要设置的 TextBoxes 值
  1. A public method with a class parameter, which contains all the values that can be set in the InvoiceForm controls. This is probably the preferred method to pass these values, because it can be more easily extended and re-used in different contexts.
  2. A public method with string parameters, corresponding to the TextBoxes values to set


带有类参数的公共方法:
注意this.Owner is InvoiceForm frm是用来标识当前的Owner.
UpdateMyControls 类是用于传输特定值的容器.如果所有者不同,SearchForm 的行为可能会有所不同.
这在某种程度上简化了,但您可以使用此选择对不同的调用者重复使用 SearchForm,为每个 Owner 获得不同的结果.


Public method with a class parameter:
Note that this.Owner is InvoiceForm frm is used to identify the current Owner.
The UpdateMyControls class is the container used to transfer specific values. The SearchForm could act differently if the Owner was a different one.
This is somewhat simplified, but you can use this selection to re-use the SearchForm with different callers, having different results for each Owner.

注意:用于传输值/引用的类,可以在 SearchForm 的构造函数中传递,可能使用众所周知的合同(接口),它定义了值及其类型.此处描述的范围太广,但您应该考虑探索这种可能性.

Note: The class used to transfer the values/references, could be passed in the contructor of SearchForm, possibly using a well-known contract (an Interface), which defines the values and their types. Too broad to describe here, but you should consider exploring this possibility.

public partial class InvoiceForm : Form
{
    public class UpdateMyControls 
    {
        public string CodeText { get; set; }
        public string NameText { get; set; }
        public string BlahText { get; set; }
    }

    private void btnSearch_Click(object sender, EventArgs e)
    {
        var searcher = new SearchForm();
        searcher.Show(this);
    }

    public void UpdateControls(UpdateMyControls allValues)
    {
        this.CodeTextBox.Text = allValues.CodeText;
        this.NameTextBox.Text = allValues.NameText;
        this.BlahTextBox.Text = allValues.BlahText;
    }
}

public partial class SearchForm : Form
{
    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter) {
            if (this.Owner is InvoiceForm frm) {
                InvoiceForm.UpdateMyControls updateClass = new InvoiceForm.UpdateMyControls();

                updateClass.CodeText = sqldr[codecolumn].ToString();
                updateClass.NameText = sqldr[Namecolumn].ToString();
                updateClass.BlahText = sqldr[Blahcolumn].ToString();
                frm.UpdateControls(updateClass);
                this.Close();
            }
        }
    }
}

多参数公共方法:

public partial class InvoiceForm : Form
{
    private void btnSearch_Click(object sender, EventArgs e)
    {
        var searcher = new SearchForm();
        searcher.Show(this);
    }

    public void UpdateControls(string Code, string Name, string Blah)
    {
        this.CodeTextBox.Text = Code;
        this.NameTextBox.Text = Name;
        this.BlahTextBox.Text = Blah;
    }
}

public partial class SearchForm : Form
{
    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter) {
            string CodeValue = sqldr[codecolumn].ToString()
            string NameValue = sqldr[Namecolumn].Tostring
            string BlahValue = sqldr[Blahcolumn].Tostring 

            if (this.Owner is InvoiceForm frm) {
                frm.UpdateControls(CodeValue, NameValue, BlahValue);
                this.Close();
            }
        }
    }
}

这篇关于如何将文本从一个表单中的 DataGridView 传输到另一个表单中的私有 TextBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆