SQLite C#和DataGridView [英] SQLite C# and DataGridView
问题描述
Form2中的代码是datagridview
public void fill_grid(){
MessageBox.Show(Yuhuuuuuuuuu);
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(Errrrrrrrror);
}
cn.Close();
}
Form1中的代码:
Form2 frm = new Form2();
frm.fill_grid();
我收到消息Yuhuuuuuuuuuuu,但datagridview是空的。
#
SOLVED
#
主窗体(Form1)我设置实例
表单frm_Listusers = new form_Listusers();
但此后:
form_Listusers frm_Listusers = new form_Listusers();
我可以访问我的函数frm.fill_grid();
当您运行以下代码:
code> form2 frm = new Form2();
frm.fill_grid();
您只需创建一个Form2的实例,但窗体显示为Form1。
那么会发生什么,你看到Form1的DataGridView你没有填写它,因为你在Form2中这样做。
如果你要添加显示Form2除了Form1之外,仅在下面添加一行:
frm.Show();
有几种方法来显示Form2,具体取决于所需的选项(仅显示Form2,显示两个等)。
关于你看到的消息,这是因为行:
MessageBox.Show(Yuhuuuuuuuuu);
此事件触发所有应用程序,而不依赖于现在显示的表单。
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();
when you run following code:
Form2 frm = new Form2();
frm.fill_grid();
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:
frm.Show();
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.
这篇关于SQLite C#和DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!