简体   繁体   English

获取datagridview行数据到另一个窗体上的文本框

[英]getting datagridview row data to textboxes on another form

i have a datagridview that show my data in columns. 我有一个datagridview在列中显示我的数据。 what i'm trying to accomplish is that after choosing a row and pressing edit button a new form will open and split the row for the right text boxes to update the data. 我要完成的工作是,在选择一行并按下“编辑”按钮后,新表格将打开并拆分该行,以显示正确的文本框以更新数据。

the datagridview row shows different types of data: name,email,date,etc... datagridview行显示不同类型的数据:名称,电子邮件,日期等。

any idea? 任何想法? Thanks in advance! 提前致谢!

This site explains how to send data between forms, it would be as simple as selecting the right cell in the datagrid, sending that info off to the right textbox, for all of them. 该站点说明了如何在表单之间发送数据,就像在数据网格中选择正确的单元格,然后将所有信息发送到正确的文本框一样简单。 then sending them back. 然后将它们发回。 Data between forms 表格之间的数据

The basics are to create a method that can be use to get the value, 基础知识是创建一种可用于获取值的方法,

    public string getTextBoxValue()
{
    return TextBox.Text;
}

then you can just call the method to pass the data between the forms, 那么您可以调用方法在表单之间传递数据,

this.Text = myForm2.getTextBoxValue();

however you will be sending the values of the cells, and will be making a textbox.text equal to the return of the method this is a basic example of the theory, giev it a try yourself to make it work for what you want it to do, if you just cant do it come back and ask for help and ill edit with the code, but only after youve tried yourself first 但是,您将发送单元格的值,并将使textbox.text等于方法的返回值,这是该理论的一个基本示例,请尝试自己使其能够实现所需的功能如果您做不到,那就返回并寻求帮助,并对代码进行错误的编辑,但前提是您必须先尝试一下

You can create a class, say MyDataCollection, with properties corresponding to your DataGridView columns. 您可以创建一个类,例如MyDataCollection,其类具有与DataGridView列相对应的属性。 When you press the Edit button, create a new instance of this class, fill it with the necessary data and pass it as parameter to the EditForm's constructor. 当您按下“编辑”按钮时,创建该类的新实例,用必要的数据填充它,并将其作为参数传递给EditForm的构造函数。

public class MyDataCollection
{
    public string Name;
    public string Email;
    // -- 
}

In your main form: 在您的主要形式中:

void btnEdit_Click(object sender, EventArgs e)
{
    // Create the MyDataCollection instance and fill it with data from the DataGridView
    MyDataCollection myData = new MyDataCollection();
    myData.Name = myDataGridView.CurrentRow.Cells["Name"].Value.ToString();
    myData.Email = myDataGridView.CurrentRow.Cells["Email"].Value.ToString();
    // --

    // Send the MyDataCollection instance to the EditForm
    formEdit = new formEdit(myData);
    formEdit.ShowDialog(this);
}

And the edit form should look like this: 并且编辑表单应如下所示:

public partial class formEdit : Form
{
    // Define a MyDataCollection object to work with in **this** form
    MyDataCollection myData;

    public formEdit(MyDataCollection mdc)
    {
        InitializeComponent();

        // Get the MyDataCollection instance sent as parameter
        myData = mdc;
    }

    private void formEdit_Load(object sender, EventArgs e)
    {
        // and use it to show the data
        textbox1.Text = myData.Name;
        textbox2.Text = myData.Email;
        // --
    }
}

You can also forget about the MyDataCollection class and pass the entire DataGridViewRow to the formEdit's constructor. 您也可以忽略MyDataCollection类,并将整个DataGridViewRow传递给formEdit的构造函数。

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

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