C# 在Windows窗体中将Datatable数据绑定到Gridview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14794470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:00:46  来源:igfitidea点击:

Bind Datatable data to Gridview in Windows form

c#winformsvsto

提问by Jennie Krist

I search all over the stackoverflow and unable to find a suitable answer for my problem. I wanted to bind datatable values to datagridview in windows form.Specially data table in one class and Gridview in Seperate file.

我在整个 stackoverflow 中搜索,但找不到适合我的问题的答案。我想将数据表值绑定到 Windows 窗体中的 datagridview。特别是一个类中的数据表和单独文件中的 Gridview。

Here is my Code.

这是我的代码。

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);
    }
}
}

I Just tried to add Gridview,here is that code. First i add Add -> NewItem -> WindowsForm & add as form1.cs

我只是尝试添加 Gridview,这是代码。首先我添加 Add -> NewItem -> WindowsForm 并添加为 form1.cs

Then i add Gridview to this form1.cs class from toolbox.Then double click gridview.

然后我从工具箱中将 Gridview 添加到这个 form1.cs 类中。然后双击 gridview。

Here is my form1.cs coding

这是我的 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. 

}

}

Both files are under same namespace.When i tries to create a object from a class (ThisAddIn th = new ThisAddIn();)then it shows,

两个文件都在同一个命名空间下。当我尝试从一个类中创建一个对象时 (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

ThisAddIn.ThisAddIn(Microsoft.Office.tools.Outlook Factory factory,IsServiceProvider serviceProvider)

此 AddIn 不包含采用 0 个参数的构造函数

I'm a fresher to c# and please help me to solve this problem,If you can give me a solution with explanation is great..

我是 c# 的新手,请帮我解决这个问题,如果你能给我一个有解释的解决方案,那就太好了..

采纳答案by Jeremy Thompson

1) The GetDetails method must return a DataTable, so I changed stringto DataTableand return dt;

1)GetDetails方法必须返回一个DataTable,所以我改变stringDataTable和返回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) Notice how I instantiate the ThisAddIn class, then I call the GetDetails method - returning the results into a DataTable who's scope is in context.

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) When you instantiate ThisAddIn th = new ThisAddIn();you get the error:

3)当你实例化ThisAddIn th = new ThisAddIn();你得到错误:

This AddIn does not contain a constructor that takes 0 arguments

此 AddIn 不包含采用 0 个参数的构造函数

To resolve this you need to supply some values (arguments in the parameter) when instantiating the class:

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

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

回答by JIYAUL MUSTAPHA

  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();
                }
            }
        }