温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

实现DataGridView添加删除修改的方法

发布时间:2020-08-31 14:51:00 来源:亿速云 阅读:422 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关实现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 < 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添加删除修改的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI