简体   繁体   中英

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. 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. 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". all "form2" should do is load and populate a datagridview based off of a list that is passed to the form. 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.

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 .

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.

Steps are pretty simple:

  • Create winform app and just add one control, a DataGridView and name it. In this example its simply named 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. You could do this based on a click of a button.
  • Create code to create a List<EDIFile> list. In this example GetDataGridViewData() returns List<EDIFile> . It basically creates 10 dummy EDIFile objects
  • Set DataSource of the DataGridView

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:

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:

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

And Form2 class should look like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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