/// <summary> /// dgv columnbutton click事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgvSheet_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (dgvSheet.Columns[e.ColumnIndex].Name == "radiob")//此处索引列可以使name、也可以使headertext,看具体的设置。 { MessageBox.Show(e.RowIndex.ToString() + "行的按钮被点击了。");} //从数据库读取需要绑定的表的字段 SqlConnection conn = sqlConn(); conn.Open(); SqlCommand comm = new SqlCommand(string.Format("select syscolumns.[name] from sysobjects,syscolumns where sysobjects.id=syscolumns.id and sysobjects.[name]='{0}'", sqlTableName),conn ); SqlDataReader reader = comm.ExecuteReader(); DataTable dt = new DataTable(); dt.Load(reader); //给dt 增加两列,这一列的数据来自于EXCEL的字段选择。 dt.Columns.Add("Excel列表"); dt.Columns.Add("默认字段"); ds.Tables.Add(dt); FillData(); dgvRange.DataSource = ds.Tables[0];} private void FillData() { DataGridViewComboBoxColumn dgcmb = new DataGridViewComboBoxColumn(); dgcmb = (DataGridViewComboBoxColumn)dgvRange.Columns["Excel列表"]; dgcmb.DisplayStyleForCurrentCellOnly = true; dgcmb.DataPropertyName = "Excel列表"; dgcmb.DataSource = ds.Tables[1]; //组合框下拉列表设置 dgcmb.DisplayMember = "Excel列表"; //组合框下拉值设置 dgcmb.ValueMember = "Excel列表";dgcmb.DropDownWidth = 280; dgcmb.MaxDropDownItems = 20; }