当前位置导航:炫浪网>>网络学院>>在线图书>>程序设计教程>>Visual C# 基础入门

六十、DataGrid显示数据

本文将介绍如何利用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);
                }
            }
        }

        /// <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);
                }
            }
        }

        /// <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
            {
            }
        }

        /// <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>
       &

相关内容
赞助商链接