0
我想在「ExternalApplication」類內部的方法「GetApplicationName」中訪問「dataGridView1」。 (類和方法名稱不是描述性的)「v」將對應於「dataGridView1」中的行,將「v」從字符串轉換爲int後,我想用它來選擇並突出顯示該數據視圖中的一行。但是在那個方法裏「dataGridView1」超出了範圍。我認爲做上面的事情比較簡單,然後恰恰相反 - 試圖讓「v」變成全球的似乎需要更多工作的等價物(?)不確定。所引用的代碼迄今爲止工作...我嘗試了很多東西,但無法得到它。在類方法中訪問dataGridView它沒有範圍的地方
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)
{
}
}
}
如果我這樣做:public void GetApplicationName(string v,DataGrid dataGridView1)這是你的意思嗎?這會導致運行時錯誤「無效的過程調用或參數」。該呼叫來自網頁 - 對數據網格一無所知。對不起,如果我不知道足夠的C#來執行你的代碼... – user1325143 2014-09-29 08:21:54
嘗試使'dataGridView1'成爲一個靜態成員,然後直接從嵌套類訪問'dataGridView1'。如果這不起作用,則將外部類的實例傳遞給嵌套類,然後使用該實例訪問嵌套類內的外部類的成員。 – 2014-09-30 23:57:02