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

时间:2012-08-15 17:58:37

标签: 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 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 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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; }
    }
}

答案 2 :(得分: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
}

答案 3 :(得分: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);
        }