[英]populate the values of multiple rows in single row in gridview
我正在嘗試在頁面后面的代碼的行數據綁定部分中使用c Sharp填充gridview中單行中多行的值,例如:
Col1 Col6
a 1
a 2
a 3
a 4
我已將其更改為
Col1 Col6
a 1
2
3
4
現在我要實現這一目標,請問有什么幫助嗎?
Col1 Col6
a 1 2 3 4
下面的代碼有什么問題?
if (e.Row.Cells[0].Text == "" && e.Row.Cells[5].Text != "")
{
for (int a = 0; a<GridView1.Rows.Count; a++)
{
string s = GridView1.Rows[a].Cells[5].Text;
GridView1.Rows[e.Row.RowIndex - 1].Cells[5].Text += s;
}
}
我在犯什么錯誤,有人嗎?
Cell[0] is Col1
Cell[5] is Col6
我認為您是從錯誤的方向來解決這個問題。 將GridView
視為僅顯示內容。 在將數據交給GridView
之前,您需要盡可能地安排數據。 我不確定您的數據是如何存儲的,但是您可能想要遍歷數據並創建本質上是一個分組的新記錄,並將所需的所有數據放入可以綁定到的一行中。
使用RowDataBound
(或任何數據綁定方法)的問題在於,您正在逐行處理數據,這不是您想要的,因為您無法在上下文中“看到”其他行數據。
至於數據整理,您可以使用以下代碼段獲取每個col1值的逗號分隔列表:
string commaValues = string.Join(",",
(yourList.Where(i => i.Col1.Equals(someValue)).
Select(s => s.YourStringValue)).ToArray()));
這將為給定值(示例中為someValue
)生成一個逗號分隔的字符串:
commaValues = "1,2,3,4,5"
使用上面的內容組成排列的對象List
,以便直接綁定它。 例如:
class NewObject
{
public string ID { get; set; }
public string Text { get; set; }
}
List<NewObject> arrangedData = new List<NewObject>();
// arrange the data here
yourGrid.DataSource = arrangedData;
yourGrid.DataBind();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.