关于DataGridView自定义列的问题 我用的是VS2005.我想在WinForm加载时,在DataGridView中增加一个自定义列"操作",该列有二个按钮"修改","删除",点击按钮触发相应事件,请问下这个应该怎么做? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以有两列,一列修改,一列删除先在DataGridView中添加两列这样的按钮列,隐藏着,看你什么 时候需要操作就显示出来 我要在一个列里放二个 linkbutton 这个怎么来做 你是看网页的惯性思维吧,你做这样的2个按钮在winform里面的datagridview不太好实现,你完全可以加2列datagridviewbuttoncolumn嘛 分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+=...等事件............................... } }} 楼主是用web的编程方式处理winform的问题。winform下,直接把按扭放到工具条中,焦点在DataGridView的单元格中。如果想修改、删除,根据Row.Index Column.Index 直接操作就行了。WebForm模式(浏览器中),对界面控件的控制比较弱,那种处理方式是不得已而为之。 c#验证码如何识别 本人想用C#开发一个模拟DNF的加点器.请问有朋友能帮帮忙吗? 模拟网页登陆的问题 高分求动态报表实例,顶者有分 窗体如何与用户控件交互 了解Datagrid的朋友进来,进来有分 日期格式判斷和轉換 “刷新”的问题 关于项目外包 怎样锁定一个窗体,使它的大小不可以调整? 请各位大牛提供思路 关于TabControl中不同页面间交互的问题?先谢为快!!!急求高手解答
隐藏着,看你什么 时候需要操作就显示出来
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+=...等事件...............................
}
}
}
winform下,直接把按扭放到工具条中,焦点在DataGridView的单元格中。
如果想修改、删除,根据Row.Index Column.Index 直接操作就行了。WebForm模式(浏览器中),对界面控件的控制比较弱,那种处理方式是不得已而为之。