DataGridView实用攻略-响应DataGridView的按钮列

按钮列也是DataGridView的常用列,我们增加按钮列后还要为按钮增加功能。
首先,构建按钮列。
  1.             DataGridViewButtonColumn buttons = new DataGridViewButtonColumn();
  2.             {
  3.                 buttons.HeaderText = "查看";
  4.                 buttons.Text = "查看";
  5.                 buttons.UseColumnTextForButtonValue = true;
  6.                 buttons.DisplayIndex = 6;
  7.             }
  8.             dataGridView1.Columns.Add(buttons);
接着,定义按钮的相关事件。
  1.         private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  2.         {
  3.             int CIndex = e.ColumnIndex;
  4.             int RIndex = e.RowIndex;
  5.             cqchz1.chushen.orderno = dataGridView1.Rows[RIndex].Cells[0].Value.ToString();
  6.             if (CIndex == 6)
  7.             {
  8.                 SqlConnection conn = new SqlConnection();
  9.                 conn.ConnectionString = cqchz_mis.Connection.Connstring;
  10.                 string cmdstr1;
  11.                 cmdstr1 = "select 机构,检查员,检查人日数,路途人日数,是否报销,角 色 from first_jianchayuan where 报告编号 = '" + cqchz1.chushen.orderno + "'";
  12.                 conn.Open();
  13.                 SqlCommand Cmd1 = new SqlCommand(cmdstr1, conn);
  14.                 SqlDataAdapter Adpt = new SqlDataAdapter();
  15.                 Adpt.SelectCommand = Cmd1;
  16.                 DataSet Ds = new DataSet();
  17.                 Adpt.Fill(Ds, "result");
  18.                 DataTable tb = new DataTable();
  19.                 tb = Ds.Tables["result"];
  20.                 DataView dv = new DataView(tb);
  21.                 dataGridView2.DataSource = dv;               
  22.             }
  23.         }
需要注意的是, dataGridView1_CellContentClick要在界面双击dataGridView1生成后台的代码,否则要在相关窗体的Designer_cs文件的dataGridView1部分里加上
  1. this.dataGridView1.CellContentClick += 
  2. new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值