在Windows窗体中将Datatable的数据绑定到Gridview。

3
我在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#的新手,请帮助我解决这个问题,如果您能给我一个带有解释的解决方案,那就太好了。

2个回答

4

1) GetDetails方法必须返回一个DataTable,因此我将string更改为DataTable并返回dt;

public partial class ThisAddIn
{
public DataTable GetDetails()
  {
  // Some Codes here
    DataTable dt = new DataTable();
    dt.Columns.Add("id");
    dt.Columns.Add("uid");
    dt.Columns.Add("email");
    DataRow row = dt.NewRow();    
     row["id"] = sid;
     sid++;    
     row["uid"] = uid;
     row["email"] = e;
     dt.Rows.Add(row);
     return dt;
  }
}

2) 注意我如何实例化ThisAddIn类,然后调用GetDetails方法 - 将结果返回到一个作用域为上下文的DataTable中。

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    ThisAddIn th = new ThisAddIn();
    //Declare a DataTable and call to GetDetails
    DataTable dt =  th.GetDetails();
    this.dataGridView1.Visible = true;
    dataGridView1.AutoGenerateColumns = false;
    dataGridView1.DataSource = dt;
}

3) 当您实例化 ThisAddIn th = new ThisAddIn(); 时,会出现以下错误:

此 AddIn 不包含不带参数的构造函数

为了解决这个问题,您需要在实例化类时提供一些值(在参数中提供参数):

ThisAddIn th = new ThisAddIn(value1, value2, etc)

我需要提供哪些值?我能在这里提供dt吗,因为现在dt正在返回吗? - undefined
在Visual Studio中,当您键入开括号(时,会出现一个工具提示,告诉您它期望的值。您还可以打开类public partial class ThisAddIn,并且它应该有一个名为public ThisAddIn(Type value, Type value)的构造方法。 - undefined

0
  private void BindProductsGrid()
        {
            dataGridView1.Rows.Clear();
            DataTable dt = new DataTable();
            dt = bl.BindProducts();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dataGridView1.Rows.Add();
                    dataGridView1.AllowUserToAddRows = false;
                    dataGridView1.Rows[i].Cells[1].Value = dt.Rows[i]["Product_id"].ToString();
                    dataGridView1.Rows[i].Cells[2].Value = dt.Rows[i]["Product_name"].ToString();
                    dataGridView1.Rows[i].Cells[3].Value = dt.Rows[i]["Quantity"].ToString();
                }
            }
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接