简体   繁体   English

如何使用对象列表作为数据源向C#winforms项目添加gridview

[英]How to add a gridview to a C# winforms project with a list of objects as the datasource

I'm building a winforms application with C# in Visual Studio 2015 community. 我正在Visual Studio 2015社区中使用C#构建Winforms应用程序。 In this project I have a class built as so: 在这个项目中,我有一个这样构建的类:

public class EDIFile
    {
        public string fullInFilePath { get; set; }
        public string fullOutFilePath { get; set; }
        public string InfileName { get; set; }
        public string OutfileName { get; set; }
        public string UniqueID { get; set; }
        public DateTime infileDateTime { get; set; }
        public DateTime outfileDateTime { get; set; }
        public TimeSpan timeDiff { get; set; }
    }

I have a list built of this class. 我有一个此类的清单。 After it is loaded I would like to have a gridview that is populated with the contents of this list. 加载后,我想要一个包含此列表内容的gridview。 I've looked online and I've found a couple of solutions but none of them have worked for me. 我看了网上,找到了两个解决方案,但没有一个对我有用。 Please help me out with some specific instructions if you can. 如果可以的话,请通过一些具体说明帮助我。 Thanks in advance. 提前致谢。

I still haven't gotten it to work for me yet. 我还没有得到它为我工作。 Based off of the two answers here is the code that I have so far: 基于这两个答案,这里是我到目前为止的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProviderPayProject1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load_1(object sender, List<EDIFile> EDIFiles)
        {
            DataGridView dataGridView1 = new DataGridView();
            dataGridView1.AutoGenerateColumns = true;
            Controls.Add(dataGridView1);
            dataGridView1.DataSource = EDIFiles;
        }
    }
}

this is the code from "form2" that is called from "form1". 这是“ form1”中调用的“ form2”中的代码。 all "form2" should do is load and populate a datagridview based off of a list that is passed to the form. 所有“ form2”应该做的就是基于传递到表单的列表加载并填充datagridview。 Please help me know what I'm doing wrong. 请帮助我知道我在做什么错。

Not entirely sure I understand your question, if you are looking to create DataGridView through code you could do this. 不能完全确定我理解您的问题,如果您希望通过代码创建DataGridView ,则可以这样做。

List<EDIFile> data; // construct your data.
DataGridView grid = new DataGridView();
grid.AutoGenerateColumns = true;
this.Controls.Add(grid);

grid.DataSource = data;

Other option, add DataGidView control through designer and set the DataSource . 其他选项,通过设计器添加DataGidView控件并设置DataSource

ex..
List<EDIFile> data; // construct your data.
dataGridView1.DataSource =data;

You could use the following code, its pretty simple. 您可以使用以下代码,它非常简单。 When the form is loaded it sets the DataGridViews datasource. 加载表单后,它将设置DataGridViews数据源。

Steps are pretty simple: 步骤非常简单:

  • Create winform app and just add one control, a DataGridView and name it. 创建winform应用程序,然后仅添加一个控件,一个DataGridView并将其命名。 In this example its simply named dataGridView1 . 在此示例中,其简单命名为dataGridView1 You'll need to figure this step out yourself though . 不过,您需要自己弄清楚这一步
  • Determine how the DataSource for how the DataGridView will be set, in this example, it is set when the form is loaded, the Form1_Load() function. 确定如何设置DataGridView的DataSource ,在此示例中,是在窗体加载时设置的Form1_Load()函数。 You could do this based on a click of a button. 您可以通过单击按钮来执行此操作。
  • Create code to create a List<EDIFile> list. 创建代码以创建List<EDIFile>列表。 In this example GetDataGridViewData() returns List<EDIFile> . 在此示例中, GetDataGridViewData()返回List<EDIFile> It basically creates 10 dummy EDIFile objects 它基本上会创建10个虚拟EDIFile对象
  • Set DataSource of the DataGridView 设置DataGridView DataSource

Assuming you have the winform from the first step, you should be able to change the namespace WindowsFormsApplication1 and the Form class Form1 to what you need: 假设您具有第一步的winform,则应该能够将名称空间WindowsFormsApplication1和Form类Form1更改为所需的名称:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        // the list of EDFiles 
        public List<EDIFile> EDFiles { get; set; }
        public Form1()
        {
            InitializeComponent();
            Load += Form1_Load; // add Form Load eventhandler
        }
        /// <summary>
        /// Called when the form is loaded
        /// </summary>
        private void Form1_Load(object sender, EventArgs e)
        {
            // get dummy data
            EDFiles = GetDataGridViewData();
            // set datasource to the dummy data
            dataGridView1.DataSource = EDFiles;
        }
        /// <summary>
        /// Returns dummy data for the datagridview
        /// </summary>
        /// <returns></returns>
        private List<EDIFile> GetDataGridViewData()
        {
            // bascially just creates dummy data for the example,
            // you'll need to implement based on how you need it to be
            var newEDFiles = new List<EDIFile>();
            Random random = new Random();
            for (int i = 1; i <= 10; i++)
            {
                int randomNumber = random.Next(0, 100);
                var infileDateTime = DateTime.Now;
                var outfileDateTime = infileDateTime.AddDays(randomNumber);
                var edfile = new EDIFile()
                {
                    fullInFilePath = "fullInFilePath" + i,
                    fullOutFilePath = "fullOutFilePath" + i,
                    InfileName = "InfileName" + i,
                    OutfileName = "InfileName" + i,
                    UniqueID = "UniqueID" + i,
                    infileDateTime = infileDateTime,
                    outfileDateTime = outfileDateTime,
                    timeDiff = outfileDateTime- infileDateTime
                };
                newEDFiles.Add(edfile);
            }
            return newEDFiles;
        }
    }
    public class EDIFile
    {
        public string fullInFilePath { get; set; }
        public string fullOutFilePath { get; set; }
        public string InfileName { get; set; }
        public string OutfileName { get; set; }
        public string UniqueID { get; set; }
        public DateTime infileDateTime { get; set; }
        public DateTime outfileDateTime { get; set; }
        public TimeSpan timeDiff { get; set; }
    }
}

My example looks like this: 我的示例如下所示: 在此处输入图片说明

In the Form1 class for the button click event you'll need the following: 在按钮单击事件的Form1类中,您将需要以下内容:

var f2 = new Form2();
f2.ShowForm(GetDataGridViewData());

And Form2 class should look like this: Form2类应如下所示:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public void ShowForm(List<EDIFile> EDIFiles)
    {
        DataGridView dataGridView1 = new DataGridView();
        dataGridView1.AutoGenerateColumns = true;
        Controls.Add(dataGridView1);
        dataGridView1.DataSource = EDIFiles;
        this.ShowDialog();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM