SQLite C# 和 DataGridView
如果我从另一个表单调用函数,尝试用数据填充 datagridview,我会遇到问题。如果我在 form2 中的 private void button1_Click 中调用函数,其中 datagridview 是 datagridview,但如果我从另一个表单(Form1)调用函数,则一切工作都很完美,datagridview 为空。
Form2 中的代码在哪里,datagridview
public void fill_grid() {
MessageBox.Show("Yuhuuuuuuu");
form_Listusers form = new form_Listusers();
SQLiteConnection cn = new SQLiteConnection(Form1.dbQuery);
cn.Open();
string SQL;
SQL = "SELECT users_id, name, username, place FROM users";
SQLiteCommand cmd = new SQLiteCommand(SQL, cn);
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
da.Fill(ds);
DataTable dt = ds.Tables[0];
this.grid_userlist.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show("Errrrrrrror");
}
cn.Close();
}
Form1 中的代码:
Form2 frm = new Form2();
frm.fill_grid();
我收到消息“Yuhuuuuuuuu”,但 datagridview 为空。
#已解决
#在主窗体(Form1)中,我设置了实例
Form frm_Listusers = new form_Listusers();
,但在此之后:
form_Listusers frm_Listusers = new form_Listusers();
我可以访问我的函数 frm.fill_grid();
I have problem if I try to fill datagridview with data if i call function from another form. Everything work perfect if i call function in private void button1_Click in form2 where is datagridview but if i call function from another form (Form1) datagridview is empty .
Code in Form2 where is datagridview
public void fill_grid() {
MessageBox.Show("Yuhuuuuuuu");
form_Listusers form = new form_Listusers();
SQLiteConnection cn = new SQLiteConnection(Form1.dbQuery);
cn.Open();
string SQL;
SQL = "SELECT users_id, name, username, place FROM users";
SQLiteCommand cmd = new SQLiteCommand(SQL, cn);
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
da.Fill(ds);
DataTable dt = ds.Tables[0];
this.grid_userlist.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show("Errrrrrrror");
}
cn.Close();
}
Code in Form1 :
Form2 frm = new Form2();
frm.fill_grid();
I got Message "Yuhuuuuuuuu" but datagridview is empty.
#
SOLVED
#
In Main Form (Form1) i set instance
Form frm_Listusers = new form_Listusers();
but after this:
form_Listusers frm_Listusers = new form_Listusers();
i can access to my function frm.fill_grid();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您运行以下代码时:
您只是创建了 Form2 的实例,但显示的 Form 是 Form1。
然后发生的情况是,您看到 Form1 的 DataGridView 您没有填充它,因为您在 Form2 中制作了它。
如果要在 Form1 上添加 Form2 的显示,只需在末尾添加以下行:
显示 Form2 的方法有多种,具体取决于您想要的选项(仅显示 Form2、显示两者等)。
关于您看到的消息,这是因为该行:
MessageBox.Show("Yuhuuuuuuu");
此事件针对所有应用程序触发,并且不依赖于现在显示的表单。
when you run following code:
You just create an instance of Form2, but the Form shown you is Form1.
Then what happens is you see the DataGridView of Form1 that you did not fill it because you made this in Form2.
If you want to add the display of Form2 addition to Form1, add only the following line at the end:
There are several ways to display the Form2, depending on the option you want (show only the Form2, show both, etc.).
About the message you see, it's because the line:
MessageBox.Show ("Yuhuuuuuuu");
This event fires for all the application and does not depend on which form is displayed now.
更改此 `this.grid_userlist.DataSource = dt;到 grid_userlist.DataSource = dt;
并从另一个表单中的第二个表单类创建实例,然后调用 form2 的方法
change this `this.grid_userlist.DataSource = dt; to grid_userlist.DataSource = dt;
and create instance from your second form class in another form then call methods of form2 for example