您應該添加一個新行到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);
}
我測試了代碼,它似乎正在按您的要求工作。如果我錯過了某些東西或者有任何部分不清楚,請告訴我。
這裏是之前和數據表後:
之前
後:
添加的行中的'DataTable'代替 – Magnus 2013-02-27 21:59:40
我的全部DataTable代碼如下所示:DataTable dt = null; dt = CLASS.METHOD(PARAM1,PARAM2,...); gv.DataSource = dt;和DataBind();如何添加一行? – Risho 2013-02-27 22:20:38