首页新闻找找看学习计划

请教大神:C#像这样的界面如何做,求实现思路

0
悬赏园豆:50 [待解决问题]

 

 

 

 

请教大神:C#像这样的界面如何做,求实现思路

boy专注的主页 boy专注 | 初学一级 | 园豆:152
提问于:2015-03-03 15:59
< >
分享
所有回答(6)
0

<table width="98%">

<tr rolspan="2"><td colspan="2"></td><td></td><td></td></tr>

<tr><td></td><td></td><td></td></tr>

--循环绑定数据

for(var i=0;i<10;i++){

<tr><td></td><td></td><td></td></tr>

}

</table>

KingMi | 园豆:1098 (小虾三级) | 2015-03-03 16:07

谢谢你的回答,我想实现的是在winform中点击文本框获取焦点弹出下拉框,选择某项后,把某些值填充到主表格内,winform接触较少,所以求思路。

支持(0) 反对(0) boy专注 | 园豆:152 (初学一级) | 2015-03-03 16:14

@boy专注: 我说的是webform页面相关

支持(0) 反对(0) KingMi | 园豆:1098 (小虾三级) | 2015-03-03 16:26
0

Winform的话,

一个就是你自己写事件代码,我记得微软有个类似的Sample.

另外一个就是找现成的控件,比如DexExpress, Componnet One, telerik , .... 市场上大约有那么5~6款商业控件

把GRID做得你能想象到的功能都有了。

爱编程的大叔 | 园豆:30212 (高人七级) | 2015-03-03 16:25
0

用第三方的控件比较容易实现些

