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