我用的是VS2005.我想在WinForm加载时,在DataGridView中增加一个自定义列"操作",该列有二个按钮"修改","删除",点击按钮触发相应事件,请问下这个应该怎么做?

解决方案 »

  1.   

    可以有两列,一列修改,一列删除先在DataGridView中添加两列这样的按钮列,
    隐藏着,看你什么 时候需要操作就显示出来
      

  2.   

    我要在一个列里放二个 linkbutton 这个怎么来做
      

  3.   

    你是看网页的惯性思维吧,你做这样的2个按钮在winform里面的datagridview不太好实现,你完全可以加2列datagridviewbuttoncolumn嘛
      

  4.   

    分2列最简单了啊,如果要放一列里就要定制了using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Windows.Forms.VisualStyles;namespace WindowsApplication112
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            DataGridView DGV = new DataGridView();
                DGV.Parent = this;
                DGV.Dock = DockStyle.Fill;
                DGV.RowHeadersVisible = false;
                DGV.ColumnHeadersVisible = false;            DGV.Columns.Add(new TwoButtonColumn());
                DGV.Columns.Add(new TwoButtonColumn());            DGV.Rows.Add(3);
            }        class TwoButtonColumn : DataGridViewColumn
            {
                public TwoButtonColumn()
                {
                    this.CellTemplate = new TwoButtonCell();
                }
            }        class TwoButtonCell : DataGridViewCell
            {
                Button B1 = new Button();
                Button B2 = new Button();
                bool B1Down = false;
                bool B2Down = false;            public TwoButtonCell()
                {
                    this.ValueType = typeof(String);                B1.Text = "修改";
                    B2.Text = "删除";
                }            protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
                {
                    graphics.FillRectangle(Brushes.White, cellBounds);                B1.Location = cellBounds.Location;
                    B2.Location = Point.Add(cellBounds.Location, new Size(2 + cellBounds.Width / 2, 0));
                    B1.Size = new Size(cellBounds.Width / 2, cellBounds.Height);
                    B2.Size = new Size(cellBounds.Width / 2, cellBounds.Height);                ButtonRenderer.DrawButton(graphics, new Rectangle(B1.Location, B1.Size),
                        B1Down ? PushButtonState.Pressed : PushButtonState.Normal);
                    ButtonRenderer.DrawButton(graphics, new Rectangle(B2.Location, B2.Size),
                        B2Down ? PushButtonState.Pressed : PushButtonState.Normal);                Size B1StringSize = TextRenderer.MeasureText(B1.Text, B1.Font);
                    Size B2StringSize = TextRenderer.MeasureText(B2.Text, B2.Font);                graphics.DrawString(B1.Text, B1.Font, new SolidBrush(B1.ForeColor),
                         Point.Add(B1.Location, new Size((B1.Width - B1StringSize.Width) / 2,
                         (B1.Height - B1StringSize.Height) / 2)));
                    graphics.DrawString(B2.Text, B2.Font, new SolidBrush(B2.ForeColor),
                         Point.Add(B2.Location, new Size((B2.Width - B2StringSize.Width) / 2,
                         (B2.Height - B2StringSize.Height) / 2)));
                }            protected override void OnMouseDown(DataGridViewCellMouseEventArgs e)
                {
                    Rectangle Test = this.DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);                if (Test.Contains(this.DataGridView .PointToClient(  Control.MousePosition)))
                        if (e.Location.X < Test.Width / 2)
                            B1Down = true;
                        else
                            B2Down = true;                base.OnMouseDown(e);
                }            protected override void OnMouseUp(DataGridViewCellMouseEventArgs e)
                {
                    B1Down = false;
                    B2Down = false;                base.OnMouseDown(e);
                }            // 以上代码实现了双按钮列的显示和相应鼠标
                  // 但是还要相应click+=...等事件...............................
            }
        }
    }
      

  5.   

    楼主是用web的编程方式处理winform的问题。
    winform下,直接把按扭放到工具条中,焦点在DataGridView的单元格中。
    如果想修改、删除,根据Row.Index Column.Index 直接操作就行了。WebForm模式(浏览器中),对界面控件的控制比较弱,那种处理方式是不得已而为之。