0

我有一个主窗体名称 fmMain

图片

黑标是显示datagridview的浏览文件路径

红色标记是向datagridview显示表格。

我尝试将路径发送到 datagridview 并成功。这是代码

namespace tstIniF
{
    public partial class fmMain : Form
    {
        string ConfigFileName = "app.cfg";

        CFileConfig cFileConfig;

        public fmMain()
        {
            InitializeComponent();
            cFileConfig = new CFileConfig();

        }



        private void btnQuit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnDirectort_Click(object sender, EventArgs e)
        {
            if (dlgFolder.ShowDialog() != DialogResult.OK) return;
            string s = dlgFolder.SelectedPath;
            txtDirectory.Text = s;


            /*p = (string)dgvConfigFile.Rows[idx++].Cells[1].Value; cFileConfig.cfgContourFile = p;
            p = (string)dgvConfigFile.Rows[idx++].Cells[1].Value; cFileConfig.cfgConnectionString = p;*/
        }



        private void btnDirectBase_Click(object sender, EventArgs e)
        {
            if (dlgFile.ShowDialog() != DialogResult.OK) return;
            string s = dlgFile.FileName;
            int idx = 0;
            dgvConfigFile.Rows[idx++].Cells[1].Value = cFileConfig.cfgBaseMapFile = s;
        }

        private void btnDirectCont_Click(object sender, EventArgs e)
        {
            if (dlgFile.ShowDialog() != DialogResult.OK) return;
            string s = dlgFile.FileName;
            int idx = 1;
            dgvConfigFile.Rows[idx++].Cells[1].Value = cFileConfig.cfgContourFile = s;
        }

        private void btnDirectConn_Click(object sender, EventArgs e)
        {
            fConn op = new fConn();

            op.ShowDialog();
        }
}
}

红色标记为 btnDirectConn 我显示这样的新表格

图片2

这是我的表格 fConn

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace tstIniF
{
    public partial class fConn : Form
    {
        public fConn()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtServ.Text.Trim() == "" || txtDb.Text.Trim() == "" || txtUid.Text.Trim() == "" || txtPwd.Text.Trim() == "")
            {
                MessageBox.Show("Mohon diisi semua field....");
            }
            else
            {
                //string textAll = this.txtServ.Text + this.txtDb.Text + this.txtUid.Text + this.txtPwd.Text;
                fmMain frm = new fmMain();
                frm._textBox = _textBox1;
                this.Close();
                //Close();
                //frm.Show();
            }
        }

        public string _textBox1
        {
            get { return txtServ.Text + txtDb.Text; }
        }
    }
}

问题是如何将 fConn 表单中的数据显示到 fmMain datagridview ,我填写 fConn 条目并关闭并返回 fmMain 所以结果是

图3

4

2 回答 2

1

我会用委托来处理这个,

更改fConn表格如下

public partial class fConn : Form
{
    public SaveDelegate SaveCallback;

    public fConn()
    {
        InitializeComponent();
    }

    public void btnSave_Click(object sender, EventArgs e)
    {
        SaveCallback("set text what you need to send to main form here...");
    }
}

如下fmMain所示

公共委托无效 SaveDelegate(字符串文本);

public partial class fmMain
{
    public fmMain()
    {
        InitializeComponent();
    }

    private void btnDirectConn_Click(object sender, EventArgs e)
    {
        fConn op = new fConn();

        op.SaveCallback += new SaveDelegate(this.SavemCallback);
        op.ShowDialog();
    }

    private void SavemCallback(string text)
    {
        // you have text from fConn here ....
    }
于 2013-09-05T03:26:19.207 回答
0

在 frmMain 中,在类级别声明 fConn 表单,以便在表单关闭时不会处理它。现在 frmMain 可以访问 fConn 中的任何公共对象。

不要在 fConn 中重新声明 frmMain。_textBox = op._textBox1之后立即使用op.ShowDialog();

于 2013-09-05T03:05:15.763 回答