[英]How to populate DropDownList in GridView EditItemTemplate depending on other column values
[英]How to set a gridView column's text depending on other columns which are binded?
我在ASP中有一個GridView,可用於在購物車中顯示產品。
我設置了GridView的源ShopDataSet,並且自動填充了名稱,價格和數量列。
我要實現的是我添加的新列,該列顯示每行的成本,即Cost = price * quantity;
如何在不對數據庫執行新查詢的情況下以編程方式執行此操作?
我不得不說,在價格和數量上,我為價格設置了格式,例如"${0}"
為數量設置了"{0} piece(s)"
。
在SqlDataReader中獲取結果,並使用SqlDataReader的結果填充DataTable。
現在,創建一個新的DataTable,其中的列與所需的輸出匹配,包括您的案例中的Cost。 現在,遍歷第一個數據表的每一行,並通過成本計算將行添加到新的數據表中。
例如,
DataTable oldTable = new DataTable();
oldTable .Load(rdr); // where rdr is your SqlDataReader
DataTable newTable = new DataTable();
DataColumn noteID = new DataColumn("Cost", typeof(string));
newTable.Columns.Add(noteID);
//Add other columns
foreach (DataRow row in oldTable .Rows)
{
DataRow newRow = newTable.NewRow();
newRow["Cost"] = //your calculation
...
...
newTable.Add(newRow);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.