DataGridView implements a column of cells to display multiple modes of operation

Source: Internet
Author: User


Rt:


The main methods used:

How to draw in cellpainting

        private void Datagridview1_cellpainting (object sender, DataGridViewCellPaintingEventArgs e) { if (e.columnindex >= 0 && e.rowindex >= 0) {if (this.datagridview1.columns[e.co Lumnindex]. HeaderText = = "Op") {Employee model = This.datagridview1.rows[e.rowindex].
                    Databounditem as Employee; if (model!= null) {StringFormat SF = StringFormat.GenericDefault.Clone () as
                        StringFormat; sf.
                        FormatFlags = Stringformatflags.displayformatcontrol; sf.
                        Alignment = Stringalignment.center; sf.
                        LineAlignment = Stringalignment.center; sf.
                        trimming = Stringtrimming.ellipsischaracter;
                        E.paintbackground (E.cellbounds, false); int MD = model.
                        Age% 3;
            string text = "Delete";            if (model.
                        Age% 3 = 1) {text = "Modify";
                                Switch (MD) {Case 1:
                                E.graphics.drawstring (text, This.dataGridView1.Font, brushes.red, E.cellbounds, SF);
                            Break
                                Case 2:string text2 = "delete"; if (model.
                                Age% 3 = 2) {Text2 = "modify";

                                } SizeF size = e.graphics.measurestring (text, This.dataGridView1.Font);
                                SizeF size2 = e.graphics.measurestring (Text2, This.dataGridView1.Font); float z = size. Width/(size. Width + size2.
                                Width); Float z2 = size2. Width/(size.Width + size2.
                                Width);
                                RectangleF rect1 = new RectangleF (E.cellbounds.left, e.cellbounds.top, E.cellbounds.width * z, e.cellbounds.height); RectangleF rect2 = new RectangleF (rect1.
                                Right, E.cellbounds.top, E.cellbounds.width * Z2, E.cellbounds.height);
                                E.graphics.drawstring (text, This.dataGridView1.Font, brushes.red, Rect1, SF);
                                E.graphics.drawstring (Text2, This.dataGridView1.Font, brushes.red, Rect2, SF);
                            Break Default:e.graphics.drawstring (text, This.dataGridView1.Font, brushes.red, E.cellbounds,
                                SF); 
                        Break
                } e.handled = true;
 }
            }
        }

The action is processed in the Cellmouseclick event.

        private void Datagridview1_cellmouseclick (object sender, DataGridViewCellMouseEventArgs e) {P
            oint pt = e.location; if (e.columnindex >= 0 && e.rowindex >= 0) {if (this.datagridview1.columns[e.co Lumnindex]. HeaderText = = "Op") {Employee model = This.datagridview1.rows[e.rowindex].
                    Databounditem as Employee; if (model!= null) {int MD = model.
                        Age% 3;
                        string text = "Delete"; if (model.
                        Age% 3 = 1) {text = "Modify";
                           
                                Switch (MD) {Case 1:
                            Break
                         Case 2:string text2 = "delete";       if (model.
                                Age% 3 = 2) {Text2 = "modify";
                                } Graphics g = This.dataGridView1.CreateGraphics ();

                                SizeF size = g.measurestring (text, This.dataGridView1.Font);
                                SizeF size2 = g.measurestring (Text2, This.dataGridView1.Font); float z = size. Width/(size. Width + size2.
                                Width); Float z2 = size2. Width/(size. Width + size2.
                                Width); Rectangle rect = new Rectangle (0, 0, This.datagridview1.columns[e.columnindex]. Width, This.datagridview1.rows[e.rowindex].
                                Height); RectangleF rect1 = new RectangleF (rect. Left, rect. Top, Rect. Width * Z, rect.
                                Height); RectangleF rect2 = new RectangleF (rect1. Right, rect. Top, Rect. Width * Z2, rect.
            Height);                    if (rect1.
                                Contains (PT)) {MessageBox.Show ("1111111"); } if (Rect2.
                                Contains (PT)) {MessageBox.Show ("2222222");
                            } break;
                        Default:break; } 
                    }
                }
            }
            
        }


Image above:


Source: http://download.csdn.net/detail/zanfeng/9453608


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.