0

0

如何实现DataGridView的添加删除修改?

零下一度

零下一度

发布时间:2017-06-24 09:31:03

|

4180人浏览过

|

来源于php中文网

原创

1,创建winform窗体应用程序

2,在界面上拖入DataGridView控件

3,添加相应的列如图:

4,开始编写后面的代码:

private DataTable CountryDt = new DataTable();
       private DataTable CityDt = new DataTable();

public Main()
       {
           InitializeComponent();

InitCountryDt();
           InitCityDt();
           InitGrid();
       }

private void InitCityDt()
       {
           string[] citys = { "CN|1|北京", "CN|2|天津", "CN|3|山西", "JP|4|大阪", "JP|5|横滨", "JP|6|名古屋", "JP|7|神户", "US|8|纽约"
                   , "US|9|洛杉矶", "US|10|芝加哥", "US|11|休斯敦", "US|12|费城", "US|13|旧金山"};
           CityDt.Columns.Add("cityCode");
           CityDt.Columns.Add("cityName");
           CityDt.Columns.Add("Pid");
           for (int i = 0; i            {
               var newRow = CityDt.NewRow();
               newRow["cityCode"] = citys[i].Split('|')[1];
               newRow["cityName"] = citys[i].Split('|')[2];
               newRow["Pid"] = citys[i].Split('|')[0];
               CityDt.Rows.Add(newRow);
           }
       }
       private void InitCountryDt()
       {
           string[] countrys = { "CN|中国", "JP|日本", "US|美国" };
           CountryDt.Columns.Add("countryCode");
           CountryDt.Columns.Add("countryName");
           for (int i = 0; i            {
               var newRow = CountryDt.NewRow();
               newRow["countryCode"] = countrys[i].Split('|')[0];
               newRow["countryName"] = countrys[i].Split('|')[1];
               CountryDt.Rows.Add(newRow);
           }

}
       private void InitGrid()
       {
           var dt = new DataTable();
           dt.Columns.Add("Id");
           dt.Columns.Add("CountryCode");
           dt.Columns.Add("CityCode");
           for (int i = 10; i            {
               var newRow = dt.NewRow();
               newRow["Id"] = i.ToString();
               dt.Rows.Add(newRow);
           }
           dataGridView1.DataSource = dt;
       }

private void btnAdd_Click(object sender, EventArgs e)
       {
           var dt = dataGridView1.DataSource as DataTable;

网奇IOS智能在线订单系统
网奇IOS智能在线订单系统

产品简介: 网奇IOS智能订单系统,是网奇公司研发的一款智能在线订单编辑以及管理系统。本系统适合使用在;在线报名、酒店预定、信息反馈、在线订单和在线投诉等等诸多应用上。本系统所有选项字段完全通过后台控制,在线报名系统可以变为在线预定系统,同时可以变为任何其它的系统,里面的栏目字段,可以任意添加、删除、 修改。本系统为网奇公司全国独家首创,顺应网络需求,安装十分便利,上传即可使用。产品特色:

下载

var newRow = dt.NewRow();
           newRow["Id"] = dt.Rows.Count + 1;
           dt.Rows.Add(newRow);

for (int i = 0; i            {
               var countryCell = new DataGridViewComboBoxCell();
               countryCell.DataSource = CountryDt;
               countryCell.ValueMember = "countryCode";
               countryCell.DisplayMember = "countryName";
               dataGridView1.Rows[i].Cells["countryCode"] = countryCell;
           }
       }

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
       {
           var dt = this.dataGridView1.DataSource as DataTable;
           if (dataGridView1.Columns[e.ColumnIndex].Name == nameof(CountryCode))
           {
               var countryCode = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
               var drs = CityDt.Select("Pid='" + countryCode + "'");
               var newCityDt = new DataTable();
               newCityDt.Columns.Add("cityCode");
               newCityDt.Columns.Add("cityName");
               newCityDt.Columns.Add("Pid");
               foreach (DataRow row in drs)
               {
                   var newRow = newCityDt.NewRow();
                   newRow["cityCode"] = row["cityCode"];
                   newRow["cityName"] = row["cityName"];
                   newRow["Pid"] = row["Pid"];
                   newCityDt.Rows.Add(newRow);
               }
               var cityCell = new DataGridViewComboBoxCell();

cityCell.DataSource = newCityDt;
               cityCell.DisplayMember = "cityName";
               cityCell.ValueMember = "cityCode";
               dataGridView1.Rows[e.RowIndex].Cells["CityCode"] = cityCell;
           }
       }

private void Main_Load(object sender, EventArgs e)
       {
           var vdt = dataGridView1.DataSource as DataTable;
           for (int i = 0; i            {
               var cell = new DataGridViewComboBoxCell()
               {
                   DisplayMember = "countryName",
                   ValueMember = "countryCode",
                   DataSource = CountryDt
               };

dataGridView1.Rows[i].Cells["CountryCode"] = cell;
               if (i % 2 == 0)
               {
                   dataGridView1.Rows[i].Cells["CountryCode"].Value = "JP";
                   dataGridView1.Rows[i].Cells["CityCode"].Value = new Random().Next(4, 7);
               }
               //else {
               //    dataGridView1.Rows[i].Cells["CountryCode"].Value = "CN";
               //}
               if (i % 5 == 0)
               {
                   dataGridView1.Rows[i].Cells["CountryCode"].Value = "CN";
                   dataGridView1.Rows[i].Cells["CityCode"].Value = new Random().Next(1, 3);
               }
               if (i % 9 == 0)
               {
                   dataGridView1.Rows[i].Cells["CountryCode"].Value = "US";
                   dataGridView1.Rows[i].Cells["CityCode"].Value = new Random().Next(8, 13);
               }
           }
       }

private void btnRemove_Click(object sender, EventArgs e)
       {

var selected = dataGridView1.SelectedRows;
           var dt = dataGridView1.DataSource as DataTable;
           if (selected.Count > 0)
           {
               for (var i = 0; i                {
                   var row = selected[i];
                   dt.Rows.RemoveAt(row.Index);
               }
           }
       } 

相关专题

更多
C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

10

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

29

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

13

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

11

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

8

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

55

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
麻省理工大佬Python课程
麻省理工大佬Python课程

共34课时 | 5.2万人学习

【web前端】Node.js快速入门
【web前端】Node.js快速入门

共16课时 | 2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号