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

1,创建winform窗体应用程序

2,在界面上拖入DataGridView控件如何实现DataGridView的添加删除修改?

3,添加相应的列如图:如何实现DataGridView的添加删除修改?如何实现DataGridView的添加删除修改?

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;

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);
               }
           }
       } 

以上就是如何实现DataGridView的添加删除修改?的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2440347.html

(0)
上一篇 2025年3月3日 12:24:28
下一篇 2025年3月3日 12:24:43

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • 什么是MVVM架构和数据绑定?

      (申明:最近在做一个练习,写点东西,谨供参考。) 1、界面展示:其中的布局和样式就不说了,重点在MVVM架构和数据绑定(Model层使用EF(Entity Framework)实体框架,不做介绍)。   绑定后: 2、架构介绍: 在Vi…

    2025年3月3日 编程技术
    200
  • 实现GridView自动滚动的功能

    引言     最新有一个winform项目使用的是devexpress的控件,所以最近都在摸索使用这套控件,实在是佩服整套控件的强大,同时代码写起来也简洁。客户有一个需求,希望报表结果能在外接的大屏幕上定时滚动。这个报表我们使用的控件就是g…

    2025年3月3日
    200
  • 总结MVC中数据验证实例

    一、一般情况 对于使用过MVC框架的人来说,对MVC的数据验证不会陌生,比如,我有一个Model如下: 1 public class UserInfo2 {3 [Required(ErrorMessage = “UserName不可为空11…

    2025年3月3日
    200
  • ASP.NET MVC重写的实例教程

    这篇文章主要为大家详细介绍了asp.net mvc重写razorviewengine实现多主题切换,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 在ASP.NET MVC中来实现主题的切换一般有两种方式,一种是通过切换皮肤的css和js…

    2025年3月3日 编程技术
    200
  • Asp.net 中用GridView控件的实例教程

    在gridview控件中,第0列有放一个checkbox控件,现想实现对checkbox进行单选,怎么实现呢?下面小编通过本文给大家分享asp.net 中使用gridview控件实现checkbox单选功能,一起看看吧 在GridView控…

    2025年3月3日 编程技术
    200
  • Asp.NET页面事件加载的顺序是什么样的

    这篇文章主要给大家介绍了关于asp.net页面中事件加载的先后顺序,文中通过图文以及示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。 本文主要给大家介绍了关于Asp.NET页面事件加载先后顺序…

    2025年3月3日
    200
  • 对MVC进行数据验证详解

    这篇文章主要为大家详细介绍了mvc数据验证的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 一、一般情况 对于使用过MVC框架的人来说,对MVC的数据验证不会陌生,比如,我有一个Model如下: public class Use…

    2025年3月3日
    200
  • c语言数组怎么初始化

    方法:1、定义数组时给所有元素赋初值,例“int a[5]={1,2,3,4,5}”;2、给一部分元素赋值,例“int a[5]={1,2}”;3、定义时不指定数组长度,直接给所有元素赋初值,例“int a[]={1,2,3,4,5}”。 …

    2025年3月3日
    200
  • 使用Spry轻松将XML数据显示到HTML页

    随着对dreamweavse cs3中集成spry功能的深入学习,了解并掌握到spry框架的一些功能模块,其中就有通过dreamweavse cs3可视化操作轻松将xml数据显示到html页中。 当然,谈到如何将xml数据插入显示到html…

    2025年3月3日
    200
  • 新专利!三星手机或搭载葡萄糖传感器 无创测血液数据

    近日,三星一项新专利曝光,三星将葡萄糖传感器集成到智能手机,三星专利中描述的技术涉及位于智能手机背面的内置传感器,该传感器可以无创地测量用户血液中的葡萄糖水平。 三星新专利示意图传感器可以通过使用光基技术,如近红外光谱,来检测皮肤中的葡萄糖…

    2025年3月2日
    200

发表回复

登录后才能评论