2013-02-27 53 views
0

我需要添加一行到具有AutoGenerateColumns =“true”的gridview。这是一个窍门。 SQL查询被写成这样的方式(使用旋转),它返回三个一组记录为如下所示:当某些条件存在时自动添加一行到gridview

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以格式化单元格中的信息。

我想我可以在两个事件方法的eihter中添加一行,但在我看来,在周期中已经太迟了。我是对的?

我遇到过各种帖子,如this onethis one,但他们都以不同的方式去做,比如按钮点击事件。

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

任何建议如何去呢?

感谢,

R.

更新:我已经更新从上面的查询输出。正如您在下半部分看到的,“临界停机时间”被排除了4次,因此我需要拦截数据并添加“临界停机时间”,“总修理时间”和空行作为分隔符。

+0

添加的行中的'DataTable'代替 – Magnus 2013-02-27 21:59:40

+0

我的全部DataTable代码如下所示:DataTable dt = null; dt = CLASS.METHOD(PARAM1,PARAM2,...); gv.DataSource = dt;和DataBind();如何添加一行? – Risho 2013-02-27 22:20:38

回答

1

您应该添加一个新行到DataTable。像这样:

首先,找到你想要插入新行的索引。

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

然后,您可以:这是通过使用行的主键(我假设你行具有的主键)

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

然后创建一个新的行并将其插入到表完成像以前一样绑定GridView。

UPDATE

接收从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); 
} 

我测试了代码,它似乎正在按您的要求工作。如果我错过了某些东西或者有任何部分不清楚,请告诉我。

这里是之前和数据表后:

之前

enter image description here

后:

enter image description here

+0

我想这个问题仍然存在于我需要这样做的事件或生命周期中?并且不行,行没有PK - sproc查询由一个PIVOT语句组成,该语句从一个视图中获取数据,该视图中有另外三个视图以UNION形式连接。我曾经发布过一段时间,但我认为这可能会吓跑人们,或者认为我很疯狂,也没有人回答。我可以发布代码,但它必须是明天早晨。 – Risho 2013-02-27 23:42:56

+0

@Risho在将它绑定到网格之前,将行添加到'DataTable'中。您不需要对GridView进行任何操作。 – Magnus 2013-02-27 23:49:05

+0

如果你没有主键,它仍然是可行的,只是更麻烦一点。快速提问。用例总是在“00000A”和“XYXYXY”之间添加一行,还是更通用:我想在“节”之间添加一个空行?按部分,我指的是第一列具有相同值的行。 – 2013-02-27 23:49:56