c# - DataGridView "loses"列与 List<object>

标签 c# winforms datagridview

我有一个 List ComparedItem,我想在 datagridview dataGridViewCompare 中显示它。 ComparedItem的一些属性也是List,dataGridViewCompare不会显示这些列。

其实我不想在单元格中显示列表,而是想在用户选择特定行时在文本框中显示此单元格的内容。

public class ComparedItem
{
    public ElementItem SourceElement { get; private set; }
    public ElementItem TargetElement { get; private set; }
    public bool HasErrors { get; set; }
    public List<ErrorType> ErrorTypes { get; set; }
    public string FriendlyErrorNames { get; set; }
    public List<string> DetailsInconsistencyError { get; set; }
    public string DetailsSpecialCharsError { get; set; }
    public string DetailsTextError { get; set; }

    public ComparedItem(ElementItem source, ElementItem target)
    {
        SourceElement = source;
        TargetElement = target;
        DetailsInconsistencyError = new List<string>();
        DetailsInconsistencyError.Add("Test"); // <- Temp
        ErrorTypes = new List<ErrorType>();
        FriendlyErrorNames = String.Empty;
    }
}

在我的主要形式中:

List<ComparedItem> = new List<ComparedItem> comparedItems;
// fill list in some other code...
dataGridViewCompare.DataSource = comparedItems;

dataGridViewCompare 显示我期望的所有行,但仅显示列

  • 源元素
  • 目标元素
  • 有错误
  • FriendlyErrorNames
  • 详细说明特殊字符错误
  • 详细信息文本错误

显示。

  • 错误类型和
  • 详细信息不一致错误

迷路了。

是否无法在 datagridview 单元格中保存列表?

最佳答案

我不认为你可以显示 List<T>在 DGV 单元中。

它在 DataSource 中但是,这样您就可以提取值并将它们显示在 TextBoxes 中随心所欲。

以下是如何访问列表中(第一个选定的)行绑定(bind)到的项目:

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (dataGridView1.SelectedRows.Count <= 0) return;
    ComparedItem ci = (dataGridView1.SelectedRows[0].DataBoundItem as ComparedItem);

    if (ci != null) 
    {
      textBox1.Text = someStringRepresentation(ci.ErrorTypes);
      textBox2.Text = someStringRepresentation(ci.DetailsInconsistencyError);
    }
}

取决于SelectionMode您可能希望将类似的代码添加到 CurrentCellChanged事件等..

关于c# - DataGridView "loses"列与 List<object>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31606970/

相关文章:

c# - 使用 Typemock 断言一个方法被调用了 x 次

c# - 如何将标签添加到 DataGridView 单元格

c# - 只读 DataGridView 中的列和行

c# - 如何在 EF 4.1 Code First 中建立外键关系

c# - 使用 LocationManager.SetProviderLocation 模拟位置,如何使位置完整

c# - 选择数据网格(wpf)中的行索引

c# - 如何在 C# 中使用 openFileDialog 打开文件 .txt?

c# - WPF 使用语句打开另一个窗体

c# - 进度条和计时器

vb.net - 如何在 DataGridView 上制作固定背景