DataGridVIew控件绑定数据之后的,增、插、删操作
最开始没有绑定数据,很快就实现了增、插、删操作,可是绑定数据之后,进行这些操作就会报错。
网上对这方面的资料比较少,自己摸索着找到了解决方法,也就是直接对绑定的数据进行操作,这里以DataTable为例。
其中点击事件为DataGridView控件绑定的ContextMenuStrip控件。datatable为全局变量。
1.增加一行
1 private void toolStripMenuItem_AddRow_Click(object sender, EventArgs e)
2 {
3 datatable.Rows.Add();
4
5 //dataGridView_Barcode.Rows.Add();
6 //datatable.Rows.Add();
7 }
2.插入一行
1 private void ToolStripMenuItem_InsertRow_Click(object sender, EventArgs e)
2 {
3 int index = dataGridView_Barcode.CurrentCell.RowIndex;
4
5 DataRow dr = datatable.NewRow();
6 datatable.Rows.InsertAt(dr, index + 1);
7
8 //int index = dataGridView_Barcode.CurrentCell.RowIndex;
9 //dataGridView_Barcode.Rows.Insert(index + 1, 1);
10 }
new一个新的datarow竟然想了很久才找到,哈哈。
3.删除选中的行
1 private void ToolStripMenuItem_DeleteRow_Click(object sender, EventArgs e)
2 {
3
4 int nCounts = dataGridView_Barcode.SelectedRows.Count;
5 for (int i = nCounts - 1; i >= 0; i--)
6 {
7 datatable.Rows.RemoveAt(dataGridView_Barcode.SelectedRows[i].Index);
8 }
9
10 //int nCounts = dataGridView_Barcode.SelectedRows.Count;
11 //for (int i = nCounts - 1; i >= 0; i--)
12 //{
13 // dataGridView_Barcode.Rows.Remove(dataGridView_Barcode.SelectedRows[i]);
14 //}
15
16 }
希望能帮到大家!注释中都是不绑定数据时候的操作。
DataGridVIew控件绑定数据之后的,增、插、删操作的更多相关文章
- [置顶] DataGridView控件---绑定数据方法
DataGridView控件是在windows应用程中显示数据最好的方式,它只需要几行简短的代码就可以把数据显示给用户,同时又支持增.删.改操作.今天将自己总结的增加数据的方法总结分 ...
- DataGridView控件绑定数据之后,置顶操作
一个小小的置顶,就搞了半个小时,还是记录一下吧. 1.第一个问题就是datatable的插入只能是Insert DataRow,但是获取选中的行,都是DataGridViewRow,不能直接转换. 找 ...
- C# DataGridView控件绑定数据后清空数据
//1.this.dataGridView1.DataSource = null;//会将DataGridView的列也删掉 //2.this.dataGridView1.Columns.Clear( ...
- C# DataGridView控件清空数据完美解决方法
C# DataGridView控件绑定数据后清空数据在清除DataGridview的数据时: 1.DataSource为NULL(DataGridView.DataSource= null;)这样会将 ...
- 转:C# DataGridView控件清空数据出错解决方法
C# DataGridView控件绑定数据后清空数据在清除DataGridview的数据时: 1.DataSource为NULL(DataGridView.DataSource= null;)这样会将 ...
- 在aspx页动态加载ascx页面内容,给GridView控件绑定数据
在aspx页动态加载ascx页面内容 //加载ascx页面内容Control c1 = this.Page.LoadControl("WebUserControl1.ascx"); ...
- DataGridView控件绑定数据源
前言: 近期听说DataGridView控件能直接绑定数据源.而不用穿越这层那层的忍辱负重.获取数据.真是高兴的屁颠屁颠的.后来一想二狗肯定不会弄.特意写了一个笨蛋版的教程--也算记录生活.欢度端午了 ...
- 将C#datagridview控件的数据导出到Excel中
1.添加引用Microsoft.Office.Interop.Excel. 2.程序代码引用using Excel = Microsoft.Office.Interop.Excel; 3.控件事件代码 ...
- DataGridView控件添加数据时空白的可 错误情况
写一个小程序,将数据库中的两张表相关信息显示在DataGridView中.代码如下: //获取项目数据,添加到表中 SqlConnection con = new SqlConnection(Main ...
随机推荐
- GitHub-SSH密钥获取
SSH密钥 需要先安装git的客户端,下载: http://git-scm.com/download/ 使用下列步骤完成密钥的添加. 检查系统是否存在密钥 运行 Git Bash, 在弹出的终端中输入 ...
- vue 封装弹窗组件注意
父组件 <template> <div> <p @click="onDelete"> 打开 </p> <!-- 弹框 --&g ...
- Codeforces Round #789 (Div. 2)
题集链接 A. Tokitsukaze and All Zero Sequence 题意 Tokitsukaze 有一个长度为 n 的序列 a. 对于每个操作,她选择两个数字 ai 和 aj (i≠j ...
- 牛客SQL刷题第三趴——SQL大厂面试真题
01 某音短视频 SQL156 各个视频的平均完播率 [描述]用户-视频互动表tb_user_video_log.(uid-用户ID, video_id-视频ID, start_time-开始观看时间 ...
- ooday01类_对象_访问成员
笔记: 什么是类?什么是对象? 现实生活中是由很多很多对象组成的,基于对象抽出了类 对象:软件中真实存在的单个个体/东西 类:类别/类型,代表一类个体 类是对象的模子,对象是类的具体的实例 类中可以包 ...
- HBase学习(二) 基本命令 Java api
一.Hbase shell 1.Region信息观察 创建表指定命名空间 在创建表的时候可以选择创建到bigdata17这个namespace中,如何实现呢? 使用这种格式即可:'命名空间名称:表名' ...
- Scanner练习
练习1 键盘输入两个数字求和 public static void main(String[] args) { Scanner in = new Scanner(System.in); System. ...
- python开发环境配置(Windows)
简介 由于在搭建pyhon开发环境时会出现各种各样的问题,因此将这些问题记录下来 1.下载python 从官网下载对应系统的python版本(最新稳定版即可):官网地址为:python下载地址, 建议 ...
- GP查询表状态常用SQL
- MySQL8.0错误日志Error log
GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 理论知识 错误日志内容 错误日志包含mysqld启动和关闭的时间信息,还包含诊断消息,如服务器启动和关闭期间以及服务器运行 ...