概述
DataGridView事件CellContentClick
触发dataGridView的CellContentClick事件,并且在事件中调用Edit 方法。
实现DataGridView中添加CheckBox后不满足条件去掉勾选
并且实现勾选后将绑定数据的存入表的动作
点击确定后去掉勾选
Main.cs
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) { switch (this.tabControl1.SelectedIndex) { //case 0: // //显示数据 // ShowVlues(); // break; case 1: TableDefine.Initial_TableE(); for (int i = 0; i <= TableDefine.tGridField.Length - 1; i++) { cboSearch.Items.Add(TableDefine.tGridField[i].sCaption); cboSearchList.Items.Add(TableDefine.tGridField[i].sFieldName); } if (cboSearch.Items.Count > 0) { cboSearch.SelectedIndex = 0; } //取机种 DataTable ModelDT = fslConn.GetAllModel(); if (ModelDT.Rows.Count > 0) { foreach (DataRow item in ModelDT.Rows) { cboModel.Items.Add(item[0]); } } //取流程 DataTable RouteDT = fslConn.GetAllRoute(); if (RouteDT.Rows.Count > 0) { foreach (DataRow item in RouteDT.Rows) { cboRoute.Items.Add(item[0]); } } //显示数据 ShowEVlues(); break; } } //取数据显示 void ShowEVlues() { string sFieldName = string.Empty; string sFieldText = string.Empty; if (cboSearch.SelectedIndex > -1 && txtSearch.Text.Trim() != "") { sFieldName = cboSearchList.Items[cboSearch.SelectedIndex].ToString(); sFieldText = txtSearch.Text.Trim(); } if (string.IsNullOrWhiteSpace(sFieldText)) gvDataE.DataSource = fslConn.GetAllFslEName(); else gvDataE.DataSource = fslConn.GetAllFslEName(sFieldName, sFieldText); //栏位title for (int i = 0; i <= gvDataE.Columns.Count - 1; i++) { gvDataE.Columns[i].Visible = false; } for (int i = 0; i < TableDefine.tGridField.Length; i++) { string sGridField = TableDefine.tGridField[i].sFieldName; if (gvDataE.Columns.Contains(sGridField)) { gvDataE.Columns[sGridField].HeaderText = TableDefine.tGridField[i].sCaption; gvDataE.Columns[sGridField].DisplayIndex = i; //栏位显示顺序 gvDataE.Columns[sGridField].Visible = true; } } gvDataE.Focus(); //为dgv增加复选框列 DataGridViewCheckBoxColumn checkbox = new DataGridViewCheckBoxColumn(); //列显示名称 checkbox.HeaderText = "选择"; checkbox.Name = "IsChecked"; checkbox.TrueValue = true; checkbox.FalseValue = false; checkbox.DataPropertyName = "IsChecked"; //列宽 checkbox.Width = 50; //列大小不改变 checkbox.Resizable = DataGridViewTriState.False; //添加的checkbox在dgv第一列 this.gvDataE.Columns.Insert(0, checkbox); } //实现搜索 private void txtSearch_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != (char)Keys.Return) return; ShowEVlues(); editFilter.Focus(); } //实现通过流程取站点 private void cboRoute_SelectedIndexChanged(object sender, EventArgs e) { if (cboRoute.Text != "") { //取站点 DataTable StationTypeDT = fslConn.GetAllStationType(cboRoute.Text); if (StationTypeDT.Rows.Count > 0) { for (int i = 0; i < StationTypeDT.Rows.Count; i++) { //cboStationType.Items.Add(item[0]); cboStationType.Items.Add(new Metadata(StationTypeDT.Rows[i][2].ToString(), StationTypeDT.Rows[i][0].ToString(), StationTypeDT.Rows[i][1].ToString())); } } } } //点击勾选事件后进行数据绑定 private void gvDataE_CellContentClick(object sender, DataGridViewCellEventArgs e) { string Model = cboModel.Text; string Route = cboRoute.Text; if (Model == "" || Route == ""|| cboStationType.Text == "") { MessageBox.Show("机种、流程、站点不能为空,请检查!"); //DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)this.gvDataE.Rows[e.RowIndex].Cells["IsChecked"]; // this.gvDataE.Rows[e.RowIndex].Cells["IsChecked"].Value = false; //将勾选去掉 this.gvDataE.RefreshEdit(); return; } //站点代码 string StationType = ((Metadata)cboStationType.SelectedItem).ID; //站点描述 string StationTypeDesc = ((Metadata)cboStationType.SelectedItem).NAMEDESC; if (e.ColumnIndex == 0 && e.RowIndex != -1) { //获取控件的值 DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)this.gvDataE.Rows[e.RowIndex].Cells["IsChecked"]; Boolean flag = Convert.ToBoolean(checkCell.Value); //KP品名 string gvFunName = gvDataE.Rows[e.RowIndex].Cells["MAT_TYPE_CODE"].Value.ToString(); //KP品名描述 string gvFunNameDesc = gvDataE.Rows[e.RowIndex].Cells["MAT_TYPE_DESC"].Value.ToString(); if (flag == false) { checkCell.Value = true; //添加权限 fslConn.SaveFslPower(gvFunName, gvFunNameDesc, Model,StationType, StationTypeDesc); } } }
ComboBox取值
main.cs //取机种 DataTable ModelDT = fslConn.GetAllModel(); if (ModelDT.Rows.Count > 0) { foreach (DataRow item in ModelDT.Rows) { cboModel.Items.Add(item[0]); } } fslConn.cs //取机种 public DataTable GetAllModel() { return fslDAL.GetAllModel(); } fslDAL.cs //取机种 public DataTable GetAllModel() { string sql = "SELECT MODEL FROM IMES.M_MODEL"; return utility.Query(sql); }
储存数据
fslConn.cs public long SaveFslPower(string gvFunName, string gvFunNameDesc, string Model, string StationType, string StationTypeDesc ) { return fslDAL.SaveFslPower(gvFunName, gvFunNameDesc, Model, StationType, StationTypeDesc); } fslDAL.cs //存权限 public long SaveFslPower(string gvFunName, string gvFunNameDesc, string Model, string StationType, string StationTypeDesc) { string id = GetMaxId(); string sql = @"INSERT INTO IMES.M_STATIONTYPE_PARTSPEC (ID,MODEL,STATION_TYPE,STATION_DESC,KP_SPEC,KP_SPEC_DESC,QTY,ENABLED,CREATE_EMPNO,CREATE_TIME,UPDATE_EMPNO) VALUES(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11)"; object[] para = new object[] { id, Model, StationType , StationTypeDesc,gvFunName, gvFunNameDesc, "1", "Y", utility.GlobalUserNo,DateTime.Now, utility.GlobalUserNo }; return utility.ExecuteSql(sql, para); }
右键删除数据
实现效果
Main.cs //实现DataGridView栏位右键删除数据 private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { if (gvData.Rows.Count == 0 || gvData.CurrentRow == null) { return; } string ID = gvData.CurrentRow.Cells["ID"].Value.ToString(); if (MessageBox.Show("ID:" + ID + " 确认删除吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { //M_STATIONTYPE_PARTSPEC_HT写入历史库 fslConn.SaveFslHistoryData(ID); 移除权限 fslConn.DeleteFslByIDAndFunc(ID); //显示数据 ShowVlues(); } } fslConn.cs //删除权限 public long DeleteFslByIDAndFunc(string id) { return fslDAL.DeleteFslByIDAndFunc( id); } fslDAL.cs //删除权限 public long DeleteFslByIDAndFunc(string id) { string sql = @"Delete from IMES.M_STATIONTYPE_PARTSPEC WHERE ID =:1"; object[] para = new object[] { id }; return utility.ExecuteSql(sql, para); }
到此这篇关于C# DataGridView中实现勾选存储数据和右键删除数据的文章就介绍到这了,更多相关C# DataGridView右键删除数据内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!
最后
以上就是专一大树为你收集整理的C# DataGridView中实现勾选存储数据和右键删除数据(示例代码)的全部内容,希望文章能够帮你解决C# DataGridView中实现勾选存储数据和右键删除数据(示例代码)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复