DataGridView初始化,加载数据
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初始化,加载数据的更多相关文章
- Layui table 组件的使用:初始化加载数据、数据刷新表格、传参数
背景 笔者之前一直使用 bootstrap table ,因为当前项目中主要使用 Layui 框架,于是也就随了 Layui table ,只是在使用的时候出现了一些问题,当然也是怪自己不熟悉的锅吧! ...
- Android Launcher分析和修改4——初始化加载数据
上面一篇文章说了Launcher是如何被启动的,Launcher启动的过程主要是加载界面数据然后显示出来, 界面数据都是系统APP有关的数据,都是从Launcher的数据库读取,下面我们详细分析Lau ...
- DataGridView重新加载数据后,选中上次选中的行。
public int SelecedRow;//记录当前鼠标点中的行索引(用于解决用户获取看板数据,刷新数据后,当前选中行回到默认行) private void dataGridView1_CellC ...
- Tomcat启动时加载数据到缓存---web.xml中listener加载顺序(优先初始化Spring IOC容器)
JavaWebSpringTomcatCache 最近用到在Tomcat服务器启动时自动加载数据到缓存,这就需要创建一个自定义的缓存监听器并实现ServletContextListener接口,并且 ...
- Tomcat启动时加载数据到缓存---web.xml中listener加载顺序(例如顺序:1、初始化spring容器,2、初始化线程池,3、加载业务代码,将数据库中数据加载到内存中)
最近公司要做功能迁移,原来的后台使用的Netty,现在要迁移到在uap上,也就是说所有后台的代码不能通过netty写的加载顺序加载了. 问题就来了,怎样让迁移到tomcat的代码按照原来的加载顺序进行 ...
- Bootstrap-Select 动态加载数据的小记
关于前端框架系列的可以参考我我刚学Bootstrap时候写的LoT.UI http://www.cnblogs.com/dunitian/p/4822808.html#lotui bootstrap- ...
- echarts在.Net中使用实例(二) 使用ajax动态加载数据
通过上一篇文章可以知道和echarts参考手册可知,series字段就是用来存储我们显示的数据,所以我们只需要用ajax来获取series的值就可以. option 名称 描述 {color}back ...
- DevExpress的GridControl的实时加载数据解决方案(取代分页)
http://blog.csdn.net/educast/article/details/4769457 evExpress是一套第三方控件 其中有类似DataGridView的控件 今天把针对Dev ...
- iscroll5 上拉,下拉 加载数据
我这里的思路是上拉时候只是加载第一页的内容,可根据实际情况修改其中的代码.请勿照搬.样式没怎么调,可以加载gif动画.1.没有数据时候,下拉可以加载数据.2.没有数据时候,点击也可以加载数据.3.其余 ...
- js和jquery页面初始化加载函数的方法及先后顺序
运行下面代码.弹出A.B.C.D.E的顺序:A=B=C>D=E. jquery:等待页面加载完数据,以及页面部分元素(不包括图片.视频), js:是页面全部加载完成才执行初始化加载. <! ...
随机推荐
- 苹果软件App上架问题
0.官方网站 开发者中心 itunes connect 优酷 哔哩哔哩 腾讯视频 1.上架流程 1.1 开发者账号申请 2017年苹果企业开发者账请完号申整指南 iOS开发之苹果开发者账号注册申请流程 ...
- 对Java意义重大的7个性能指标
本文中,小编搜集了7个最有影响的衡量标注,让你可以不依赖日志文件来了解应用程序现在,让我们看看这些性能指标,并了解如何查看并收集它们.: 一,响应时间和吞吐量 根据应用程序的响应时间可以知道程序完 ...
- POJ - 1251A - Jungle Roads 利用最小生成树
The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...
- XPath语法使用的一些心得
XPath语法还是很强大的,支持一些函数和操作符,方便操作,但是因为版本的原因可能有的函数只能在XPath2(2007)里使用,而不能在XPath1(1999)中使用,比如函数 ends-with(s ...
- 2016级算法第五次上机-C.Bamboo和"Coco"
1064 Bamboo和"Coco" 分析题意 每个亡灵至少一个花瓣,相邻的亡灵中思念值高的要获得的花瓣高(思念值相等是不需要花瓣一样多的).主要考贪心思路,为了使得花瓣总量最少, ...
- EOFError: Compressed file ended before the end-of-stream marker was reached
EOFError: Compressed file ended before the end-of-stream marker was reached python在下载时,出现上述错误提示,一般这种 ...
- Android 开发者文档 -- 应用基础知识
https://developer.android.com/guide/components/fundamentals 应用基础知识 Android 应用采用 Java 编程语言编写.Android ...
- 2.6 Go 读取CSV
Go读取CSV文件,其内容被转换成字符串数组 package main import ( "encoding/csv" "fmt" "io/iouti ...
- C++标准库类模板vector
vector是C++标准库STL中的一个重要的类模板,相当于一个更加健壮的,有很多附加能力的数组 使用vector前首先要包含头文件 #include<vector> 1.vector的 ...
- 安装orcle服务器端后,不需要安装客户端,可通过plsql登录
用PL/SQL连接oracle数据库,不管是本地的还是远程的,一般都需要安装oracle客户端(500M左右)比较大,而且在各个系统上安装也有些讲究,需要修改相应配置文件,有点麻烦,像平时开发时候,我 ...