xiaocong_soft | 园豆:556 (小虾三级) | 2015-03-04 10:51
0
    public class BoundGridView : DataGridView
    {
        private int _baseColumnHeadHeight;

        public BoundGridView()
            : base()
        {
            this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            _baseColumnHeadHeight = this.ColumnHeadersHeight;
            this.Headers = new HeaderCollection(this.Columns);
            //  this.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
            this.RowHeadersWidth = 80;

        }

        public HeaderCollection Headers { get; private set; }

        protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush solidBrush = new SolidBrush(this.RowHeadersDefaultCellStyle.ForeColor);
            //StringFormat format = new StringFormat();
            //format.LineAlignment = StringAlignment.Center;
            //format.Alignment = StringAlignment.Center;
            e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, solidBrush, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 5);
            base.OnRowPostPaint(e);
        }

        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1 && e.ColumnIndex != -1)
            {
                int lev = this.Headers.GetHeaderLevels();
                this.ColumnHeadersHeight = (lev + 1) * _baseColumnHeadHeight;
                for (int i = 0; i <= lev; i++)
                {
                    HeaderItem tempHeader = this.Headers.GetHeaderByLocation(e.ColumnIndex, i);
                    if (tempHeader == null || i != tempHeader.EndY || e.ColumnIndex != tempHeader.StartX) continue;
                    DrawHeader(tempHeader, e);
                }
            }
            else
            {
                base.OnCellPainting(e);
                return;
            }
            e.Handled = true;
        }

        private int ComputeWidth(int startX, int endX)
        {
            int width = 0;
            for (int i = startX; i <= endX; i++)
                width += this.Columns[i].Width;
            return width;
        }

        private int ComputeHeight(int startY, int endY)
        {
            return _baseColumnHeadHeight * (endY - startY + 1);
        }

        private void DrawHeader(HeaderItem item, DataGridViewCellPaintingEventArgs e)
        {
            if (this.ColumnHeadersHeightSizeMode != DataGridViewColumnHeadersHeightSizeMode.DisableResizing)
                this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            int lev = this.Headers.GetHeaderLevels();
            lev = (lev - item.EndY) * _baseColumnHeadHeight;

            SolidBrush backgroundBrush = new SolidBrush(e.CellStyle.BackColor);
            SolidBrush lineBrush = new SolidBrush(this.GridColor);
            Pen linePen = new Pen(lineBrush);
            StringFormat foramt = new StringFormat();
            foramt.Alignment = StringAlignment.Center;
            foramt.LineAlignment = StringAlignment.Center;

            Rectangle headRec = new Rectangle(e.CellBounds.Left, lev, ComputeWidth(item.StartX, item.EndX) - 1, ComputeHeight(item.StartY, item.EndY) - 1);
            e.Graphics.FillRectangle(backgroundBrush, headRec);
            e.Graphics.DrawLine(linePen, headRec.Left, headRec.Bottom, headRec.Right, headRec.Bottom);
            e.Graphics.DrawLine(linePen, headRec.Right, headRec.Top, headRec.Right, headRec.Bottom);
            e.Graphics.DrawString(item.Content, this.ColumnHeadersDefaultCellStyle.Font, Brushes.Black, headRec, foramt);
        }

        //private void DrawBodyCell(DataGridViewCellPaintingEventArgs e)
        //{
        //    int index = e.RowIndex;
        //    int cells = e.ColumnIndex;

        //    List<ColumnItem> dataList=(List<ColumnItem>)this.DataSource;

        //    if (cells<dataList.Count&&dataList[cells].Cells.Count > index)
        //    {

        //        SolidBrush backgroundBrush = new SolidBrush(e.CellStyle.BackColor);
        //        SolidBrush lineBrush = new SolidBrush(this.GridColor);
        //        Pen linePen = new Pen(lineBrush);
        //        StringFormat foramt = new StringFormat();
        //        foramt.Alignment = StringAlignment.Center;
        //        foramt.LineAlignment = StringAlignment.Center;

        //        Rectangle headRec = new Rectangle(e.CellBounds.Left, e.CellBounds.Bottom, e.CellBounds.Width, e.CellBounds.Height);
        //        e.Graphics.FillRectangle(backgroundBrush, headRec);
        //        e.Graphics.DrawLine(linePen, headRec.Left, headRec.Bottom, headRec.Right, headRec.Bottom);
        //        e.Graphics.DrawLine(linePen, headRec.Right, headRec.Top, headRec.Right, headRec.Bottom);
        //        e.Graphics.DrawString(dataList[cells].Cells[index], this.ColumnHeadersDefaultCellStyle.Font, Brushes.Black, headRec, foramt);
        //    }
        //}
    }



    public class HeaderItem
    {
        private int _startX;
        private int _startY;
        private int _endX;
        private int _endY;
        private bool _baseHeader;

        public HeaderItem(int startX, int endX, int startY, int endY, string content)
        {
            this._endX = endX;
            this._endY = endY;
            this._startX = startX;
            this._startY = startY;
            this.Content = content;
        }

        public HeaderItem(int x, int y, string content)
            : this(x, x, y, y, content)
        {

        }

        public HeaderItem()
        {

        }

        public static HeaderItem CreateBaseHeader(int x, int y, string content)
        {
            HeaderItem header = new HeaderItem();
            header._endX = header._startX = x;
            header._endY = header._startY = y;
            header._baseHeader = true;
            header.Content = content;
            return header;
        }

        public int StartX
        {
            get { return _startX; }
            set
            {
                if (value > _endX)
                {
                    _startX = _endX;
                    return;
                }
                if (value < 0) _startX = 0;
                else _startX = value;
            }
        }

        public int StartY
        {
            get { return _startY; }
            set
            {
                if (_baseHeader)
                {
                    _startY = 0;
                    return;
                }
                if (value > _endY)
                {
                    _startY = _endY;
                    return;
                }
                if (value < 0) _startY = 0;
                else _startY = value;
            }
        }

        public int EndX
        {
            get { return _endX; }
            set
            {
                if (_baseHeader)
                {
                    _endX = _startX;
                    return;
                }
                if (value < _startX)
                {
                    _endX = _startX;
                    return;
                }
                _endX = value;
            }
        }

        public int EndY
        {
            get { return _endY; }
            set
            {
                if (value < _startY)
                {
                    _endY = _startY;
                    return;
                }
                _endY = value;
            }
        }

        public bool IsBaseHeader
        { get { return _baseHeader; } }

        public string Content { get; set; }
    }

    public class HeaderCollection
    {
        private List<HeaderItem> _headerList;
        private bool _iniLock;

        public DataGridViewColumnCollection BindCollection { get; set; }

        public HeaderCollection(DataGridViewColumnCollection cols)
        {
            _headerList = new List<HeaderItem>();
            BindCollection = cols;
            _iniLock = false;
        }

        public int GetHeaderLevels()
        {
            int max = 0;
            foreach (HeaderItem item in _headerList)
                if (item.EndY > max)
                    max = item.EndY;

            return max;
        }

        public List<HeaderItem> GetBaseHeaders()
        {
            List<HeaderItem> list = new List<HeaderItem>();
            foreach (HeaderItem item in _headerList)
                if (item.IsBaseHeader) list.Add(item);
            return list;
        }

        public HeaderItem GetHeaderByLocation(int x, int y)
        {
            if (!_iniLock) InitHeader();
            HeaderItem result = null;
            List<HeaderItem> temp = new List<HeaderItem>();
            foreach (HeaderItem item in _headerList)
                if (item.StartX <= x && item.EndX >= x)
                    temp.Add(item);
            foreach (HeaderItem item in temp)
                if (item.StartY <= y && item.EndY >= y)
                    result = item;

            return result;
        }

        public IEnumerator GetHeaderEnumer()
        {
            return _headerList.GetEnumerator();
        }

        public void AddHeader(HeaderItem header)
        {
            this._headerList.Add(header);
        }

        public void AddHeader(int startX, int endX, int startY, int endY, string content)
        {
            this._headerList.Add(new HeaderItem(startX, endX, startY, endY, content));
        }

        public void AddHeader(int x, int y, string content)
        {
            this._headerList.Add(new HeaderItem(x, y, content));
        }

        public void RemoveHeader(HeaderItem header)
        {
            this._headerList.Remove(header);
        }

        public void RemoveHeader(int x, int y)
        {
            HeaderItem header = GetHeaderByLocation(x, y);
            if (header != null) RemoveHeader(header);
        }

        private void InitHeader()
        {
            _iniLock = true;
            for (int i = 0; i < this.BindCollection.Count; i++)
                if (this.GetHeaderByLocation(i, 0) == null)
                    this._headerList.Add(HeaderItem.CreateBaseHeader(i, 0, this.BindCollection[i].HeaderText));
            _iniLock = false;
        }
    }

