C# =“表格1”; this.Text=“Form1”; ((ISupportInitialize)(this.dgvMachines)).EndInit(); 此选项为.resume布局(false); IList machines=new BindingList(); dgvMachines.DataSource=机器; 添加(新机器{Name=“#1”,CashCount=100}); 添加(新机器{Name=“#2”,CashCount=200}); 添加(新机器{Name=“#3”,CashCount=300}); 添加(新机器{Name=“#4”,CashCount=400}); } } 类机器 { 公共字符串名称{get;set;} 公共十进制现金计数{get;set;} } }

C# =“表格1”; this.Text=“Form1”; ((ISupportInitialize)(this.dgvMachines)).EndInit(); 此选项为.resume布局(false); IList machines=new BindingList(); dgvMachines.DataSource=机器; 添加(新机器{Name=“#1”,CashCount=100}); 添加(新机器{Name=“#2”,CashCount=200}); 添加(新机器{Name=“#3”,CashCount=300}); 添加(新机器{Name=“#4”,CashCount=400}); } } 类机器 { 公共字符串名称{get;set;} 公共十进制现金计数{get;set;} } },c#,winforms,visual-studio,list,datagridview,C#,Winforms,Visual Studio,List,Datagridview,对于“无法创建抽象类或接口IList的实例”的特定错误,问题是您正在尝试创建接口的实例。你想要一个具体的课程。例如,List实现了IList,所以您可以 IList<string> names = new List<string>(); IList name=new List(); 接口基本上只是一个契约,描述实现它的类可以做什么 对于您的问题,您希望创建一个类来保存数据,将文件解析为对象,将它们放入BindingList或任何其他适当的集合中,然后将datagrid

对于“无法创建抽象类或接口IList的实例”的特定错误,问题是您正在尝试创建接口的实例。你想要一个具体的课程。例如,List实现了IList,所以您可以

IList<string> names = new List<string>();
IList name=new List();
接口基本上只是一个契约,描述实现它的类可以做什么

对于您的问题,您希望创建一个类来保存数据,将文件解析为对象,将它们放入BindingList或任何其他适当的集合中,然后将datagridview绑定到该集合

class Machine
{
    public string Name {get; set;}
    public decimal CashCount {get; set;}
}

public static void main()
{
    IList<Machine> machines = new BindingList<Machine>();

    foreach(string fileName in fileNames)
    {
        //read your files, put data in a Machine object
        Machine m =new Machine();
        m.Name=nm[0];
        m.CashCount=MCC1[0];
        //add it to the list
        machines.add(m);
    }

    //then bind the collection to your datagridview
    datagridView.Datasource = machines;
}
类机器
{
公共字符串名称{get;set;}
公共十进制现金计数{get;set;}
}
公共静态void main()
{
IList machines=new BindingList();
foreach(文件名中的字符串文件名)
{
//读取文件,将数据放入机器对象中
机器m=新机器();
m、 名称=纳米[0];
m、 现金计数=MCC1[0];
//将其添加到列表中
增加(m);
}
//然后将集合绑定到datagridview
datagridView.Datasource=machines;
}
然后,在表单设计器中,可以向datagridview添加2列,并将其绑定分别设置为Name和CashCount。例如,您还可以将CashCount列的格式设置为money


编辑:添加最小工作示例

using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace datagridview
{
    public partial class Form1 : Form
    {
        private DataGridView dgvMachines;

        public Form1()
        {
            InitializeComponent();
            //this is the Designer.cs code...
            this.dgvMachines = new System.Windows.Forms.DataGridView();
            ((System.ComponentModel.ISupportInitialize)(this.dgvMachines)).BeginInit();
            this.SuspendLayout();
            // 
            // dgvMachines
            // 
            this.dgvMachines.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvMachines.Location = new System.Drawing.Point(12, 12);
            this.dgvMachines.Name = "dgvMachines";
            this.dgvMachines.Size = new System.Drawing.Size(606, 400);
            this.dgvMachines.TabIndex = 0;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(630, 434);
            this.Controls.Add(this.dgvMachines);
            this.Name = "Form1";
            this.Text = "Form1";
            ((ISupportInitialize)(this.dgvMachines)).EndInit();
            this.ResumeLayout(false);

            IList<Machine> machines = new BindingList<Machine>();
            dgvMachines.DataSource = machines;
            machines.Add(new Machine { Name = "#1", CashCount = 100 });
            machines.Add(new Machine { Name = "#2", CashCount = 200 });
            machines.Add(new Machine { Name = "#3", CashCount = 300 });
            machines.Add(new Machine { Name = "#4", CashCount = 400 });
        }
    }

    class Machine
    {
        public string Name { get; set; }
        public decimal CashCount { get; set; }
    }
}
使用System.Collections.Generic;
使用系统组件模型;
使用System.Windows.Forms;
命名空间datagridview
{
公共部分类Form1:Form
{
专用DataGridView dgvMachines;
公共表格1()
{
初始化组件();
//这是Designer.cs代码。。。
this.dgvMachines=new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.dgvMachines)).BeginInit();
这个.SuspendLayout();
// 
//DGV机器
// 
this.dgvMachines.ColumnHeadersHeightSizeMode=DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgvMachines.Location=新系统.图纸.点(12,12);
this.dgvMachines.Name=“dgvMachines”;
this.dgvMachines.Size=新系统图纸尺寸(606400);
this.dgvMachines.TabIndex=0;
// 
//表格1
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=AutoScaleMode.Font;
this.ClientSize=新系统图尺寸(630434);
this.Controls.Add(this.dgvmaches);
this.Name=“Form1”;
this.Text=“Form1”;
((ISupportInitialize)(this.dgvMachines)).EndInit();
此选项为.resume布局(false);
IList machines=new BindingList();
dgvMachines.DataSource=机器;
添加(新机器{Name=“#1”,CashCount=100});
添加(新机器{Name=“#2”,CashCount=200});
添加(新机器{Name=“#3”,CashCount=300});
添加(新机器{Name=“#4”,CashCount=400});
}
}
类机器
{
公共字符串名称{get;set;}
公共十进制现金计数{get;set;}
}
}

感谢您的回复,很遗憾,我还没有完全理解您的答案,因为我无法在表格中实际显示值。我已经用我所做的编辑更新了代码made@MarkBuckley首先要确保你确实有数据。第二,确保daragridview确实添加到表单中,而不仅仅是声明。我添加了一个适合我的最小示例。我复制了你的代码,它工作了,但是我在列的顶部得到了一个灰色框,我添加了一个屏幕截图,你知道是什么导致了它吗?如果你仔细看,你会看到黑色的轮廓比datagridview略大。我敢打赌你以前的空datagrid比新的工作datagrid要好。这是我在designer中DataGridView框上使用的缩放,非常感谢你的帮助!!!谢谢你的回复,不幸的是,我还没有完全理解你的答案,因为我无法在我的表格中实际显示这些值。我已经用我所做的编辑更新了代码made@MarkBuckley首先要确保你确实有数据。第二,确保daragridview确实添加到表单中,而不仅仅是声明。我添加了一个适合我的最小示例。我复制了你的代码,它工作了,但是我在列的顶部得到了一个灰色框,我添加了一个屏幕截图,你知道是什么导致了它吗?如果你仔细看,你会看到黑色的轮廓比datagridview略大。我敢打赌你以前的空datagrid比新的工作datagrid要好。这是我在designer中DataGridView框上使用的缩放,非常感谢你的帮助!!!
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace datagridview
{
    public partial class Form1 : Form
    {
        private DataGridView dgvMachines;

        public Form1()
        {
            InitializeComponent();
            //this is the Designer.cs code...
            this.dgvMachines = new System.Windows.Forms.DataGridView();
            ((System.ComponentModel.ISupportInitialize)(this.dgvMachines)).BeginInit();
            this.SuspendLayout();
            // 
            // dgvMachines
            // 
            this.dgvMachines.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvMachines.Location = new System.Drawing.Point(12, 12);
            this.dgvMachines.Name = "dgvMachines";
            this.dgvMachines.Size = new System.Drawing.Size(606, 400);
            this.dgvMachines.TabIndex = 0;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(630, 434);
            this.Controls.Add(this.dgvMachines);
            this.Name = "Form1";
            this.Text = "Form1";
            ((ISupportInitialize)(this.dgvMachines)).EndInit();
            this.ResumeLayout(false);

            IList<Machine> machines = new BindingList<Machine>();
            dgvMachines.DataSource = machines;
            machines.Add(new Machine { Name = "#1", CashCount = 100 });
            machines.Add(new Machine { Name = "#2", CashCount = 200 });
            machines.Add(new Machine { Name = "#3", CashCount = 300 });
            machines.Add(new Machine { Name = "#4", CashCount = 400 });
        }
    }

    class Machine
    {
        public string Name { get; set; }
        public decimal CashCount { get; set; }
    }
}