将数据表从一个表单传递到另一个表单 [英] Passing a datatable from one form to another form

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

问题描述

Quote:



我有一个form1和form2。 form2是从form1打开的,填充后它有一个dgv,它在一个单击按钮上被复制到一个数据表,而form2被关闭或者可见= false,我将数据表传递给了form1但是它给了我一个空引用:这是我的代码in form2:


i have a form1 and form2 . form2 is opened from form1 , it has a dgv after filling it, its copied to a datatable on a click button and form2 is closed or visible = false, i passed the datatable to form1 but its giving me a null reference: here is my code in form2:




private void button1_Click(object sender, EventArgs e)
        {
            perclothesdescriptionDT = new DataTable();

            foreach (DataGridViewColumn col in dataGridView3.Columns)
            {
                perclothesdescriptionDT.Columns.Add(col.HeaderText);
            }

            foreach (DataGridViewRow row in dataGridView3.Rows)
            {
                DataRow dRow = perclothesdescriptionDT.NewRow();
                foreach (DataGridViewCell cell in row.Cells)
                {
                    dRow[cell.ColumnIndex] = cell.Value;
                }
                perclothesdescriptionDT.Columns.RemoveAt(0);
                perclothesdescriptionDT.Rows.Add(dRow);
            }
            this.Visible = false;

        }

        public DataTable mydt
        {
            get
            {
                return perclothesdescriptionDT;
            }
        }





表格1:





In form1 :

form2= new Form2();

                cmd1.CommandText = "insert into [dbo].[personClothesDesc](upperPart, lowerPart, belt, socks, shoes, differentSigns) values (@upperPart, @lowerPart, @belt, @socks, @shoes, @differentSigns)";
                        for (int i = 0; i < form2.mydt.Rows.Count; i++)
                        {
                            cmd1.Parameters.Clear();
                            cmd1.Parameters.AddWithValue("@upperPart", form2.mydt.Rows[i].ItemArray.GetValue(5).ToString());
                            cmd1.Parameters.AddWithValue("@lowerPart", form2.mydt.Rows[i].ItemArray.GetValue(4).ToString());
                            cmd1.Parameters.AddWithValue("@belt", form2.mydt.Rows[i].ItemArray.GetValue(3).ToString());
                            cmd1.Parameters.AddWithValue("@socks", form2.mydt.Rows[i].ItemArray.GetValue(2).ToString());
                            cmd1.Parameters.AddWithValue("@shoes", form2.mydt.Rows[i].ItemArray.GetValue(1).ToString());
                            cmd1.Parameters.AddWithValue("@differentSigns", form2.mydt.Rows[i].ItemArray.GetValue(0).ToString());
                            cmd1.ExecuteNonQuery();
                        }





我的尝试:



i必须尝试将数据表从form2传递给form1,但它给我空引用:

public DataTable mydt

{

get

{

return perclothesdescriptionDT;

}

}



What I have tried:

i have to tried to pass the datatable from form2 to form1 but its giving me null reference :
public DataTable mydt
{
get
{
return perclothesdescriptionDT;
}
}

推荐答案

是的......它会。

您创建Form2的实例,并立即使用该属性:

Well yes...it will.
You create the instance of Form2, and immediately use the property:
form2= new Form2();
cmd1.CommandText = ...
for (int i = 0; i < form2.mydt.Rows.Count; i++)
{
    cmd1.Parameters.Clear();
    cmd1.Parameters.AddWithValue("@upperPart",
         form2.mydt.Rows[i].ItemArray.GetValue(5).ToString());
    ...



但是你的Form2代码显示只有当用户点击按钮时才填充数据库。

既然你甚至不向用户显示表单,更不用说给他时间按下按钮,数据表永远不会创建,并且属性只能返回null。

可能你要做的就是访问您在其他地方创建并显示的现有屏幕上的Form2实例 - 在这种情况下,您需要将实例存储在Form1中的类级别变量中,并使用该实例中的属性而不是创建新实例。


But your code for Form2 shows the database only being filled when the user clicks a button.
Since you don't even show the form to the user, much less give him time to press a button the datatable is never created and the property can only return null.
Probably what you are trying to do is access the existing on-screen instance of Form2 that you created and displayed elsewhere - in which case you need to have stored the instance in a class level variable in Form1 and use the property from that instance rather than creating a new one.


您好请参考以下链接,它将对您有所帮助。



如何在Windows窗体应用程序中将一个表单传递给另一个表单 [ ^ ]
Hi Please refer below link it will help to you.

How to Pass Data One Form to Another in Windows Form Application[^]


我建​​议:在第二个Form中使用DataGridView



定义返回DataTable的公共行动(委托):
I suggest: in the second Form with the DataGridView

define a Public Action (delegate) that returns a DataTable:
public Action<DataTable> SendDataTable;

在第二个Form上的Button的Click EventHandler中,在构造DataTable之后,然后使用DataTable作为参数值调用委托(Action):

In the Click EventHandler for the Button on the second Form, after the DataTable is constructed, then invoke the delegate (Action) with the DataTable as the parameter value:

private void btnTransmitDataTable_Click(object sender, EventArgs e)
{
    // build the DataTable

    // invoke the Action delegate
    if(SendDataTable != null) SendDataTable(perclothesdescriptionDT);
}

在主窗体中(使用DataGridView创建窗体):

In the Main Form (that creates the Form with the DataGridView):

// in Main Form scope
Form2 form2;

// in an appropriate EventHandler ... Form_Load ?
form2= new Form2();
form2.SendDataTable += HandleDztaTableResult;

private void HandleDataTableresult(DataTable dataTable)
{
    // do whatever with the data

    // Dispose, Close, or Hide secondary Form ?
}

有很多种方式你可以解决这种形式到形式的数据传输问题。例如,您可以创建DataTable的实例并将其注入Form2的实例中;您可以通过引用将DataTable的实例作为参数传递给辅助表单中的方法等。



OriginalGriff有三篇关于不同文章的优秀系列文章将信息从形式转移到形式的方法;我建议你读一下;这里是第一个链接,它链接到系列中的另外两个:[ ^ ]。

There are many ways you could address this issue of form-to-form data transfer. You could, for example, create the instance of the DataTable and inject it into the instance of Form2; you could pass in the instance of the DataTable by reference as a parameter to a method in the secondary Form, etc.

OriginalGriff has an excellent series of three articles on different approaches to transferring information from form to form; I suggest you read them; here's a link to the first, which has links to the other two in the series: [^].


这篇关于将数据表从一个表单传递到另一个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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