在没有作用域的类方法中访问dataGridView



我想访问方法"GetApplicationName"内部的"dataGridView1",该方法本身位于类"ExternalApplication"内部。(类和方法名称不是描述性的)"v"将对应于"dataGridView1"中的一行,在将"v"从字符串强制转换为int之后,我想用它来选择并突出显示该数据视图中的行。但在该方法中,"dataGridView1"超出了作用域。我认为做上面的事情比做相反的事情更容易——试图使"v"相当于全局,这似乎需要更多的工作(?)不确定。迄今为止引用的代码。。。我试过很多东西,但都拿不到。

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;
using System.Runtime.InteropServices;
using MySql.Data.MySqlClient;
namespace houseDB1
{
    public partial class Form1 : Form
    {
        private string server;
        private string database;
        private string uid;
        private string password;
        private MySqlConnection connection;

        public Form1()
        {
            InitializeComponent();
            webBrowser1.ObjectForScripting = new ExternalApplication();
        }
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate("127.0.0.1/box3.php");
            server = "localhost";
            database = "realestate_db";
            uid = "root";
            password = "";
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
            connection = new MySqlConnection(connectionString);
            connection.Open();
            MySqlDataAdapter mySqlDataAdapter;
            mySqlDataAdapter = new MySqlDataAdapter("SELECT `ID`, `lat` , `long` FROM `house` ", connection);
            DataSet DS = new DataSet();
            mySqlDataAdapter.Fill(DS);
            dataGridView1.DataSource = DS.Tables[0];
        }
        private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
        }
        [ComVisible(true)]
        public class ExternalApplication
        {
            public void GetApplicationName(string v) // getting values from webbrowser
            {
                MessageBox.Show("ZZTOP" + v);
                //return "The application";
            }
        }
        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        }
    }
}

您可以将dataGridView1的引用传递给GetApplicationName方法。

最新更新