这是替C#微信交流群群友做的一个小实例,目的就是在datagridview选择对应行以后,点击button后获取对应行的ip,并执行相应的操作,其实我觉得这样的话button没必要非放置到datagridview里面的!但是为了满足群友的需求,还是这么做了。

先看一下运行效果:

1. DataGridView 添加一列checkbox

DataGridViewCheckBoxColumn newColumn = new DataGridViewCheckBoxColumn();
newColumn.HeaderText = "选择";
dataGridView1.Columns.Add(newColumn);

这样添加的列是放在最后一列,也许你希望它在其它列,例如第二列,那么可以:
dataGridView1.Columns.Insert(1, newColumn);

2. DataGridView 添加一个button

btn1.Name = "btnRun";
            btn1.Text = "Run";
            btn1.Visible = true;
            btn1.Location = new Point(550, 80);
            btn1.Size = new Size(80, 50);
            btn1.Parent = this;
            btn1.Click += new EventHandler(btn1_Click);
            //this.Controls.Add(btn1);
            dataGridView1.Controls.Add(btn1);

3. datagridview合并单元格,详见完整代码.

完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WindowsFormsApp28
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Button btn1 = new Button();
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("IP");
dt.Columns.Add("Option");
dt.Columns.Add("button");
dt.Rows.Add("192.168.1.10", null, null);
dt.Rows.Add("192.168.1.11", null, null);
dt.Rows.Add("192.168.1.12", null, null);
dt.Rows.Add("192.168.1.13", null, null);
dt.Rows.Add("192.168.1.14", null, null);
dt.Rows.Add("192.168.1.15", null, null);
dt.Rows.Add("192.168.1.16", null, null);
dt.Rows.Add("192.168.1.17", null, null);
dt.Rows.Add("192.168.1.18", null, null);
dt.Rows.Add("192.168.1.19", null, null);
dataGridView1.DataSource = dt; //var list = new List<Object>();
//list.Add(new { IP = "192.168.1.10", Option = "null", button = "null" });
//list.Add(new { IP = "192.168.1.11", Option = "null", button = "null" });
//list.Add(new { IP = "192.168.1.12", Option = "null", button = "null" });
//list.Add(new { IP = "192.168.1.13", Option = "null", button = "null" });
//list.Add(new { IP = "192.168.1.14", Option = "null", button = "null" });
//list.Add(new { IP = "192.168.1.15", Option = "null", button = "null" });
//dataGridView1.DataSource = list; DataGridViewCheckBoxColumn newColumn1 = new DataGridViewCheckBoxColumn();
newColumn1.HeaderText = "选择";
//dataGridView1.Columns.Add(newColumn);
dataGridView1.Columns.Insert(3, newColumn1);
DataGridViewButtonColumn newColumn2 = new DataGridViewButtonColumn();
newColumn2.HeaderText = "控件";
//dataGridView1.Columns.Add(newColumn);
dataGridView1.Columns.Insert(4, newColumn2); dt.Columns.Add("action");
dataGridView1.Rows[0].Cells[0].Value = true;
//dataGridView1.Rows[0].Cells[1].Value = true; btn1.Name = "btnRun";
btn1.Text = "Run";
btn1.Visible = true;
btn1.Location = new Point(550, 80);
btn1.Size = new Size(80, 50);
btn1.Parent = this;
btn1.Click += new EventHandler(btn1_Click);
//this.Controls.Add(btn1);
dataGridView1.Controls.Add(btn1); }
private void btn1_Click(object sender, EventArgs e)
{ // MessageBox.Show("123");
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string otherValue = dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString();
if (otherValue == "True")
MessageBox.Show(dataGridView1.Rows[i].Cells[2].Value.ToString()); }
} private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dataGridView1.Columns[1].Index)
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString()); }
/// <summary>
/// 将当前单元格中的更改提交到数据缓存,但不结束编辑模式,及时获得其状态是选中还是未选中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
} } private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{ try
{
if (dataGridView1.Rows.Count > 0)
{
int rowIndex = dataGridView1.CurrentCell.RowIndex;
int colIndex = dataGridView1.CurrentCell.ColumnIndex;
if (colIndex == 0) //第一列
{
string _selectValue = dataGridView1.CurrentCell.EditedFormattedValue.ToString();
if (_selectValue == "True")
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (i != rowIndex)
{
string otherValue = dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString();
if (otherValue == "True")
{
((DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0]).Value = false;
} }
}
}
} } }
catch (Exception ex)
{ }
} private void button1_Click(object sender, EventArgs e)
{ } private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
// 对第5列相同单元格进行合并
if (e.ColumnIndex == 5 && e.RowIndex != -1)
{
using
(
Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor)
)
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// 清除单元格
e.Graphics.FillRectangle(backColorBrush, e.CellBounds); // 画 Grid 边线(仅画单元格的底边线和右边线)
// 如果下一行和当前行的数据不同,则在当前的单元格画一条底边线
if (e.RowIndex < dataGridView1.Rows.Count - 1 &&
dataGridView1.Rows[e.RowIndex ].Cells[e.ColumnIndex].Value.ToString() !=
e.Value.ToString())
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left + 2,
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
//画最后一条记录的底线
if (e.RowIndex == dataGridView1.Rows.Count - 1)
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left + 2,
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
// 画右边线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom); // 画左边线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left ,
e.CellBounds.Top, e.CellBounds.Left ,
e.CellBounds.Bottom); // 画(填写)单元格内容,相同的内容的单元格只填写第一个
if (e.Value != null)
{
if (e.RowIndex > 0 &&
dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() ==
e.Value.ToString())
{ }
else
{
//e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
//Brushes.Black, e.CellBounds.X + 2,
//e.CellBounds.Y + 5, StringFormat.GenericDefault);
}
}
e.Handled = true;
}
} }
} }
}

  

