您的位置:首页 > 其它

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

2013-06-04 18:17 453 查看
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("按钮被点击");
                }
            }
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: