GridControl 在新行中显示值

GridControl display values in new line

我有一个 class,其中我将 FileALineFileBLine 属性的所有值存储到一个名为 Changes 的列表中 属性的代码如下所示。

public class Result
{
    public Result()
    {
        this.Changes = new List<Difference>();
    }

    public IList<Difference> Changes { get; set; }
}

public class Differences
{
    public int LineNumber { get; set; }

    public string FileALine { get; set; }

    public string FileBLine { get; set; }
}

我正在调用表单中的 Changes 列表来填充 Synfusion GridControl。它有两列 DataRowDetails。现在我只能在第二列中填写 FileAline 值或 FileBLine 值。

我无法弄清楚如何在一行中显示 FileAline 值并在下一行显示 FileBLine 值,如下图所示。

public partial class Form: Form
{
    private void DisplayValues(Result result)
    {
        if (result != null && result.Changes.Count > 0)
        {
            var differences = result.Changes;
            this.detailsGridControl.RowCount = differences.Count;
            this.detailsGridControl.ColCount = 2;

            for (var i = 0; i < differences.Count; i++)
            {
                var detailsA = differences[i];
                var row = i + 1;
                this.detailsGridControl[row, (int)FormColumns.DataRow].Text = detailsA.LineNumber.ToString();
                this.detailsGridControl[row, (int)FormColumns.Details].Text = detailsA.FileALine;
                this.detailsGridControl[row, (int)FormColumns.Details].Text = detailsA.FileBLine;
            }
        }
    }
}

所以输出一定是这样的。

关于如何实现这一点有什么建议吗?

提前致谢!

居然想出了解决办法

for (var i = 0; i < differences.Count; i++)
{
     var detailsA = differences[i];
     var row = i + 1;
     this.detailsGridControl[row, (int)FormColumns.DataRow].Text = detailsA.LineNumber.ToString();
     this.detailsGridControl[row, (int)FormColumns.Details].Text = detailsA.FileALine + Environment.NewLine + detailsA.FileBLine;
}

请尝试以下建议。

建议 1

在 Form 的 DisplayValues() 方法中,您试图为同一个单元格设置 FileALine 和 FileBLine。因此,CellValue 将被覆盖并且仅 returns 仅 FileBLine。为了从列表中设置 CellValue,可以使用 PopulateValues() 方法。参考下面的代码和示例,

Dim difference As New List(Of Differences)()
difference.Add(New Differences() With {.LineNumber = 1, .FileALine = `"FileLineA", .FileBLine = "FileLineB"})
difference.Add(New Differences() With {.LineNumber = 2, .FileALine = "FileLineB", .FileBLine = "FileLineA"})
Dim r As New Result()
For Each d As Differences In difference
r.Changes.Add(New Data() With {.DataRow = d.LineNumber, .Details = d.FileALine})
r.Changes.Add(New Data() With {.DataRow = d.LineNumber, .Details = d.FileBLine})
Next d
Me.gridControl1.PopulateHeaders(GridRangeInfo.Cells(0, 1, gridControl1.RowCount, gridControl1.ColCount), r.Changes)
Me.gridControl1.PopulateValues(GridRangeInfo.Cells(1, 1, gridControl1.RowCount, gridControl1.ColCount), r.Changes)
Me.gridControl1.Refresh()

建议 2

您还可以通过 GridStyleInfo class 的 Text 属性 设置 CellValue。参考样本,

Dim difference As New List(Of Differences)()
difference.Add(New Differences() With {.LineNumber = 1, .FileALine = "FileLineA", .FileBLine = "FileLineB"})
difference.Add(New Differences() With {.LineNumber = 2, .FileALine = "FileLineB", .FileBLine = "FileLineA"})
Dim r As New Result()
For Each d As Differences In difference
r.Changes.Add(New Data() With {.DataRow = d.LineNumber, .Details = d.FileALine})
 r.Changes.Add(New Data() With {.DataRow = d.LineNumber, .Details = d.FileBLine})
Next d
DisplayValues(r)
Private Sub DisplayValues(ByVal result As Result)
If result IsNot Nothing AndAlso result.Changes.Count > 0 Then
   Dim differences = result.Changes
   Me.gridControl1.RowCount = result.Changes.Count
   Me.gridControl1.ColCount = 2
   For i = 0 To differences.Count - 1
      Dim detailsA = TryCast(differences(i), Data)
      Dim row = i + 1
      Me.gridControl1(row, 1).Text = detailsA.DataRow.ToString()
     'To set FileALine or FileBLine for column 2
      Me.gridControl1(row, 2).Text = detailsA.Details
      Next i
   End If
End Sub

注意:您可以根据需要为列表创建一个对象来填充数据。