----------------------------------------------------

以上就是本节的全部内容,如果感觉有用,请多多的点击在看和分享,需要进技术交流群的,请加小编微信zls20210502,切记备注 进群

C#实例:datagridview单元格合并的更多相关文章

  1. DataGridView单元格合并

    本文章转载:http://www.cnblogs.com/xiaofengfeng/p/3382094.html 图: 代码就是如此简单 文件下载:DataGridView单元格合并源码 也可以参考: ...

  2. 【Winform-自定义控件】DataGridView 单元格合并和二维表头

    DataGridView单元格合并和二维表头应用: //DataGridView绑定数据 DataTable dt = new DataTable(); dt.Columns.Add("); ...

  3. 如何通过DataGridView 实现单元格合并和二维表头

    先看下实现出来的效果(这里随便写了几组数据,用来测试) 先初始一个DataGridView 设置哪几列 DataGridView 里男女这两列的 AutoSizeMode 可以设置Fill. publ ...

  4. SNF快速开发平台MVC-表格单元格合并组件

    1.   表格单元格合并组件 1.1.      效果展示 1.1.1.    页面展现表格合并单元格 图 4.1 1.1.2.    导出excel合并单元格 图 4.2 1.2.      调用说 ...

  5. PHPWord中文乱码、单元格合并、动态表格模板解决方案合集

    摘要:  最近一个项目开发要用到PHP技术导出Word文档,采用PHPWord插件,版本为0.6.2 beta,CodePlex已停止维护.网上还有另外一个版本的PhpWord,项目类名大小写上略有不 ...

  6. ExtJS 4.2 Grid组件的单元格合并

    ExtJS 4.2 Grid组件本身并没有提供单元格合并功能,需要自己实现这个功能. 目录 1. 原理 2. 多列合并 3. 代码与在线演示 1. 原理 1.1 HTML代码分析 首先创建一个Grid ...

  7. NPOI 教程 - 2.1单元格合并

    来源:http://liyingchun343333.blog.163.com/blog/static/3579731620091018212990/ 合并单元格在制作表格时很有用,比如说表格的标题就 ...

  8. asp.net使用控件datagrid实现表头单元格合并

    合并的要点: 1.datagid的单元格合并原理是table中tr,td的布局实现; 2.合并的时机实在其datagridcreate事件中实现; 3.认识一个对象TableCellCollectio ...

  9. winform中dataGridView单元格根据值设置新值,彻底解决绑定后数据类型转换的困难

    // winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现.有时候需要将数字变换为不同的文字描述,就会出现int32到string类型转换的异常,借助CellFo ...

随机推荐

  1. 通过PEB的Ldr枚举进程内所有已加载的模块

    一.几个重要的数据结构,可以通过windbg的dt命令查看其详细信息 _PEB._PEB_LDR_DATA._LDR_DATA_TABLE_ENTRY 二.技术原理 1.通过fs:[30h]获取当前进 ...

  2. mzy git学习,禁用Fast forward的普通合并(六)

    git merge --no-ff -m "msg" x-branch:禁用Fast forward的普通合并 通常,合并分支时,如果可能,Git会用Fast forward模式, ...

  3. 数据库中sql分类

    -- sql语句分类:--   1)数据定义语句(DDL):--            create/alter/drop--   2)数据操作语句(DML):--            insert ...

  4. hdfs中数据迁移

    1.hdfs集群间数据迁移 hadoop distcp hdfs://192.128.112.66:8020/user/hive/warehouse/data.db/dwi_xxxx_d  /user ...

  5. Python 脚本的执行

    源文件如下,文件名test.py,其中UTF-8根据实际情况而定,Python3默认为UTF-8,所以不用设置: #!/usr/bin/python # -*- coding: UTF-8 -*- p ...

  6. Python中的文件处理和数据存储json

    前言:每当需要分析或修改存储在文件中的信息时,读取文件都很有用,对数据分析应用程序来说尤其如此. 例如,你可以编写一个这样的程序:读取一个文本文件的内容,重新设置这些数据的格式并将其写入文件,让浏览器 ...

  7. 微服务架构及raft协议

    微服务架构全景图 服务注册和发现 Client side implement 调用需要维护所有调用服务的地址 有一定的技术难度,需要rpc框架支持 Server side implement 架构简单 ...

  8. VS Code闪现,巨头纷纷入局的Web IDE缘何崛起?

    我发了,我装的. 就在前几天,微软简短的发布了Visual Studio Code for the Web 的公告,而没过一阵,这则公告就被删除了,现在点经相关内容已经是404状态了.虽然公告的内容已 ...

  9. 解决win10 cmd运行python弹出windows应用商店下python应用程序

    方法一: 1.我一开始下载完python后,忘记下载到哪个位置,在win10底下输入框搜索python,点击打开文件所在位置,所在位置是python快捷键的位置,直接复制进行环境配置 配置完环境变量后 ...

  10. 服务器安装CentOS7.9系统(U盘启动方式)

    一.安装环境 机房的华为GPU服务器,型号G2500,8张P4显卡,需要安装最小化的CentOS7.9操作系统,利用U盘启动的方式进行安装. 二.安装说明 虽然本环境是GPU服务器,但是安装方式同样适 ...