DataGridview动态添加列
1.获取数据源(select * from table名称)
2.动态绑定数据源
1 private void GetTableInfo(DataTable dt) 2 { 3 listBh = new List<BH_DICTInfo>(); 4 foreach (DataRow dr in dt.Rows) 5 { 6 BH_DICTInfo model = new BH_DICTInfo(); 7 model.DictId = dr["DICT_ID"].ToString(); 8 model.DictName = dr["DICT_NAME"].ToString(); 9 model.DictType = dr["DICT_TYPE"].ToString(); 10 model.Enable = Convert.ToDecimal(dr["ENABLE"].ToString()); 11 model.Orders = Convert.ToDecimal(dr["ORDERS"].ToString()); 12 model.Remark = dr["REMARK"].ToString(); 13 listBh.Add(model); 14 } 15 dgvBh.DataSource = listBh; 16 //清除上次数据源 17 this.dgvBh.Columns.Clear(); 18 //属性时是否自动创建列 19 dgvBh.AutoGenerateColumns = false; 20 21 DisplayCol(dgvBh, "DictId", "字典编号"); 22 DisplayCol(dgvBh, "DictName", "字典名称"); 23 DisplayCol(dgvBh, "DictType", "字典类型"); 24 DisplayCol(dgvBh, "Orders", "字典名称"); 25 DisplayCol(dgvBh, "Enable", "是否可用"); 26 DisplayCol(dgvBh, "Remark", "备注"); 27 DisplayCol(dgvBh, "Checkbox", "删除"); 28 //this.dgvBh.DataMember = "DictName,DictType,Orders,Enable,Remark"; 29 this.dgvBh.Columns[0].Visible = false; 30 } 31 /// <summary> 32 /// 添加列表头 33 /// </summary> 34 /// <param name="dgv"></param> 35 /// <param name="dataPropertyName"></param> 36 /// <param name="headerText"></param> 37 void DisplayCol(DataGridView dgv, string dataPropertyName, string headerText) 38 { 39 //dgv.Columns[dataPropertyName].Width = 100; 40 if (dataPropertyName == "Checkbox") 41 { 42 DataGridViewCheckBoxColumn ck = new DataGridViewCheckBoxColumn(); 43 ck.DataPropertyName = dataPropertyName; 44 ck.HeaderText = headerText; 45 ck.Name = dataPropertyName; 46 dgv.Columns.AddRange(new DataGridViewColumn[] { ck }); 47 } 48 else 49 { 50 DataGridViewTextBoxColumn obj = new DataGridViewTextBoxColumn(); 51 obj.DataPropertyName = dataPropertyName; 52 obj.HeaderText = headerText; 53 obj.Name = dataPropertyName; 54 obj.Resizable = DataGridViewTriState.True; 55 obj.ReadOnly = true; 56 dgv.Columns.AddRange(new DataGridViewColumn[] { obj }); 57 } 58 }
3.获取选中的行
#region 修改事件 /// <summary> /// 修改事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnBh_update_Click(object sender, EventArgs e) { model = new BH_DICTInfo(); foreach (DataGridViewRow row in this.dgvBh.SelectedRows) { model.DictId = row.Cells["DictId"].Value.ToString(); model.DictName = row.Cells["DictName"].Value.ToString(); model.DictType = row.Cells["DictType"].Value.ToString(); model.Enable = Convert.ToDecimal(row.Cells["Enable"].Value.ToString()); model.Orders = Convert.ToDecimal(row.Cells["Orders"].Value.ToString()); model.Remark = row.Cells["Remark"].Value.ToString(); } if (model == null) MessageBox.Show("请选择"); BhDictAddEdit SysEdit = new BhDictAddEdit("业务字典编辑信息", "保存编辑", model); SysEdit.ShowDialog(); BindDataBh(); } #endregion
4.删除多行操作
1 #region 删除事件 2 /// <summary> 3 ///删除事件 4 /// </summary> 5 /// <param name="sender"></param> 6 /// <param name="e"></param> 7 private void btnBh_delete_Click(object sender, EventArgs e) 8 { 9 //动态添加链接对象语句 10 StringBuilder sqlStr = new StringBuilder(); 11 for (int i = 0; i < dgvBh.Rows.Count; i++) 12 { 13 object cbh = this.dgvBh.Rows[i].Cells["Checkbox"].Value; 14 if (cbh != null) 15 { 16 sqlStr.Append("DICT_ID="); 17 sqlStr.Append("\'"); 18 sqlStr.Append(this.dgvBh.Rows[i].Cells["DictId"].Value.ToString()); 19 sqlStr.Append("\'"); 20 sqlStr.Append(" or "); 21 } 22 } 23 24 if (sqlStr.ToString() != "") 25 { 26 string sq = sqlStr.ToString().Substring(0, sqlStr.ToString().TrimEnd().LastIndexOf("or")); 27 28 if (DevExpress.XtraEditors.XtraMessageBox.Show("您确定要删除吗?", "系统提示") == System.Windows.Forms.DialogResult.OK) 29 // if (MessageBox.Show("您确定要删除吗?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes) 30 { 31 string sql = "delete from bh_dict where " + sq + ""; 32 ParameterSetConfig.GetDataDal(); 33 ParameterSetConfig.OpenDB(); 34 DataTable dt = ParameterSetConfig._dataDal.ExecuteSqlReturnTable(sql); 35 ParameterSetConfig.closeDB(); 36 BindDataBh(); 37 } 38 } 39 else 40 { 41 MessageBox.Show("请选择要删除的行"); 42 } 43 }