如何给datagridview里的button添加事件
2014-11-12
展开全部
//我写了一个简单的代码,希望对你有点帮助~~
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace DGV_ButtonEvent_WIN
{
public partial class Form1 : Form
{
DataSet ds = new DataSet();
DataTable dtInfo = new DataTable();
string strConn = "Server=.;Trusted_Connection=SSPI;Database=DBTRUCK;Enlist=false;";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
string strSql = "SELECT * FROM CarFee";
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
sda.Fill(ds, "ds");
conn.Close();
dataGridView1.DataSource=ds.Tables[0];
}
//dataGridView1事件
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
if (column is DataGridViewButtonColumn)
{
//这里可以编写你需要的任意关于按钮事件的操作~
MessageBox.Show("按钮被点击");
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace DGV_ButtonEvent_WIN
{
public partial class Form1 : Form
{
DataSet ds = new DataSet();
DataTable dtInfo = new DataTable();
string strConn = "Server=.;Trusted_Connection=SSPI;Database=DBTRUCK;Enlist=false;";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
string strSql = "SELECT * FROM CarFee";
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
sda.Fill(ds, "ds");
conn.Close();
dataGridView1.DataSource=ds.Tables[0];
}
//dataGridView1事件
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
if (column is DataGridViewButtonColumn)
{
//这里可以编写你需要的任意关于按钮事件的操作~
MessageBox.Show("按钮被点击");
}
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询