C#中.DataGridView控件,添加右键按钮,代码如何写.!!

C#中.DataGridView控件,添加右键按钮,代码如何写.!!,第1张

没听说过右键按钮,是右键菜单差不多...

我的是VS2005,和2003的可能控件上名字有点差别,不过不会差太多...

首先在窗体上添加一个contextMenuStrip控件(工具栏,自己找),接着对contextMenuStrip控件编辑好需要的右键菜单内容和功能,然后dataGridView控件,将其contextMenuStrip属性设置为contextMenuStrip1(contextMenuStrip控件名称)即可在dataGridView控件中使用自定义的右键菜单了.

<asp:datagridview ID="d1">

<asp:itemtemplate>

<table>

<tr>

<td><asp:Button ID="Button1" Text="修改"></asp:Button></td>

<td><asp:Button ID="Button2" Text="删除"></asp:Button></td>

</tr>

</table>

</asp:itemtemplate>

</asp:datagridview>

public Form1()

{

InitializeComponent()

this.Load += new EventHandler(Form1_Load)

}

List<string>strSourec = new List<string>{ "1", "2" }

void Form1_Load(object sender, EventArgs e)

{

foreach (string item in strSourec)

{

DataGridViewButtonColumn Column1 = new DataGridViewButtonColumn()

Column1.HeaderText = item

this.dataGridView1.Columns.Add(Column1)

}

DataGridViewRow dr = new DataGridViewRow()

for (int i = 0i <strSourec.Counti++)

{

DataGridViewButtonCell dgvbc = new DataGridViewButtonCell()

dgvbc.Value = strSourec[i]

dr.Cells.Add(dgvbc)

}

dataGridView1.Rows.Add(dr)

this.dataGridView1.CellMouseDown += new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseDown)

}

void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)

{

if (dataGridView1[e.ColumnIndex, e.RowIndex].Value == null) return

MessageBox.Show(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString())

}

参考:

http://zhidao.baidu.com/link?url=JImY3prv3dVBJlYSnTOIenUJnx6xyRRVFZYpnmg_flu755xA7RgADZtRswoKr1QMcCjClxWjmbHtRgsyFnZYya

不明白的再私信我


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11606661.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存