2013-02-27 43 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

相關問題