datagridview 中添加了一个button类型的列,怎么写button的事件

时间:2024-01-12 23:01:44
 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[];
} //dataGridView1事件
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= )
{
DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
if (column is DataGridViewButtonColumn)
{
//这里可以编写你需要的任意关于按钮事件的操作~
MessageBox.Show("按钮被点击");
}
     //DGV下拉框的取值
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex-].Value.ToString());
}
}
}
}