0

我有两个表格(表格 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 函数将字符串数组传回时,我似乎无法使用数据设置我的文本框。有人可以请,请帮助!

谢谢。

4

4 回答 4

0

您将数据表传递给 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
}

然后在 Form2

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
}
于 2012-08-15T18:58:06.130 回答
0

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

这是来自form1的代码:

    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);
        }

    }

来自 Form2

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; }
    }
}
于 2012-08-15T18:46:30.943 回答
0

简单的。在 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);
        }  
于 2012-08-15T19:02:10.433 回答
0

您是遇到错误还是数据没有显示。也许尝试ToString()方法中每个字符串数组索引后面的getcontactinfo方法。(即contactInfo[0].ToString();

于 2012-08-15T18:17:20.197 回答