如何将文本从一种形式的datagridview传输到另一种形式的私有文本框 [英] How can I transfer text from a datagridview in one form to a private textbox in another form

查看:71
本文介绍了如何将文本从一种形式的datagridview传输到另一种形式的私有文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[摘要]
我的C#程序中有一个发票表格,其中包含几个私有的 TextBoxes 。当用户更改这些文本框的文本时,会出现搜索表单。我想将某些值从搜索表单的dataGridView(链接到数据库)转移到 Invoice表单的那些文本框。 code>(例如,当我按Enter键时。)。

[Summary] I have an Invoice form with several private TextBoxes in my C# 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 those TextBoxes in the Invoice form(when I press Enter for example).

[说明]
在发票表单中,我有商品代码文本框和商品名称文本框。当用户更改这些文本框的文本时,将出现搜索表单,并且输入的文本将被传输到searchform的文本框中。搜索表单只有一个文本框和一个datagridview。在搜索表单的文本框中输入内容后,datagridview将搜索并在datagridview中显示结果。 datagridview具有作为发票表单文本框的可重复列。现在,我要设法做到这一点:当我在searchform上输入内容时,datagridview当前行的信息将被转移到Invoice表单中的可重复文本框中。商品代码列到商品代码文本框,商品名称列到商品名称文本框。

[Description] In my Invoice form I have merchendise code textbox and merchandise name text box. When the user changes the text of those textboxes, the search form appears and the entered text will be transfered to the textbox of searchform. The searchform only has a textbox and a datagridview. When something is entered in the text box of search form the datagridview will search and show the results in the datagridview. The datagridview has reletive columns as Invoice form's textboxs . Now I want to manage to do this: when I enter on searchform, the information of the current row of the datagridview be transfered to the reletive 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 sais:


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

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

让我们假设我不想将这些文本框的保护级别更改为公开。

Lets assume I dont 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:

Form2 form2 = new Form2(this);
form2.Show();

使用被调用方的所有者属性( Form2 )。使用显示(所有者) ShowDialog(Owner)方法。是调用者的实例:

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:

Form2 form2 = new Form2();
form2.Show(this);

您也可以在被叫方中拥有一个公共财产( Form2 ),用于设置当前呼叫者():

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

Form2 form2 = new Form2();
form2.MyCaller = this;
form2.Show();

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

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 表单)使用该方法传回用户选择的值。

使用 Form.Show(this)还意味着显示的表单将是 parented (不要与 Parent 属性,但带有显示它并保留在其顶部的窗体。

您也可以使用 ShowDialog(this)方法(如果您的情况更可取)。在这种情况下,表单将显示为模式对话框。

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.
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 preferrable 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是InvoiceForm frm 用于标识当前的所有者

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)
    {
        SearchForm 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)
    {
        SearchForm 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传输到另一种形式的私有文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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