日期:2014-05-18 浏览次数:21122 次
        private void DrawDataGridView(DataGridView d, Graphics g, ref int startRowIndex)
        {
            Rectangle r = GetControlRect(d);
            if (d.ColumnHeadersDefaultCellStyle.BackColor != Color.White)
                g.FillRectangle(new SolidBrush(d.ColumnHeadersDefaultCellStyle.BackColor), r.X, r.Y, r.Width, d.ColumnHeadersHeight);
            float colWidth = 0;
            for (int j = 0; j < d.Columns.Count; j++)
            {
                if (d.Columns[j].Visible)
                    colWidth += d.Columns[j].Width;
                //((float)d.Columns[i].Width /(float)d.Width )
            }
            int startPx = r.X;
            int[] ColumnWidth = new int[d.Columns.GetColumnCount(DataGridViewElementStates.Visible)];
            DataGridViewColumn column = d.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
            int idx = 0;
            while (column != null)
            {
                Rectangle rtxt = new Rectangle(startPx, r.Y, (int)(((float)column.Width / colWidth) * d.Width), d.ColumnHeadersHeight);
                ColumnWidth[idx] = rtxt.Width;
                startPx += rtxt.Width;
                SizeF sf = g.MeasureString(column.HeaderText, d.ColumnHeadersDefaultCellStyle.Font, rtxt.Width);
                switch (d.ColumnHeadersDefaultCellStyle.Alignment)
                {
                    case DataGridViewContentAlignment.BottomCenter:
                    case DataGridViewContentAlignment.MiddleCenter:
                    case DataGridViewContentAlignment.TopCenter:
                        if (sf.Width <= rtxt.Width && sf.Height <= rtxt.Height)
                        {
                            rtxt = new Rectangle(rtxt.X + (rtxt.Width - (int)sf.Width) / 2, rtxt.Y + (rtxt.Height - (int)sf.Height) / 2, (int)sf.Width + 1, (int)sf.Height + 1);
                        }
                        break;
                    default:
                        if (sf.Height < rtxt.Height)
                        {
                            rtxt = new Rectangle(rtxt.X, rtxt.Y + (rtxt.Height - (int)sf.Height) / 2, (int)sf.Width + 1, (int)sf.Height + 1);
                        }
                        break;
                }
                g.DrawString(column.HeaderText, d.ColumnHeadersDefaultCellStyle.Font, new SolidBrush(d.ColumnHeadersDefaultCellStyle.ForeColor), rtxt);
                column = d.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
                idx++;
            }
            if (startRowIndex < 0 || startRowIndex >= d.Rows.Count)
                return;
            SolidBrush cellcolor = new SolidBrush(d.DefaultCellStyle.ForeColor);
            //for (int i = 0; i < rowCount; i++)
            int i = 0;