C# 尝试从表单2中的数据表中获取信息,并使用C在表单1中设置文本框#

C# 尝试从表单2中的数据表中获取信息,并使用C在表单1中设置文本框#,c#,sql,forms,textbox,dialog,C#,Sql,Forms,Textbox,Dialog,我有两个表单(表单1和表单2),通过在对话框中填充数据网格视图,我成功地将数据表从表单1传递到表单2。我还有一个事件处理程序来捕获选定行上的双击事件。当事件发生时,我想从表单2中的单击事件设置表单1中的文本框。无论我尝试什么,我似乎都无法显示文本框中的文本。下面是我的代码: //Code begins here //....Function to fill data table from form 1 and pass to form 2 private void but

我有两个表单(表单1和表单2),通过在对话框中填充数据网格视图,我成功地将数据表从表单1传递到表单2。我还有一个事件处理程序来捕获选定行上的双击事件。当事件发生时,我想从表单2中的单击事件设置表单1中的文本框。无论我尝试什么,我似乎都无法显示文本框中的文本。下面是我的代码:

    //Code begins here
    //....Function to fill data table from form 1 and pass to form 2
    private void buttonNewEntryLookUp_Click(object sender, EventArgs e)
    {

        try
        {
            cs.Open();
            da.SelectCommand = new SqlCommand("Select ctx_customername AS Customer, ctx_contactname AS Contact, ctx_custaddress1 AS Address, ctx_custcity AS City, ctx_custstate AS State, nno_custzip AS ZIP, ctx_custemail AS Email FROM Customers WHERE nno_custphone = '" + maskedTextBoxNewLogTel.Text + "'", cs);
            dt.Clear();
            da.Fill(dt);
        }
        catch
        {
            MessageBox.Show("Connection to Database could not be established, please close this application and try again. If problem continues please contact server Admin. Thank you.", "AAMP");
            //Display this message if connection could not be made
        }
        cs.Close();//close connection to db
        if (dt.Rows.Count == 0)//if there are no returned results then this must be a new entry into the database
        {
            MessageBox.Show("Phone Number Not Found in Database.", "AAMP");
        }
        else//number was found
        {
            Form2 form2 = new Form2(dt);//create object of form 2 and pass the data table.
            form2.ShowDialog();//show form 2 with data table in the grid view.

        }
    }
    public void getContactInfo(string[] contactInfo)
    {
        textBoxNewLogCustomerName.Text = contactInfo[0];
        textBoxNewLogContactName.Text = contactInfo[1];
        textBoxNewLogAddress.Text = contactInfo[2];
        textBoxNewLogCity.Text = contactInfo[3];
        textBoxNewLogState.Text = contactInfo[4];
        textBoxNewLogZIP.Text = contactInfo[5];
        textBoxNewLogEmail.Text = contactInfo[6];
    }

    //code for form 2
    public partial class Form2 : Form
{  
    /*Globals for Form 2*/
    DataTable g_dt;
    public Form2(DataTable dt)
    {
        InitializeComponent();
        dataGridViewLookUp.DataSource = dt;
        g_dt = dt;
    }

    private void dataGridViewLookUp_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        Form1 form1 = new Form1();
        string[] contactInfo = new string[7];
        contactInfo[0] = Convert.ToString(g_dt.Rows[e.RowIndex]["Customer"]);
        contactInfo[1] = Convert.ToString(g_dt.Rows[e.RowIndex]["Contact"]);
        contactInfo[2] = Convert.ToString(g_dt.Rows[e.RowIndex]["Address"]);
        contactInfo[3] = Convert.ToString(g_dt.Rows[e.RowIndex]["City"]);
        contactInfo[4] = Convert.ToString(g_dt.Rows[e.RowIndex]["State"]);
        contactInfo[5] = Convert.ToString(g_dt.Rows[e.RowIndex]["ZIP"]);
        contactInfo[6] = Convert.ToString(g_dt.Rows[e.RowIndex]["Email"]);
        form1.getContactInfo(contactInfo);//return the row number being clicked.
        this.Close();

    }
}
我成功地将数据表传递到表单2,并捕获正确的信息以填充字符串数组,但当我通过调用getContactInfo函数将字符串数组传递回来时,我似乎无法使用数据设置文本框。谁能帮帮我吗


谢谢

