我在stackoverflow上搜索了很久,但无法找到适合我问题的答案。我想要将数据表的值绑定到Windows窗体中的DataGridView,特别是在一个类中的DataTable和另一个文件中的GridView。
以下是我的代码:
以下是我的代码:
namespace MyProj
{
public partial class ThisAddIn
{
public string GetDetails()
{
// Some Codes here
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("uid");
dt.Columns.Add("email");
//Some codes here.I just only give a data table part only.
DataRow row = dt.NewRow();
row["id"] = sid;
sid++;
row["uid"] = uid;
row["email"] = e;
dt.Rows.Add(row);
}
}
}
我刚试图添加GridView,以下是代码。
首先,我添加“添加”-> “新项目” ->“Windows表单”,然后将其添加为form1.cs。
然后,我从工具箱中向这个form1.cs类中添加了GridView。接着双击GridView。
这是我的form1.cs编码。
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//ThisAddIn th = new ThisAddIn();
this.dataGridView1.Visible = true;
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource =dt; // here show dt does not contain the current context.
这两个文件在同一个命名空间下。当我尝试从一个类创建一个对象(ThisAddIn th = new ThisAddIn();)时,它显示:
ThisAddIn.ThisAddIn(Microsoft.Office.tools.Outlook Factory factory,IsServiceProvider serviceProvider)
This AddIn does not contain a constructor that takes 0 arguments
我是C#的新手,请帮助我解决这个问题,如果您能给我一个带有解释的解决方案,那就太好了。
(
时,会出现一个工具提示,告诉您它期望的值。您还可以打开类public partial class ThisAddIn
,并且它应该有一个名为public ThisAddIn(Type value, Type value)
的构造方法。 - undefined