0

我需要在具有 AutoGenerateColumns="true" 的网格视图中添加一行。这是诀窍。SQL 查询以这样的方式编写(使用数据透视),它以三个一组的形式返回记录,如下所示:

Repair Code Repair Code Entries 6/1/2012    7/1/2012    8/1/2012    9/1/2012
00000A  Critical Down Time          1       
00000A  Critical Outage             1       
00000A  Total Repair Time          65       
00000B  Critical Down Time                                              6
00000B  Critical Outage                                                 3
00000B  Total Repair Time                                              90
00000C  Critical Down Time          1                      5    
00000C  Critical Outage             1                      5    
00000C  Total Repair Time          30                    240    
00000D  Critical Down Time                                 2     
00000E  Critical Down Time                                 1    
00000G  Critical Down Time                                 1    
00000M  Critical Down Time          1                      3    
00000M  Critical Outage             1                      3    
00000M  Total Repair Time          60                    180    

我需要在 00000A 和 XYXYXY 之间添加一个空白行。GridView 是使用 DataTable 从 bll 类填充的。我正在使用 OnRowCreated 方法来修改列标题和 OnRowDataBound 来格式化单元格中的信息。

我以为我可以在两种事件方法中的任何一种中添加一行,但在我看来,这在循环中为时已晚。我说的对吗?

我遇到过各种帖子,例如这个这个,但它们都以不同的方式进行,例如按钮单击事件。

在我的情况下,我唯一可以依赖的常数是三个类别的存在或不存在:停机时间、修复时间和总时间。在某些情况下,我只有三个类别中的一个或两个,这就是我需要插入具有相应缺失类别的行的地方。

任何建议如何去做?

谢谢,

R。

更新:我已经更新了上面查询的输出。正如您在下半部分看到的那样,“Critical Down Time”重复了 4 次,所以我需要截取数据并添加“Critical Outage”、“Total Repair Time”和一个空白行作为分隔符。

4

1 回答 1

1

您应该在DataTable. 像这样:

首先,找到要插入新行的索引。这是使用行的主键完成的(我假设您的行有主键)

int rowPosition = dt.Rows.IndexOf(dt.Rows.Find([PRIMARY KEY]));

然后创建一个新行并将其插入到表中:

dt.Rows.InsertAt(dt.NewRow(), rowPosition);

然后,您可以像以前一样绑定 GridView。

更新

从 OP 收到更多更新后,解决方案如下:

首先,一些变量。

/// <summary>
/// This holds the number and names of the subcategories that are required for each category. 
/// </summary>
string[] subCategories = new string[3] { "Critical Down Time", "Critical Outage", "Total Repair Time" };
string categoryPrevious = null;
string categoryCurrent = null;
int subCategoryOccurences = 0;
int rowCount = 0;
DataRow rowFiller = null;

这是从数据库填充数据表后接收数据表的方法。

public void PrepareDataTable(DataTable dtResults)
{

    if (dtResults == null || dtResults.Rows.Count == 0)
        return;

    //initialize category
    categoryPrevious = dtResults.Rows[0]["Category"].ToString();
    do
    {
        //get the current category
        categoryCurrent = dtResults.Rows[rowCount]["Category"].ToString();
        //check if this is a new category. this is where all the work is done
        if (categoryCurrent != categoryPrevious)
        {
            //check if we have fulfilled the requirement for number of subcategories 
            CheckSubCategoryRequirements(dtResults);
            //at this point we have fulfilled the requirement for number of subcategories 
            //add blank (separator) row
            dtResults.Rows.InsertAt(dtResults.NewRow(), rowCount);
            rowCount++;
            //reset the number of subcategories
            subCategoryOccurences = 0;
            categoryPrevious = categoryCurrent;
        }
        else
        {
            rowCount++;
            categoryOccurences++;
        }
    } while (rowCount < dtResults.Rows.Count);
    //check sub category requirements for the last category
    CheckSubCategoryRequirements(dtResults);

}

这是处理添加任何缺失子类别的方法。我已将代码提取到一个单独的方法中,因为它在代码中的两个不同位置调用:

/// <summary>
/// Checks if a category has fulfilled the requirements for # of sub categories and adds the missing sub categories, if needed
/// </summary>
private void CheckSubCategoryRequirements(DataTable dtResults)
{
    if (subCategoryOccurences< subCategories.Length)
    {
        //we need to add rows for the missing subcategories
        while (subCategoryOccurences< subCategories.Length)
        {
            //create a new row and populate category and subcategory info
            rowFiller = dtResults.NewRow();
            rowFiller["Category"] = categoryPrevious;
            rowFiller["SubCategory"] = subCategories[subCategoryOccurences];
            //insert the new row into the current location of table 
            dtResults.Rows.InsertAt(rowFiller, rowCount);
            subCategoryOccurences++;
            rowCount++;
        }
    }
}

最后,这里有一个“测试工具”来测试上面的代码:

public void RunTest()
{
    DataTable dtResults = new DataTable();
    dtResults.Columns.Add("Category");
    dtResults.Columns.Add("SubCategory");
    dtResults.Rows.Add("XXXX", "Critical Down Time");
    dtResults.Rows.Add("XXXX", "Critical Outage");
    dtResults.Rows.Add("XXXX", "Total Repair Time");
    dtResults.Rows.Add("YYYY", "Critical Down Time");
    dtResults.Rows.Add("YYYY", "Critical Outage");
    dtResults.Rows.Add("ZZZZ", "Critical Down Time");
    dtResults.Rows.Add("ZZZZ", "Critical Outage");
    dtResults.Rows.Add("ZZZZ", "Total Repair Time");
    dtResults.Rows.Add("AAAA", "Critical Down Time");

    PrepareDataTable(dtResults);
}

我已经测试了代码,它似乎可以按照您的要求工作。如果我错过了什么或任何部分不清楚,请告诉我。

这是数据表的前后:

在此处输入图像描述

后:

在此处输入图像描述

于 2013-02-27T22:53:43.113 回答