C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C# DataGridView右键删除数据

C# DataGridView中实现勾选存储数据和右键删除数据(示例代码)

作者:caoguanghui0804

这篇文章主要介绍了C# DataGridView中实现勾选存储数据和右键删除数据的示例代码,通过示例代码给大家展示运行效果图,需要的朋友可以参考下

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右键删除数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文