【问题标题】:Access dataGrid information from another user control从另一个用户控件访问 dataGrid 信息
【发布时间】:2020-07-07 09:43:44
【问题描述】:

我在 Form.cs 上设置了两个用户控件。尝试创建现代 UI。第一个用户控件 (database.cs) 将数据库加载到 dataGridView 上。然后,用户可以使用此用户控制 (database.cs) 文件编辑数据库。现在我还希望能够从第二个用户控件 (scan.cs) 访问 dataGridView。

form1.cs 代码:

public partial class Form1 : Form
{
    public static string db_n;
    public static string db_p;

    private void Form1_Load(object sender, EventArgs e)
    {
        var userdata = WelcomeForm.Run();
        if (userdata != null)
        {
            db_n = userdata.UserName;
            db_p = userdata.UserSurname;
        }
    }

    public Form1()
    {
        InitializeComponent();
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        scan1.BringToFront();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        
        database1.BringToFront();
    }
  }
}

database.cs 代码:

public partial class Database : UserControl
{
    
    public Database()
    {
        InitializeComponent();
    }

    public void load_db_Click(object sender, EventArgs e)
    {
        using (var connection = new MySqlConnection("server=localhost;user id='" + Form1.db_n + "';database=db;password='" + Form1.db_p + "'"))
        {
            try
            {
                if (connection.State == ConnectionState.Closed)
                {
                    connection.Open();
                    DataTable table = new DataTable();
                    MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM table_new", 
                    connection);
                    adapter.Fill(table);
                    dataGridView1.DataSource = table;
                }

            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }
        }
    }
}

然后我将如何访问 scan.cs 中的 dataGridView1?

【问题讨论】:

    标签: c# visual-studio user-controls


    【解决方案1】:

    您想从另一个用户控件访问该控件吗?如果有,可以参考下面的demo。

    首先,我们可以通过Property获取datagridview实例。

    Form1.cs

    public Form1 f1;
    
    public Form1()
    {
        InitializeComponent();
        // Get the instance of this form
        f1 = this;
    }
    
    public DataGridView dataGridView { get; set; }
    
    private void Form1_Load(object sender, EventArgs e)
    {
        UCdatabase uCdatabase = new UCdatabase();
        uCdatabase.Location = new Point(0, 0);
        UCscan UCscan = new UCscan(f1);
        UCscan.Location = new Point(310, 0);
        // Save the DGV instance to dataGridView 
        dataGridView = uCdatabase.DGV;
    
        Controls.Add(uCdatabase);
        Controls.Add(UCscan);
    }
    

    UCdatabase.cs

    public partial class UCdatabase : UserControl
    {
        public UCdatabase()
        {
            InitializeComponent();
        }
    
        public DataGridView DGV 
        {
            get { return dataGridView1; }
        }
    }
    

    然后通过构造函数中的参数将datagridview实例传递给Ucscan。

    UCscan.cs

    public partial class UCscan : UserControl
    {
        public Form1 form1;
    
        public UCscan(Form1 f1)
        {
            InitializeComponent();
            form1 = f1;
        }
    
        private void btAccess_Click(object sender, EventArgs e)
        {
            // Access the DGV via "form1.dataGridView"
            label1.Text = form1.dataGridView.Rows[0].Cells[0].Value.ToString();
        }
    }
    

    在这个演示中,我们可以从 datagridview 中获取第一个单元格的值。

    这是测试结果。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 2013-01-19
    相关资源
    最近更新 更多