您是否遇到错误,或者数据只是没有显示出来。可以在
getcontactinfo
方法中的每个stringarray索引后面尝试一个
ToString()
方法。(例如,
contactInfo[0].ToString();

您的OP有点模糊,但我有一些代码可以做到这一点,在表单2中获取一些数据并将其发送回表单1

以下是表格1中的代码:

    private void btnGroupNameLookup_Click(object sender, EventArgs e)
    {
        //instantiate an instance of the grp name lookup form
        frmGroupNameLookup lookupName = new frmGroupNameLookup();
        //add an event handler to update THIS form when the lookup
        //form is updated. (This is when LookupUpdated fires
        lookupName.GroupNamesFound += new frmGroupNameLookup.LookupHandler(lookupName_GroupNamesFound);
        //rc.ReconCFUpdated += new ReconCaseFileChecklist.ReconCFListHandler(ReconCFForm_ButtonClicked);

        lookupName.Show();

    }

    void lookupName_GroupNamesFound(object sender, GroupNameLookupUpdateEventArgs e)
    {
        //update the list boxes here            
        foreach (string s in e.Parents)
        {
            lstFilteredGroupParents.Items.Add(s);
        }

        foreach (string s in e.Groups)
        {
            lstFilteredGroups.Items.Add(s);
            //link supgroups and plan ids
            GetFilteredSubgroupNos(s);
            GetFilteredPlanIds(s);
        }

        //ensure dupes are stripped out
        //filter out duplicates 
        var noDupeSubgroups = subgroupList.Distinct().ToList();
        noDupeSubgroups.Sort();

        foreach (string s in noDupeSubgroups)
        {
            lstFilteredSubgroups.Items.Add(s);
        }

        var noDupePlanIDs = planIDList.Distinct().ToList();
        noDupePlanIDs.Sort();

        foreach (string s in noDupePlanIDs)
        {
            lstFilteredPlanID.Items.Add(s);
        }

    }
来自表格2

public partial class frmGroupNameLookup : Form
{
    //add a delegate, the GroupNameLookupUpdateEventArgs class is defined at the bottom
    //of this file
    public delegate void LookupHandler(object sender, GroupNameLookupUpdateEventArgs e);

    //add an event of the delegate type
    public event LookupHandler GroupNamesFound;

    //this event closes the forms and passes 2 lists back to form 1
    private void btnCommit_Click(object sender, EventArgs e)
    {
        List<string> prnt = new List<string>();
        List<string> grp = new List<string>();
        //get selected rows
        if (grdLookup.SelectedRows.Count > 0)
        {

            foreach (DataGridViewRow row in grdLookup.SelectedRows)
            {
                prnt.Add(row.Cells[0].Value.ToString());
                grp.Add(row.Cells[1].Value.ToString());
            }

            //filter out duplicates 
            var noDupeParentGroups = prnt.Distinct().ToList();
            noDupeParentGroups.Sort();
            // instance the event args and pass it each value
            GroupNameLookupUpdateEventArgs args =
                new GroupNameLookupUpdateEventArgs(noDupeParentGroups, grp);

            // raise the event with the updated arguments
            this.GroupNamesFound(this, args);

            this.Dispose();
        }
    }
}

public class GroupNameLookupUpdateEventArgs : System.EventArgs
{
    // add local member variables to hold text
    private List<string> mParents = new List<string>();
    private List<string> mGroups = new List<string>();

    // class constructor
    public GroupNameLookupUpdateEventArgs(List<string> sParents, List<string> sGroups)
    {
        this.mParents = sParents;
        this.mGroups = sGroups;
    }

    // Properties - Viewable by each listener
    public List<string> Parents
    {
        get { return mParents; }
    }

    public List<string> Groups
    {
        get { return mGroups; }
    }
}
公共部分类frmGroupNameLookup:表单
{
//添加一个委托,GroupNameLookupUpdateEventArgs类在底部定义
//这个文件的
公共委托void LookupHandler(对象发送方,GroupNameLookupUpdateEventArgs e);
//添加委托类型的事件
找到公共事件查找处理程序GroupNames;
//此事件关闭表单并将2个列表传递回表单1
私有无效btnCommit\u单击(对象发送者,事件参数e)
{
List prnt=新列表();
List grp=新列表();
//获取所选行
如果(grdLookup.SelectedRows.Count>0)
{
foreach(grdLookup.SelectedRows中的DataGridViewRow行)
{
添加(row.Cells[0].Value.ToString());
grp.Add(row.Cells[1].Value.ToString());
}
//滤除重复项
var noDupeParentGroups=prnt.Distinct().ToList();
noDupeParentGroups.Sort();
//实例事件args并将每个值传递给它
GroupNameLookupUpdateEventArgs参数=
新组名LookupUpdateEventArgs(noDupeParentGroups,grp);
//使用更新的参数引发事件
this.GroupNamesFound(this,args);
这个。Dispose();
}
}
}
公共类GroupNameLookupUpdateEventArgs:System.EventArgs
{
//添加本地成员变量以保存文本
私有列表mParents=新列表();
私有列表管理组=新列表();
//类构造函数
公共组名称LookupUpdateEventArgs(列表备用项、列表sGroups)
{
这个。mParents=sParents;
this.mGroups=sGroups;
}
//属性-可由每个侦听器查看
公开名单父母
{
获取{return mParents;}
}
公共列表组
{
获取{return mGroups;}
}
}

您正在将数据表传递到Form2的构造函数中:

Form2 form2 = new Form2(dt);
您可以改为传递对整个Form1的引用:

Form2 form2 = new Form2(this);
然后在Form1上公开Form2可以用来更新数据的方法。大概是这样的:

//In Form1
public DataTable getDataTable(){
  //return datatable
}

public void setTextBoxValue(string Value){
  //Set the value
}
然后在表格2中

private Form1 _form1;

public Form2(Form1 form1){
  _form1 = form1;
  dataGridViewLookUp.DataSource = _form1.getDataTable();
  g_dt = dt;
}

private void dataGridViewLookUp_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
  _form1.setTextBoxValue(Convert.ToString(g_dt.Rows[e.RowIndex]["Customer"]));
  //etc
}

简单。将联系人信息设置为Form2中的公共方法,然后在form1中的ShowDialog()之后分配它

else//number was found  
        {  
            Form2 form2 = new Form2(dt);//create object of form 2 and pass the data table.  
            form2.ShowDialog();//show form 2 with data table in the grid view.  
            getContactInfo(form2.ContactInfoFromForm2);
        }  

我没有收到错误,我尝试了您所说的似乎没有什么不同。请尝试在form2中创建pulic string[]属性,然后在form1中调用该方法来填充文本框。但是,只需访问form2中的public方法,而不是传递的参数。假设表单仍然是可访问的。并且只让方法返回带有数据表中数据的字符串数组?你能给我举个例子吗?如果Form2是一个模式对话框,Form1保持打开状态,你最好从Form2更新Form1。在本例中,当您调用getContactInfo()时,您不能保证Form2已完成加载、单元格已被单击或用户尚未关闭Form2