C#如何在DataGridView的RowHeader顯示字符串和圖標

href="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_filelist.xml" rel="File-List" />

【資料收集自網絡】


在DataGridView的CellPainting事件中,加入如下代碼

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{    href="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_filelist.xml" rel="File-List" />

if (e.RowIndex >= 0 && e.ColumnIndex == -1)

{

Rectangle newRect = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Height, e.CellBounds.Height);

//新建一個圖標

System.Drawing.Icon ico = new Icon("F://xx.ico");

using (Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),

backColorBrush = new SolidBrush(e.CellStyle.BackColor))

{

using (Pen gridLinePen = new Pen(gridBrush,2))

        {

            // Erase the cell.

            e.Graphics.FillRectangle(backColorBrush, e.CellBounds);        

 

            //劃線

            Point p1 = new Point(e.CellBounds.Left+e.CellBounds.Width, e.CellBounds.Top);

            Point p2 = new Point(e.CellBounds.Left+e.CellBounds.Width, e.CellBounds.Top+e.CellBounds.Height);

            Point p3 = new Point(e.CellBounds.Left,e.CellBounds.Top+ e.CellBounds.Height);

            Point[] ps = new Point[]{p1,p2,p3};

            e.Graphics.DrawLines(gridLinePen,ps);

 

            //畫圖標

            e.Graphics.DrawIcon(ico, newRect);

            //畫字符串

            e.Graphics.DrawString("123", e.CellStyle.Font, Brushes.Crimson, e.CellBounds.Left + 20, e.CellBounds.Top, StringFormat.GenericDefault);

            e.Handled = true;

          }

      }

}

 

}

 

 

例外還有其他方法供參考,也是在DataGridView的CellPainting事件中

href="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_filelist.xml" rel="File-List" />

//A顯示在-1(即RowHeader)列之上

if (e.RowIndex >= 0 && e.ColumnIndex == 0)

{

e.Graphics.DrawString("A", this.dataGridView1.Font, Brushes.Red, 20, e.CellBounds.Top + 5);

}

 

//A則被-1(即RowHeader)列覆蓋。

if (e.RowIndex >= 0 && e.ColumnIndex == -1)

{

e.Graphics.DrawString("A", this.dataGridView1.Font, Brushes.Red, 20, e.CellBounds.Top + 5);

}

 

//XX圖片在-1(即RowHeader)列之上

if (e.RowIndex >= 0 && e.ColumnIndex == 0)

{

Image img = Image.FromFile(@"F:/xx.bmp");

    e.Graphics.DrawImage(img, 0, e.CellBounds.Top);


}

 

//XX圖片被-1(即RowHeader)列覆蓋

if (e.RowIndex >= 0 && e.ColumnIndex == -1)

{

Image img = Image.FromFile(@"F:/xx.bmp");

e.Graphics.DrawImage(img, 0, e.CellBounds.Top);

}

 

//XX圖片被-1(即RowHeader)列覆蓋

if (e.RowIndex >= 0 && e.ColumnIndex == -1)

{

Image img = Image.FromFile(@"F:/xx.bmp");

e.Graphics.DrawImage(img, e.CellBounds.Left, e.CellBounds.Top);

}

 

//XX圖片被0列覆蓋

if (e.RowIndex >= 0 && e.ColumnIndex == 0)

{

Image img = Image.FromFile(@"F:/xx.bmp");

e.Graphics.DrawImage(img, e.CellBounds.Left, e.CellBounds.Top);

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章