当前位置: 首页 > news >正文

威海网站建设兼职合肥公司网站建设

威海网站建设兼职,合肥公司网站建设,力软框架做网站,优化关键词排名哪家好背景:最近在项目中有需求需要在DataGridView中添加“删除”、“修改”按钮,用来对数据的操作以及显示。 在DataGridView中显示需要的按钮 首先在DataGridView中添加需要的列,此列是用来存放按钮的。 然后在代码中“画”按钮。 if (e.Column…

背景:最近在项目中有需求需要在DataGridView中添加“删除”、“修改”按钮,用来对数据的操作以及显示。

在DataGridView中显示需要的按钮

 首先在DataGridView中添加需要的列,此列是用来存放按钮的。

然后在代码中“画”按钮。

if (e.ColumnIndex >= 0 && e.RowIndex >= 0){if (this.dgvwProdCode.Columns[e.ColumnIndex].Name == "act"){StringFormat sf = StringFormat.GenericDefault.Clone() as StringFormat;//设置重绘入单元格的字体样式sf.FormatFlags = StringFormatFlags.DisplayFormatControl;sf.Alignment = StringAlignment.Center;sf.LineAlignment = StringAlignment.Center;sf.Trimming = StringTrimming.EllipsisCharacter;e.PaintBackground(e.CellBounds, true);//重绘边框//设置要写入字体的大小System.Drawing.Font myFont = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));SizeF sizeDel = e.Graphics.MeasureString("删除", myFont);SizeF sizeMod = e.Graphics.MeasureString("修改", myFont);SizeF sizeIsEnable = e.Graphics.MeasureString("启用/禁用", myFont);float fDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width + sizeIsEnable.Width); //float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width + sizeIsEnable.Width);float fIsEnable = sizeIsEnable.Width / (sizeDel.Width + sizeMod.Width+ sizeIsEnable.Width);//设置每个“按钮的边界”RectangleF rectDel = new RectangleF(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width * fDel, e.CellBounds.Height);RectangleF rectMod = new RectangleF(rectDel.Right, e.CellBounds.Top, e.CellBounds.Width * fMod, e.CellBounds.Height);RectangleF rectIsEnable = new RectangleF(rectMod.Right, e.CellBounds.Top, e.CellBounds.Width* fIsEnable, e.CellBounds.Height);e.Graphics.DrawString("删除", myFont, Brushes.Black, rectDel, sf); //绘制“按钮”e.Graphics.DrawString("修改", myFont, Brushes.Black, rectMod, sf);e.Graphics.DrawString("启用/禁用", myFont, Brushes.Black, rectIsEnable, sf);e.Handled = true;}}

以上代码是在DataGridView中可以显示需要的按钮。

给DataGridView添加事件,可以通过按钮来操作数据库

使用CellMouseClick方法来去执行事件。

 private void dgvwProdCode_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e){if (e.ColumnIndex >= 0 && e.RowIndex >= 0){Point curPosition = e.Location;//当前鼠标在当前单元格中的坐标if (this.dgvwProdCode.Columns[e.ColumnIndex].Name == "act"){Graphics g = this.dgvwProdCode.CreateGraphics();System.Drawing.Font myFont = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));SizeF sizeDel = g.MeasureString("删除", myFont);SizeF sizeMod = g.MeasureString("修改", myFont);SizeF sizeIsEnable = g.MeasureString("启用/禁用", myFont);float fDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width + sizeIsEnable.Width);float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width + sizeIsEnable.Width);float fIsEnable = sizeIsEnable.Width / (sizeDel.Width + sizeMod.Width + sizeIsEnable.Width);Rectangle rectTotal = new Rectangle(0, 0, this.dgvwProdCode.Columns[e.ColumnIndex].Width, this.dgvwProdCode.Rows[e.RowIndex].Height);RectangleF rectDel = new RectangleF(rectTotal.Left, rectTotal.Top, rectTotal.Width * fDel, rectTotal.Height);RectangleF rectMod = new RectangleF(rectDel.Right, rectTotal.Top, rectTotal.Width * fMod, rectTotal.Height);RectangleF rectIsEnable = new RectangleF(rectMod.Right, rectTotal.Top, rectTotal.Width * fIsEnable, rectTotal.Height);//判断当前鼠标在哪个“按钮”范围内if (rectDel.Contains(curPosition))//删除{IProduct product = new ProductImpl();ProductInfoEntity productInfo = new ProductInfoEntity();productInfo.recipe = dgvwProdCode.Rows[e.RowIndex].Cells[1].Value.ToString();if (product.Delete(productInfo) > 0){this.dgvwProdCode.Rows.RemoveAt(e.RowIndex);MessageBox.Show("删除成功");}dgvwProdCode.Refresh();//刷新显示}else if (rectMod.Contains(curPosition))//修改{// FormProductOperate formProductOperate = new FormProductOperate();FormProductOperate formProductOperate = FormProductOperate.GetInstance();formProductOperate.Text = "修改产品";formProductOperate.btnAdd.Visible = false;formProductOperate.btnConfirm.Visible = true;formProductOperate.recipe = dgvwProdCode.Rows[dgvwProdCode.CurrentRow.Index].Cells[1].Value.ToString();formProductOperate.Show();dgvwProdCode.Refresh();//刷新显示}else if (rectIsEnable.Contains(curPosition)){IProduct product = new ProductImpl();//获取选中产品记录产品idstring recipe = dgvwProdCode.Rows[dgvwProdCode.CurrentRow.Index].Cells[1].Value.ToString();if (product.UpdateStatus(recipe) > 0){UpDataViewSource();MessageBox.Show("状态更改成功");}}}}}

在按钮上鼠标箭头变成小手样式

使用CellMouseMove方法

private void dgvwProdCode_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e){if (e.ColumnIndex >= 0 && e.RowIndex >= 0){Point curPosition = e.Location;//当前鼠标在当前单元格中的坐标if (this.dgvwProdCode.Columns[e.ColumnIndex].Name == "act"){this.Cursor = Cursors.Hand;//解决绘图时画面闪烁SetStyle(ControlStyles.UserPaint, true);SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲}}}

这样在DataGridView中完整的按钮和操作就完成。以及样式上的修改。

http://www.yayakq.cn/news/755902/

相关文章:

  • 网站建设 淄博 兼职做黎川旅游网站的目的
  • 专门做985招聘信息的网站2022必火的创业项目
  • 北京seo排名优化网站wordpress中文伪原创
  • 淘宝二官方网站是做啥的百度对网站建设公司
  • 许昌住房和城乡建设部网站做百度移动端网站优化
  • 天津做无痛人流费用五洲网站房产网站设计方案
  • 有了页游源代码如何做网站生产企业网站建设
  • 网站维护与建设考试企查查官网在线查询
  • 松原网站建设哪家好定南建设银行网站点
  • 网站建设需要些什么资料网站定制开发要多久
  • 如何在网站上做网盘广告公司朋友圈创意宣传
  • 网站推广网络怎么做app网站ui原型
  • 河南省建设厅网站中州杯广州网站建设 易点
  • 电子商务平台怎么注册关键词优化时间
  • 龙岗高端网站设计专家工作邮箱怎么注册
  • 如何让百度快速收录新网站wordpress启用主题
  • 十年专业网站建设能免费做婚礼邀请函的网站
  • 金馆长做图网站黑科技推广软件
  • 国家建设部门三类人员官方网站wordpress钩子的意思
  • 如何维护自己公司网站免费网上商城
  • 电脑制作网站总么做wordpress模板 众筹
  • 网站建设比较好公司seo平台代理
  • 网站备案查询不到施工企业资料
  • 洛阳做网站的搜狗排名优化工具
  • 做服装哪个网站图片多网站空间代理站
  • 怎么做一个网站送给女朋友优秀设计作品的网站
  • 网站颜色搭配网站后台程序
  • 五屏网站建设品牌好电子商务企业网站建设发展论文
  • 怎样制作免费的网站华强北电子商城
  • jsp网站开发学习心得无极电影网免费完整播放电视剧