简体   繁体   English

如何在每个类别的末尾添加空数据行

[英]How to add empty datarow at the end of each category

I have DataTable in a C# application. 我在C#应用程序中有DataTable

This DataTable has a column named "Category" and has 10 different values. DataTable具有一个名为“类别”的列,并具有10个不同的值。

This is how my DataTable rows looks like: 这是我的DataTable行的样子:

在此处输入图片说明

How can I add a empty row after each Category group, here is an example of what I need: 如何在每个类别组之后添加一个空行,这是我需要的示例:

在此处输入图片说明

Any clue? 有什么线索吗?

Here is the solution that I came up with: 这是我想出的解决方案:

var insertAtIndexes = dataTable.Rows.Cast<DataRow>()
    .GroupBy(row => row["Category"])
    .Select(rowGroup => rowGroup.Select(row => dataTable.Rows.IndexOf(row) + 1)
    .Max()).ToList();

for (var i = 0; i < insertAtIndexes.Count; i++)
{
    var emptyRow = dataTable.NewRow();
    dataTable.Rows.InsertAt(emptyRow, insertAtIndexes[i] + i);
}

This will insert an empty row after each category group (this assumes like in your example that the rows are already ordered by category). 这将在每个类别组之后插入一个空行(假设在您的示例中该行已经按类别进行了排序)。 We insert in a for loop because as we insert new rows into the table, the insertAtIndexes will need to be increased to account for the newly inserted rows. 我们插入for循环是因为在表中插入新行时,需要增加insertAtIndexes来说明新插入的行。

NOTE: You can only insert the dataTable.NewRow() if your DataTable columns allow for nulls. 注意:如果您的DataTable列允许为空,则只能插入dataTable.NewRow() If they don't, then do something like this to assign default values. 如果没有,则执行类似的操作以分配默认值。 You would't have a blank row because your non-string columns wouldn't allow nulls: 您不会有空白行,因为您的非字符串列不允许为null:

for (var i = 0; i < insertAtIndexes.Count; i++)
{
    var emptyRow = dataTable.NewRow();
    dataTable.Rows.InsertAt(SetDefaultValues(emptyRow), insertAtIndexes[i] + i);
}

static DataRow SetDefaultValues(DataRow row)
{
    row.SetField(1, 0);
    row.SetField(2, 0);
    row.SetField(3, 0);
    row.SetField(4, 0);
    return row;
}
for (int i = dataTable.Rows.Count - 1; i > 0; i--)
{
    if ((string)dataTable.Rows[i]["Category"] != (string)dataTable.Rows[i - 1]["Category"])
    {
        var row = dataTable.NewRow();
        row["Category"] = string.Empty;
        dataTable.Rows.InsertAt(row, i);
    }
}
//Include Two Empty Rows After Each WCG
var insertAtIndexes = ds.Tables["Capacity Progress to Due Date"].Rows.Cast<DataRow>()
//.GroupBy(row => new { wcg = row.Field<int>("WcgName"), Date = Convert.ToDateTime(row.Field<int>("DueDate").ToString()) })
.GroupBy(row => row["WcgName"])
.Select(rowGroup => rowGroup.Select(row => ds.Tables["Capacity Progress to Due Date"].Rows.IndexOf(row) + 1)
.Max()).ToList();

for (var i = 0; i < insertAtIndexes.Count; i++){
    var emptyRow = ds.Tables["Capacity Progress to Due Date"].NewRow();
    var secondemptyRow = ds.Tables["Capacity Progress to Due Date"].NewRow();
    ds.Tables["Capacity Progress to Due Date"].Rows.InsertAt(emptyRow, insertAtIndexes[i] + i + i);
    ds.Tables["Capacity Progress to Due Date"].Rows.InsertAt(secondemptyRow, insertAtIndexes[i] + i + i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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