![](/img/trans.png)
[英]not able to set the visible property of button in one form from another form
[英]Not able to hold string from one form to other
能够在执行c.Show()时显示数据库。 当我关闭表格2并单击button6时,表格2上的gridview为空。 任何想法如何解决此错误?
表格1:
private void System_btn_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Bitmap picture = new Bitmap(openFileDialog1.FileName);
ZoneStatus c = new ZoneStatus();
c.dbname = System.IO.Path.GetFileNameWithoutExtension(openFileDialog1.SafeFileName);
//c.Show();
}
}
private void button6_Click(object sender, EventArgs e)
{
ZoneStatus zoneStatus_form = new ZoneStatus();
zoneStatus_form.Show();
}
表格2:
public string dbconnection;
public string dbname {get;set;}
private void ZoneStatus_Load(object sender, EventArgs e)
{
dbconnection = @"Data Source=" + dbname + ".db;Version=3;";
SQLiteConnection sqliteCon = new SQLiteConnection(dbconnection);
{
sqliteCon.Open();
// Create new DataAdapter
using (SQLiteDataAdapter a = new SQLiteDataAdapter(
"SELECT * FROM Alarm_Info", sqliteCon))
{
// Use DataAdapter to fill DataTable
DataTable dt = new DataTable();
a.Fill(dt);
dataGridView1.DataSource = dt; // to update my database
}
sqliteCon.Close();
}
}
将“公共字符串dbname”更改为“公共静态字符串dbname”。(如果您也想使用该变量,则与dbconnection相同。)假设您已经在from1中声明了您的字符串,并且想要在form2中使用此字符串,因此您的代码会像这样。
形式1:
public static string dbname;
在form2中,创建form1的对象并像这样访问此变量。
形式2:
form1 objf1 = new form1();
string str=objf1.dbname;
现在,您可以使用变量dbname进行任何操作。 您可以像我一样分配给其他变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.