C# 从另一个类向窗体上的gridView添加数据
如何从另一个类向窗体上的dataGridView追加数据 这是一节课:C# 从另一个类向窗体上的gridView添加数据,c#,.net,winforms,datagridview,C#,.net,Winforms,Datagridview,如何从另一个类向窗体上的dataGridView追加数据 这是一节课: class TermSh { public HttpWebRequest request_get_page_with_captcha; public HttpWebResponse response_get_page_with_captcha; public string url; public Form1 form1; public
class TermSh
{
public HttpWebRequest request_get_page_with_captcha;
public HttpWebResponse response_get_page_with_captcha;
public string url;
public Form1 form1;
public BindingSource bindingSource1 = new BindingSource();
public int id = 0;
public TermSh(Form1 form1)
{
this.form1 = form1;
form1.dataGridView1.DataSource = bindingSource1;
}
public void somemethod()
{
try
{
cookies += response_get_page_with_captcha.Headers["Set-Cookie"];
bindingSource1.Add(new Log(id++, DateTime.Now, cookies));
form1.dataGridView1.Update();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
及表格类别:
TermSh term_sh = new TermSh(this);
term_sh.somemethod();
我做错了什么?为什么我的datagridview在代码执行后是空的,但通过调试,我看到bindingSource1不是空的。
如何添加数据?我认为,你实现目标的方式是不正确的。 首先,我认为从一个班传给另一个班是非常糟糕的。然后,您可以简单地操作一个列表并返回值,然后在表单中使用这个值(列表) 我认为这样做更好: [EDIT 1]下面的这个类是您的ptimary类,它有一个方法,这个方法返回一个新日志,您可以将这个返回值添加到表单1中的datagridview中
class TermSh
{
public HttpWebRequest request_get_page_with_captcha;
public HttpWebResponse response_get_page_with_captcha;
public string url;
public int id = 0;
public List<Log> somemethod()
{
try
{
cookies += response_get_page_with_captcha.Headers["Set-Cookie"];
return new Log(id++, DateTime.Now, cookies); //use this return value in your Form and update datagridview
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
[编辑3]并在Form1中使用此代码,使用类TermSh的返回值,并填充dataGridView:
TermSh term_sh = new TermSh(city, type, null, null);
logList.Add(term_sh.getPageWithCaptchaConnection());
logBindingSource.DataSource = logList;
logBindingSource.ResetBindings(false);
[EDIT 4]因此,如果您有一个问题:“如何在bindingSource中将此类用作集合?”。这很简单,您可以用对象填充dataGridView:这很有帮助。我认为,实现目标的方式是不正确的。 首先,我认为从一个班传给另一个班是非常糟糕的。然后,您可以简单地操作一个列表并返回值,然后在表单中使用这个值(列表) 我认为这样做更好: [EDIT 1]下面的这个类是您的ptimary类,它有一个方法,这个方法返回一个新日志,您可以将这个返回值添加到表单1中的datagridview中
class TermSh
{
public HttpWebRequest request_get_page_with_captcha;
public HttpWebResponse response_get_page_with_captcha;
public string url;
public int id = 0;
public List<Log> somemethod()
{
try
{
cookies += response_get_page_with_captcha.Headers["Set-Cookie"];
return new Log(id++, DateTime.Now, cookies); //use this return value in your Form and update datagridview
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
[编辑3]并在Form1中使用此代码,使用类TermSh的返回值,并填充dataGridView:
TermSh term_sh = new TermSh(city, type, null, null);
logList.Add(term_sh.getPageWithCaptchaConnection());
logBindingSource.DataSource = logList;
logBindingSource.ResetBindings(false);
[EDIT 4]因此,如果您有一个问题:“如何在bindingSource中将此类用作集合?”。这很简单,您可以用对象填充dataGridView:这很有帮助。我认为,实现目标的方式是不正确的。 首先,我认为从一个班传给另一个班是非常糟糕的。然后,您可以简单地操作一个列表并返回值,然后在表单中使用这个值(列表) 我认为这样做更好: [EDIT 1]下面的这个类是您的ptimary类,它有一个方法,这个方法返回一个新日志,您可以将这个返回值添加到表单1中的datagridview中
class TermSh
{
public HttpWebRequest request_get_page_with_captcha;
public HttpWebResponse response_get_page_with_captcha;
public string url;
public int id = 0;
public List<Log> somemethod()
{
try
{
cookies += response_get_page_with_captcha.Headers["Set-Cookie"];
return new Log(id++, DateTime.Now, cookies); //use this return value in your Form and update datagridview
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
[编辑3]并在Form1中使用此代码,使用类TermSh的返回值,并填充dataGridView:
TermSh term_sh = new TermSh(city, type, null, null);
logList.Add(term_sh.getPageWithCaptchaConnection());
logBindingSource.DataSource = logList;
logBindingSource.ResetBindings(false);
[EDIT 4]因此,如果您有一个问题:“如何在bindingSource中将此类用作集合?”。这很简单,您可以用对象填充dataGridView:这很有帮助。我认为,实现目标的方式是不正确的。 首先,我认为从一个班传给另一个班是非常糟糕的。然后,您可以简单地操作一个列表并返回值,然后在表单中使用这个值(列表) 我认为这样做更好: [EDIT 1]下面的这个类是您的ptimary类,它有一个方法,这个方法返回一个新日志,您可以将这个返回值添加到表单1中的datagridview中
class TermSh
{
public HttpWebRequest request_get_page_with_captcha;
public HttpWebResponse response_get_page_with_captcha;
public string url;
public int id = 0;
public List<Log> somemethod()
{
try
{
cookies += response_get_page_with_captcha.Headers["Set-Cookie"];
return new Log(id++, DateTime.Now, cookies); //use this return value in your Form and update datagridview
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
[编辑3]并在Form1中使用此代码,使用类TermSh的返回值,并填充dataGridView:
TermSh term_sh = new TermSh(city, type, null, null);
logList.Add(term_sh.getPageWithCaptchaConnection());
logBindingSource.DataSource = logList;
logBindingSource.ResetBindings(false);
[EDIT 4]因此,如果您有一个问题:“如何在bindingSource中将此类用作集合?”。这很简单,您可以使用对象填充dataGridView:很有帮助。以及如何不将form类传递给另一个类?另外:然后您可以简单地操作列表并返回值并使用此值。。。如何?你可以在你的表格中使用这个类?例如,形式为:TermSh tsh=new TermSh();List ls=tsh.somemethode();bindingsource.DataSource=ls;代码现在已清除,但datagridview仍然为空。。。我明天去看看。现在我设置你+1,我会接受,但是:也许我可以给你这个项目,你看我一眼?我可以将它发送到哪里?如何不将表单类传递给另一个类?另外:您可以简单地操作列表并返回值,然后使用此值。。。如何?你可以在你的表格中使用这个类?例如,形式为:TermSh tsh=new TermSh();List ls=tsh.somemethode();bindingsource.DataSource=ls;代码现在已清除,但datagridview仍然为空。。。我明天去看看。现在我设置你+1,我会接受,但是:也许我可以给你这个项目,你看我一眼?我可以将它发送到哪里?如何不将表单类传递给另一个类?另外:您可以简单地操作列表并返回值,然后使用此值。。。如何?你可以在你的表格中使用这个类?例如,形式为:TermSh tsh=new TermSh();List ls=tsh.somemethode();bindingsource.DataSource=ls;代码现在已清除,但datagridview仍然为空。。。我明天去看看。现在我设置你+1,我会接受,但是:也许我可以给你这个项目,你看我一眼?我可以将它发送到哪里?如何不将表单类传递给另一个类?另外:您可以简单地操作列表并返回值,然后使用此值。。。如何?你可以在你的表格中使用这个类?例如,形式为:TermSh tsh=new TermSh();List ls=tsh.somemethode();bindingsource.DataSource=ls;代码现在已清除,但datagridview仍然为空。。。我明天去看看。现在我设置你+1,我会接受,但是:也许我可以给你这个项目,你看我一眼?我可以把它寄到哪里?