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 < citys.Length; 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 < countrys.Length; 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 < 20; 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 < dt.Rows.Count; 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 < vdt.Rows.Count; 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 < selected.Count; i++)
{
var row = selected[i];
dt.Rows.RemoveAt(row.Index);
}
}
}

以上代码实现了DataGridView的添加删除修改,如需源代码请联系我:QQ 738469945

DataGridView初始化,加载数据的更多相关文章

  1. Layui table 组件的使用:初始化加载数据、数据刷新表格、传参数

    背景 笔者之前一直使用 bootstrap table ,因为当前项目中主要使用 Layui 框架,于是也就随了 Layui table ,只是在使用的时候出现了一些问题,当然也是怪自己不熟悉的锅吧! ...

  2. Android Launcher分析和修改4——初始化加载数据

    上面一篇文章说了Launcher是如何被启动的,Launcher启动的过程主要是加载界面数据然后显示出来, 界面数据都是系统APP有关的数据,都是从Launcher的数据库读取,下面我们详细分析Lau ...

  3. DataGridView重新加载数据后,选中上次选中的行。

    public int SelecedRow;//记录当前鼠标点中的行索引(用于解决用户获取看板数据,刷新数据后,当前选中行回到默认行) private void dataGridView1_CellC ...

  4. Tomcat启动时加载数据到缓存---web.xml中listener加载顺序(优先初始化Spring IOC容器)

    JavaWebSpringTomcatCache  最近用到在Tomcat服务器启动时自动加载数据到缓存,这就需要创建一个自定义的缓存监听器并实现ServletContextListener接口,并且 ...

  5. Tomcat启动时加载数据到缓存---web.xml中listener加载顺序(例如顺序:1、初始化spring容器,2、初始化线程池,3、加载业务代码,将数据库中数据加载到内存中)

    最近公司要做功能迁移,原来的后台使用的Netty,现在要迁移到在uap上,也就是说所有后台的代码不能通过netty写的加载顺序加载了. 问题就来了,怎样让迁移到tomcat的代码按照原来的加载顺序进行 ...

  6. Bootstrap-Select 动态加载数据的小记

    关于前端框架系列的可以参考我我刚学Bootstrap时候写的LoT.UI http://www.cnblogs.com/dunitian/p/4822808.html#lotui bootstrap- ...

  7. echarts在.Net中使用实例(二) 使用ajax动态加载数据

    通过上一篇文章可以知道和echarts参考手册可知,series字段就是用来存储我们显示的数据,所以我们只需要用ajax来获取series的值就可以. option 名称 描述 {color}back ...

  8. DevExpress的GridControl的实时加载数据解决方案(取代分页)

    http://blog.csdn.net/educast/article/details/4769457 evExpress是一套第三方控件 其中有类似DataGridView的控件 今天把针对Dev ...

  9. iscroll5 上拉,下拉 加载数据

    我这里的思路是上拉时候只是加载第一页的内容,可根据实际情况修改其中的代码.请勿照搬.样式没怎么调,可以加载gif动画.1.没有数据时候,下拉可以加载数据.2.没有数据时候,点击也可以加载数据.3.其余 ...

  10. js和jquery页面初始化加载函数的方法及先后顺序

    运行下面代码.弹出A.B.C.D.E的顺序:A=B=C>D=E. jquery:等待页面加载完数据,以及页面部分元素(不包括图片.视频), js:是页面全部加载完成才执行初始化加载. <! ...

随机推荐

  1. pos机代理行业

        传统模式(代理):代理商线性分润,代理商或营运商给一代大量的机器及要求,而且这个要求公对公结算(必须要有公司),分润也是直接给一代.然后一代找二代推广,层层发展.这样你会很担心你的上家的上家突 ...

  2. [ActionSprit 3.0] FMS服务器带宽检测

    package { import flash.display.Sprite; import flash.net.NetConnection; import flash.events.NetStatus ...

  3. 10分钟教你用Python玩转微信之好友性别比例统计分析

    01 前言+效果展示 想必,微信对于大家来说,是再熟悉不过的了.那么,大家想不想探索一下微信上的各种奥秘呢?今天,我们一起来简单分析一下微信上的好友性别比例吧~废话不多说,开始干活. 结果如下: 02 ...

  4. EPC sequence

    nps-epc-term-2.7.0 eNodeB: /root/b2b/eutran/01/bin ./clean_log.sh ./set_ip.sh [root@CEN6- bin]# ./cl ...

  5. 使用webbench工具测试网站访问压力

    介绍 Webbench是一个在Linux下使用的网站压测工具.它使用fork()模拟多个客户端 同时访问我们设定的URL,测试网站在压力下工作的性能, 最多可以模拟3万个并发连接去测试网站的负载能力. ...

  6. MM 后台配置(转)

    本文转自:https://www.cnblogs.com/yanglikun/p/4124797.html 一.全局配置 1.一般配置 SPRO->SAP NETWEAVER -> GEN ...

  7. int和double究竟占多少个字节?c++等

    最近在看深入理解计算机系统这本书,上面提到了在32位机器和64机器中int类型都占用4个字节.后来,查了The C Programming language这本书,里面有一句话是这样的:Each co ...

  8. 解决js array的key不为数字时获取长度的问题

    最近写js时碰到了当数组key不为数字时,获取数组的长度为0 的情况. 1.问题场景 var arr = new Array(); arr[‘s1‘] = 1001; console.log(arr. ...

  9. Oracle使用总结

    1. 在ORACLE中Service Name即为数据库名称: 2. 在做删除操作时,需要加Commit进行操作提交: 3. 使用sqlldr将数据进行批量导入到ORACLE中: 3.1  Sqlld ...

  10. J15W-10T-16T黄铜丝口截止阀厂家,J15W-10T-16T黄铜丝口截止阀价格 - 专题栏目 - 无极资讯网

    无极资讯网 首页 最新资讯 最新图集 最新标签   搜索 J15W-10T-16T黄铜丝口截止阀 无极资讯网精心为您挑选了(J15W-10T-16T黄铜丝口截止阀)信息,其中包含了(J15W-10T- ...