GridControl在新行中显示值

时间:2017-07-06 00:07:24

标签: c# .net winforms syncfusion gridcontrol

我有一个课程,其中我将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;
            }
        }
    }
}

所以输出必须是这样的。

Output

有关如何实现这一目标的任何建议吗?

提前致谢!

2 个答案:

答案 0 :(得分:2)

请尝试以下建议。

建议1

在Form的DisplayValues()方法中,您尝试为同一单元格设置FileALine和FileBLine。因此,将覆盖CellValue并仅返回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类的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

注意:您可以为列表创建一个对象,以方便地填充数据。

答案 1 :(得分:1)

实际上找到了解决方案。

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