调用实例:

/// <summary>
        /// 绑定数据到gridview
        /// </summary>
        private void BindData2GridView(BaseModel baseModel, Int32 type, BoundGridView bgv)
        {
            try
            {
                bgv.Columns.Clear();
                for (int i = 0; i < baseModel.BaseSchema.Count; i++)
                {
                    DataGridViewTextBoxColumn cell = new DataGridViewTextBoxColumn();
                    cell.ReadOnly = true;
                    cell.HeaderText = baseModel.BaseSchema[i].Alias;
                    cell.Name = baseModel.BaseSchema[i].Name;
                    cell.DataPropertyName = baseModel.BaseSchema[i].Name;
                    bgv.Columns.Add(cell);
                }

                var groups = (from item in baseModel.BaseSchema select item.Group).Distinct();

                int start = 0;

                foreach (String g in groups)
                {
                    int count = baseModel.BaseSchema.Count(col => col.Group == g);
                    bgv.Headers.AddHeader(start, start + count - 1, 1, 1, g);
                    start = start + count;
                }

                if (type == 1)
                {
                    if (baseModel.BaseData == null || baseModel.BaseData.Rows.Count == 0)
                    {
                        MessageBox.Show("未能读取数据,请确认您选择的Excel格式正确或者Sheet名称正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    bgv.DataSource = baseModel.BaseData;
                }
                else
                {
                    bgv.DataSource = baseModel.ExportData;
                }
            }
            catch
            {

            }
        }
Raysonxin | 园豆:13 (初学一级) | 2015-03-04 16:35
效果是这样的
支持(0) 反对(0) Raysonxin | 园豆:13 (初学一级) | 2015-03-04 16:46
0

我这边是用jQuery自行封装的

Y2zz | 园豆:388 (菜鸟二级) | 2015-03-12 10:44
0

好像有个TableLayout控件,您试试,如果是Winform的话

毛毛虫 | 园豆:240 (菜鸟二级) | 2015-03-23 13:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册