2013-10-20 83 views
0

假如我有两个数据表:在单个DataGridView单元中显示数据表内容?

- 在第一数据表orders,我找回我的订单列表,像

--------------------------- 
'order_id ' order  ' 
'---------'---------------' 
' 1  ' order details' 
'---------'---------------' 
' 2  ' order details' 
'---------'---------------' 
' 3  ' order details' 
'---------'---------------' 
' 1  ' order details' 
'---------'---------------' 

- 在第二数据表order_details,我检索每个订单的详细信息(如客户,订单价值等)

-------------------------------- 
'order_id'customer'order_val'...' 
'--------'--------'---------'---' 
'order_id' Cust1 ' Value11 '...' 
'--------'--------'---------'---' 
'order_id' Cust2 ' Value21 '...' 
'--------'--------'---------'---'  

我的问题: 多远可能的第二个数据表的建立(如缩略图),并在插入小区(即用上表中所示的“订单细节”替换)?

--------------------------- 
'order_id ' order  ' 
'---------'---------------' 
' 1  ' Cust1 Value11 ' 
'---------'---------------' 
' 2  ' Cust2 Value21 ' 
'---------'---------------' 
+0

非常感谢编辑@MrW – user2888402

回答

2

这并不难。您需要倾听CellPainting事件并使用Graphics类图绘制方法(如DrawString)在该单元格内绘制您的内容。您可能会遇到的一个问题是,您显然只能使用最少的屏幕空间,因此您可能需要ScaleTransform或以其他方式最小化要显示的内容数量。

e.RowIndex将使您能够访问底层DataRow,然后您可以使用它访问需要绘制的子行/ DataTable。 e.ColumnIndex可用于确定将显示表格内容的正确列。 e.Graphics是可让您绘制内容的实际Graphics对象。 e.CellBounds将为您提供绘图区域的大小和位置。

这里是CellPainting事件的一个小例子:如果你使用强类型DataSet

private void dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
{ 
    var dgv = (DataGridView)sender; 

    //Get the order row 
    var row = ((DataRowView)dgv.Rows[e.RowIndex].DataBoundItem).Row; 

    //Get child rows of your order row 
    var OrdDetails = row.GetChildRows("rel_Order_OrderDetails"); 

    //An example of how information from one of these child rows can be drawn inside the cell 
    e.Graphics.DrawString(OrdDetails[0]["CustomerName"], dgv.Font, Brushes.Black, e.CellBounds.Location); 
} 

,上面的代码会略有不同,你将有直接的方法来访问你的子数据父行。

+0

非常感谢 – user2888402

相关问题