本文将介绍如何利用DataGridImageColumn列来显示数据的隶属关系,具体的效果如下图
点击图标这一列,就会自动的显示它的下属数据和 收缩 下属数据。当然对于这个效果中,对原来的DataGridImageColumn进行了一些改动,对 DataGrid 的也需要一些另外的处理,还有数据也需要有要求,主要是不能对数据进行 排序 ,不来显示会乱掉。其中排序也需要一个 字段 信息 我这里设置了一个 KM _ XH ,一个是否显示的列 KM _Visible,有了这两个列,就能够很好的实现了上面的效果。
在具体的DataGrid定义的地方需要设置下面的一些方法
private
void
DataGridClick(
object
sender,System.Windows.Forms.MouseEventArgs e)
{
// this.dataGrid1.
DataGrid.HitTestInfo hti = this.dataGrid1.HitTest(new System.Drawing.Point(e.X, e.Y));
if(hti.Row==-1||hti.Column==-1)
return;
if(this.dataGrid1.TableStyles[0].GridColumnStyles[hti.Column] is DataGridImageBarColumn)
{
DataRow dr =dt.DefaultView[hti.Row].Row;
DataRow[] drs = dt.Select(LSGX+"="+dr[ID].ToString());
if(drs.Length>0)
{
bool visible = true;
try
{
if(Convert.ToBoolean(drs[0][strVisible]))
{
visible = false;
}
}
catch
{
visible = false;
}
this.setVisible(dr,visible);
}
}
}
![](http://dev.yesky.com/imagelist/05/06/85yy16eeu2tg.gif)
/// <summary>
/// 设置下级的可见性
/// </summary>
/// <param name="dr"></param>
/// <param name="visible"></param>
private
void
setVisible(DataRow dr,
bool
visible)
{
DataRow[] drs = dt.Select(LSGX+"="+dr[ID].ToString());
foreach(DataRow drChild in drs)
{
drChild[strVisible]=visible;
if(!visible)
{
this.setVisible(drChild,visible);
}
}
}
![](http://dev.yesky.com/imagelist/05/06/rql5bsr13y3d.gif)
/// <summary>
/// 设置图片信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
void
setImage(
object
sender,DataGridImageCellEventArgs e)
{
try
{
DataRow dr = dt.DefaultView[e.Row].Row;
e.Number = Number(dr,0);
e.HasList = HasList(dr);
if(e.HasList)
{
if(ShowList(dr))
{
e.Image = this.imageList1.Images[0];
}
else
{
e.Image = this.imageList1.Images[1];
}
}
else
{
e.Image = this.imageList1.Images[2];
}
}
catch
{
}
}
![](http://dev.yesky.com/imagelist/05/06/pjr7ci3vv29i.gif)
/// <summary>
/// 是否显示细表
/// </summary>
/// <param name="dr"></param>
/// <returns></returns>
private
bool
ShowList(DataRow dr)
{
if(dt.Select(LSGX+"="+dr[ID].ToString()+" and "+strVisible+" = true").Length>0)
return true;
return false;
}
/// <summary>
/// 是否有细表
/// </summary>
/// <param name="dr"></param>
/// <returns></returns>
private
bool
HasList(DataRow dr)
{
if(dt.Select(LSGX+"="+dr[ID].ToString()).Length>0)
return true;
return false;
}
/// <summary>
&
![](http://dev.yesky.com/imagelist/05/06/r5703889c3l5.gif)
点击图标这一列,就会自动的显示它的下属数据和 收缩 下属数据。当然对于这个效果中,对原来的DataGridImageColumn进行了一些改动,对 DataGrid 的也需要一些另外的处理,还有数据也需要有要求,主要是不能对数据进行 排序 ,不来显示会乱掉。其中排序也需要一个 字段 信息 我这里设置了一个 KM _ XH ,一个是否显示的列 KM _Visible,有了这两个列,就能够很好的实现了上面的效果。
在具体的DataGrid定义的地方需要设置下面的一些方法
![](http://dev.yesky.com/imagelist/05/06/g8z4384zyg56.gif)
![](http://dev.yesky.com/imagelist/05/06/pyi4hzimi091.gif)
![](http://dev.yesky.com/imagelist/05/06/fcm404151258.gif)
![](http://dev.yesky.com/imagelist/05/06/s071g8q4b80f.gif)
![](http://dev.yesky.com/imagelist/05/06/u617922bi76j.gif)
![](http://dev.yesky.com/imagelist/05/06/7064ir1kv16a.gif)
![](http://dev.yesky.com/imagelist/05/06/862ccycs6566.gif)
![](http://dev.yesky.com/imagelist/05/06/ac0gci71ewx8.gif)
![](http://dev.yesky.com/imagelist/05/06/mnr80x4dcbh7.gif)
![](http://dev.yesky.com/imagelist/05/06/b3867vx6xti9.gif)
![](http://dev.yesky.com/imagelist/05/06/gsn2ej8363c3.gif)
![](http://dev.yesky.com/imagelist/05/06/deq3myxk0njk.gif)
![](http://dev.yesky.com/imagelist/05/06/2bp9d25kpj8l.gif)
![](http://dev.yesky.com/imagelist/05/06/95fhdrsi86vi.gif)
![](http://dev.yesky.com/imagelist/05/06/q4c483xu7100.gif)
![](http://dev.yesky.com/imagelist/05/06/32ch0788dhh9.gif)
![](http://dev.yesky.com/imagelist/05/06/nb0jh1340409.gif)
![](http://dev.yesky.com/imagelist/05/06/i06qsl063081.gif)
![](http://dev.yesky.com/imagelist/05/06/183xi0f1a5c7.gif)
![](http://dev.yesky.com/imagelist/05/06/6e1j4onx9e0d.gif)
![](http://dev.yesky.com/imagelist/05/06/4bq19x19tqm4.gif)
![](http://dev.yesky.com/imagelist/05/06/y570i95rln0d.gif)
![](http://dev.yesky.com/imagelist/05/06/zsi983y7pr9n.gif)
![](http://dev.yesky.com/imagelist/05/06/66rwn579234g.gif)
![](http://dev.yesky.com/imagelist/05/06/4157a03qig8m.gif)
![](http://dev.yesky.com/imagelist/05/06/j5d6o573du54.gif)
![](http://dev.yesky.com/imagelist/05/06/t7c98a28sot3.gif)
![](http://dev.yesky.com/imagelist/05/06/7kh0827t6lv6.gif)
![](http://dev.yesky.com/imagelist/05/06/85yy16eeu2tg.gif)
![](http://dev.yesky.com/imagelist/05/06/6642p5lgnw4v.gif)
![](http://dev.yesky.com/imagelist/05/06/0jjojw8135px.gif)
![](http://dev.yesky.com/imagelist/05/06/i12wx347b91j.gif)
![](http://dev.yesky.com/imagelist/05/06/4vxl50t1k6ns.gif)
![](http://dev.yesky.com/imagelist/05/06/mar0tqih17jy.gif)
![](http://dev.yesky.com/imagelist/05/06/u45ia69bot7l.gif)
![](http://dev.yesky.com/imagelist/05/06/89c011187g42.gif)
![](http://dev.yesky.com/imagelist/05/06/7e4n86oa1lb2.gif)
![](http://dev.yesky.com/imagelist/05/06/9xp06avcg4jh.gif)
![](http://dev.yesky.com/imagelist/05/06/pbx7e4ac1l55.gif)
![](http://dev.yesky.com/imagelist/05/06/sjzccqo0auvy.gif)
![](http://dev.yesky.com/imagelist/05/06/he1506l444m8.gif)
![](http://dev.yesky.com/imagelist/05/06/b32b07a46494.gif)
![](http://dev.yesky.com/imagelist/05/06/r273lju8akcp.gif)
![](http://dev.yesky.com/imagelist/05/06/502la45520iu.gif)
![](http://dev.yesky.com/imagelist/05/06/dy6r29o0gy36.gif)
![](http://dev.yesky.com/imagelist/05/06/h12p3ej9tk3k.gif)
![](http://dev.yesky.com/imagelist/05/06/rql5bsr13y3d.gif)
![](http://dev.yesky.com/imagelist/05/06/xc3e16811421.gif)
![](http://dev.yesky.com/imagelist/05/06/82gspf80nmja.gif)
![](http://dev.yesky.com/imagelist/05/06/begt8uot8r7z.gif)
![](http://dev.yesky.com/imagelist/05/06/us3ty3r1wgk0.gif)
![](http://dev.yesky.com/imagelist/05/06/jv46l3b02700.gif)
![](http://dev.yesky.com/imagelist/05/06/dck5r0d7d0c3.gif)
![](http://dev.yesky.com/imagelist/05/06/72n99c12763x.gif)
![](http://dev.yesky.com/imagelist/05/06/ox7w6ibj7m68.gif)
![](http://dev.yesky.com/imagelist/05/06/ql5t6e13ujdx.gif)
![](http://dev.yesky.com/imagelist/05/06/xo9s48dx7ado.gif)
![](http://dev.yesky.com/imagelist/05/06/az98ribzxq46.gif)
![](http://dev.yesky.com/imagelist/05/06/gla3300952be.gif)
![](http://dev.yesky.com/imagelist/05/06/jyl4srx3qfr2.gif)
![](http://dev.yesky.com/imagelist/05/06/6w63x1s7p6h4.gif)
![](http://dev.yesky.com/imagelist/05/06/pir867n3ir04.gif)
![](http://dev.yesky.com/imagelist/05/06/1337le2ef277.gif)
![](http://dev.yesky.com/imagelist/05/06/0pqd7kg09ar0.gif)
![](http://dev.yesky.com/imagelist/05/06/3b126565zm5g.gif)
![](http://dev.yesky.com/imagelist/05/06/xu4402e1417i.gif)
![](http://dev.yesky.com/imagelist/05/06/v42l5ku101xf.gif)
![](http://dev.yesky.com/imagelist/05/06/0y5c2391e9b6.gif)
![](http://dev.yesky.com/imagelist/05/06/0by24dx62ox2.gif)
![](http://dev.yesky.com/imagelist/05/06/xgg5538569ks.gif)
![](http://dev.yesky.com/imagelist/05/06/9447u7wns1k7.gif)
![](http://dev.yesky.com/imagelist/05/06/3ucd5705822t.gif)
![](http://dev.yesky.com/imagelist/05/06/vywvpv0eqg51.gif)
![](http://dev.yesky.com/imagelist/05/06/naq9ri9yct9w.gif)
![](http://dev.yesky.com/imagelist/05/06/t2162n82c70d.gif)
![](http://dev.yesky.com/imagelist/05/06/b9e9dpb542bg.gif)
![](http://dev.yesky.com/imagelist/05/06/r8h7496rky78.gif)
![](http://dev.yesky.com/imagelist/05/06/2n3j78568v4n.gif)
![](http://dev.yesky.com/imagelist/05/06/duphxbg77tl9.gif)
![](http://dev.yesky.com/imagelist/05/06/pjr7ci3vv29i.gif)
![](http://dev.yesky.com/imagelist/05/06/960u8oz5wo3c.gif)
![](http://dev.yesky.com/imagelist/05/06/57r44j87300a.gif)
![](http://dev.yesky.com/imagelist/05/06/wi6hj05y2oe6.gif)
![](http://dev.yesky.com/imagelist/05/06/1c0111x7a5ry.gif)
![](http://dev.yesky.com/imagelist/05/06/6016klwatr2y.gif)
![](http://dev.yesky.com/imagelist/05/06/950r643kmgnx.gif)
![](http://dev.yesky.com/imagelist/05/06/58ze8q867028.gif)
![](http://dev.yesky.com/imagelist/05/06/lt1hmbnvid6f.gif)
![](http://dev.yesky.com/imagelist/05/06/9mlbny3q4833.gif)
![](http://dev.yesky.com/imagelist/05/06/e20lje0tqvrc.gif)
![](http://dev.yesky.com/imagelist/05/06/mr6f0d5jpqwq.gif)
![](http://dev.yesky.com/imagelist/05/06/ulsx038vhqu6.gif)
![](http://dev.yesky.com/imagelist/05/06/2494up94pa51.gif)
![](http://dev.yesky.com/imagelist/05/06/1rbur6q95gi1.gif)
![](http://dev.yesky.com/imagelist/05/06/6la6q9br6u69.gif)
![](http://dev.yesky.com/imagelist/05/06/c69qbg7uf4c5.gif)
![](http://dev.yesky.com/imagelist/05/06/km9ocr3qb64v.gif)
![](http://dev.yesky.com/imagelist/05/06/kfo3922j8m42.gif)
![](http://dev.yesky.com/imagelist/05/06/7538tilb7602.gif)
![](http://dev.yesky.com/imagelist/05/06/4d93wro5u400.gif)
![](http://dev.yesky.com/imagelist/05/06/gs0py7j432m3.gif)
![](http://dev.yesky.com/imagelist/05/06/6wqlcx28844k.gif)
![](http://dev.yesky.com/imagelist/05/06/clk5i1ce58z6.gif)
![](http://dev.yesky.com/imagelist/05/06/jcle7dgdqoye.gif)
(
作者:xiaodele责任编辑:方舟)