程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> DataGridView重繪代碼參考

DataGridView重繪代碼參考

編輯:.NET實例教程

1、CellFormatting事件,一般重繪單元格屬性。
    private Bitmap highPriImage;
    private Bitmap mediumPriImage;
    private Bitmap lowPriImage;
private void dataGridVIEw1_CellFormatting(object sender,
        System.Windows.Forms.DataGridVIEwCellFormattingEventArgs e)
    {
        // Set the background to red for negative values in the Balance column.
        if (dataGridVIEw1.Columns[e.ColumnIndex].Name.Equals("Balance"))
        {
            Int32 intValue;
            if (Int32.TryParse((String)e.Value, out intValue) &&
                (intValue < 0))
            {
                e.CellStyle.BackColor = Color.Red;
                e.CellStyle.SelectionBackColor = Color.DarkRed;
            }
        }

        // Replace string values in the Priority column with images.
        if (dataGridVIEw1.Columns[e.ColumnIndex].Name.Equals("Priority"))
        {
            // Ensure that the value is a string.
            String stringValue = e.Value as string;
            if (stringValue == null) return;

            // Set the cell ToolTip to the text value.
           

DataGridViewCell cell = dataGridVIEw1[e.ColumnIndex, e.RowIndex];
            cell.ToolTipText = stringValue;

            // Replace the string value with the image value.
            switch (stringValue)
            {
                case "high":
                    e.Value = highPriImage;
                    break;
                case "medium":
                    e.Value = mediumPriImage;
                    break;
                case "low":
                    e.Value = lowPriImage;
                    break;
            }
        }
    }


2、CellPainting事件,一般用於合並單元格用
Windows Forms DataGridVIEw 沒有提供合並單元格的功能,要實現合並單元格的功能就要在CellPainting事件中使用Graphics.DrawLine和 Graphics.DrawString 自己來“畫”。
下面的代碼可以對DataGridVIEw第1列內容相同的單元格進行合並:
 #region"合並單元格的測試"
private int? nextrow = null;
private int? nextcol = null;
private void dataGridView1_CellFormatting(object sender, System.Windows.Forms.DataGridVIEwCellFormattingEventArgs e)
{

    if (this.dataGridVIEw1.Columns["description"].Index == e.ColumnIndex && e.RowIndex >= 0)
    {
        if (this.nextcol != null & e.ColumnIndex == this.nextcol)
        {
            e.CellStyle.BackColor = Color.LightBlue;
            this.nextcol = null;
        }
        if (this.nextrow != null & e.RowIndex == nextrow)
        {
            e.CellStyle.BackColor = Color.LightPink;
            this.nextrow = null;
        }
        if (e.RowIndex != this.dataGridVIEw1.RowCount - 1)
        {
            if (e.Value.ToString() == this.dataGridVIEw1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
            {
                e.CellStyle.BackColor = Color.LightPink;
                nextrow = e.RowIndex + 1;
            }
        }

    }
    if (this.dataGridVIEw1.Columns["name"].Index == e.ColumnIndex && e.RowIndex >= 0)
    {
        if (e.Value.ToString() == this.dataGridVIEw1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())
        {
            e.CellStyle.BackColor = Color.LightBlue;
  &nbsp;         nextcol = e.ColumnIndex + 1;
        }
    }
}
//==========================
      
//繪制單元格
private void dataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridVIEwCellPaintingEventArgs e)
{

 

    //縱向合並
    if (this.dataGridVIEw1.Columns["description"].Index == e.ColumnIndex && e.RowIndex >= 0)
    {

        using (
            Brush gridBrush = new SolidBrush(this.dataGridVIEw1.GridColor),
            backColorBrush = new SolidBrush(e.CellStyle.BackColor))
        {
            using (Pen gridLinePen = new Pen(gridBrush))
            {
                // 擦除原單元格背景
                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                ////繪制線條,這些線條是單元格相互間隔的區分線條,
                ////因為我們只對列name做處理,所以datagridvIEw自己會處理左側和上邊緣的線條
                if (e.RowIndex != this.dataGridVIEw1.RowCount - 1)
                {
                    if (e.Value.ToString() != this.dataGridVIEw1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
           

;         {

                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                        e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下邊緣的線
                        //繪制值
                        if (e.Value != null)
                        {
                            e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                                Brushes.Crimson, e.CellBounds.X + 2,
                                e.CellBounds.Y + 2, StringFormat.GenericDefault);
                        }
                    }
                }
               

else
                {
           &nbsp;        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                        e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下邊緣的線
                    //繪制值
                    if (e.Value != null)
                    {
                        e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                            Brushes.Crimson, e.CellBounds.X + 2,
                            e.CellBounds.Y + 2, StringFormat.GenericDefault);
                    }
                }
                //右側的線
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                    e.CellBounds.Top, e.CellBounds.Right - 1,
                    e.CellBounds.Bottom - 1);

                e.Handled = true;
     &n
bsp;      }
        }
    }

    //橫向合並
    if (this.dataGridVIEw1.Columns["name"].Index == e.ColumnIndex && e.RowIndex >= 0)
    {

        using (
            Brush gridBrush = new SolidBrush(this.dataGridVIEw1.GridColor),
            backColorBrush = new SolidBrush(e.CellStyle.BackColor))
        {
            using (Pen gridLinePen = new Pen(gridBrush))
            {
                // 擦除原單元格背景
                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                if (e.Value.ToString() != this.dataGridVIEw1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())
                {

                    //右側的線
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,
                        e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                    //繪制值
                    if (e

.Value != null)
                    {
                        e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                            Brushes.Crimson, e.CellBounds.X + 2,
                            e.C
ellBounds.Y + 2, StringFormat.GenericDefault);
                    }
                }

                //下邊緣的線
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                                            e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                e.Handled = true;
            }
        }

    }

}

#endregion

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved