c# - 将 MyClass 的列表显示到 DataGridView

标签 c# winforms datagridview

我正在尝试将我的 MyClass 列表中的所有数据显示到 DataGridView 以进行故障排除。所以我在我的应用程序中实现了下面的代码(来自 Marc Gravell 的帖子 here)。它会执行,但尽管我的列表包含数据,props.Count 和 values.Length 始终为 0。因此返回的 DataTable 没有行或列。

我做错了什么?

public static DataTable ToDataTable<T>(IList<T> data)
{
    PropertyDescriptorCollection props =
        TypeDescriptor.GetProperties(typeof(T));
    DataTable table = new DataTable();
    for (int i = 0; i < props.Count; i++)
    {
        PropertyDescriptor prop = props[i];
        table.Columns.Add(prop.Name, prop.PropertyType);
    }
    object[] values = new object[props.Count];
    foreach (T item in data)
    {
        for (int i = 0; i < values.Length; i++)
        {
            values[i] = props[i].GetValue(item);
        }
        table.Rows.Add(values);
    }
    return table;
}

我的调用代码:

dgv.DataSource = Globals.ToDataTable(Lines_Formatted);

我的类(class):

public class LineData
{
    public string RowID = "";
    public bool MissingMatchingPunch = false;
    public bool ScheduleIssueWithPunches = false;
    public bool ScheduledTimeAmountMet = true;
    public bool Approved_TimeEntry = true;
    public bool Approved_PunchIn = true;
    public bool Approved_PunchOut = true;
    public DateTime DateApplicable = DateTime.MinValue;
    public string TimeType = "";
    public int TimeType_CID = -1;
    public Double HoursWorked = 0;
    public double Amount = 0;
    public DateTime PunchIn = DateTime.MinValue;
    public DateTime PunchOut = DateTime.MinValue;
    public Double DailyTotal = 0;
    public Double CumulativeTotal = 0;
    public string EnteredBy = "";
    public LineType LineTypeKey;
    public enum LineType
    {
        PunchEntry = 1,
        TimeEntry = 2,
        BlankLine = 5
    }
    public string CID_Time = "";
    public string CID_PunchIn = "";
    public string CID_PunchOut = "";
    public string Account_CID = "";
}

最佳答案

您的类(class)仅包含字段。您正在使用的代码、数据绑定(bind)和 TypeDescriptor服务通常需要公共(public)属性

要解决此问题,请将您的字段转换为自动属性。如果您使用的是 C#6 (VS2015),插入 { get; set; } 会很简单像这样

public class LineData
{
    public string RowID { get; set; } = "";
    public bool MissingMatchingPunch { get; set; } = false;
    public bool ScheduleIssueWithPunches { get; set; } = false;
    // ....
}

如果您使用的是较旧的 C# 版本,则需要添加 { get; set; }部分,但将初始化移动到类构造函数中。

public class LineData
{
    public string RowID { get; set; }
    public bool MissingMatchingPunch { get; set; }
    public bool ScheduleIssueWithPunches { get; set; }
    // ....

    public LineData()
    {
        RowID = "";
        MissingMatchingPunch = false;
        ScheduleIssueWithPunches = false;
        // ...
    }
}

您不需要使用 false 这样的默认值来初始化属性对于 bool0对于 int等等

最后,完成后该功能应该可以正常工作了。但正如我在评论中提到的,一旦你有一个 List<LineData> ,您可以直接将其用作数据 GridView 的数据源 - 无需先将其转换为 DataTable .

关于c# - 将 MyClass 的列表显示到 DataGridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33782895/

相关文章:

c# - 将 generatePublisherEvidence false 设置为默认值

c# - datagridview 自动选择列禁用 c#

c# - 使用 ComboBox 将 DataGridView 绑定(bind)到 DataTable 不起作用

c# - 带有显示详细信息选项的消息框?

C# Windows.Forms.WebBrowser 缩放

c# - 如何将相同的 DataSource 用于多个 DataGridView,并在每个 DataGridView 上应用不同的过滤器?

c# - Angular 2 - 将来自 Web Api 的 byte[] 呈现为图像源

c# - Twilio API 请求限制/限制?

c# - 如何安排数据库任务?

c# - BitmapImage 在文件不存在时抛出初始化异常