How to implement DataGridView Add and remove changes?

Source: Internet
Author: User
1. Create a WinForm form application

2. Drag into the DataGridView control on the interface

3, add the appropriate columns

4, start writing the following code:

Private DataTable Countrydt = new DataTable ();
Private DataTable Citydt = new DataTable ();

Public Main ()
{
InitializeComponent ();

Initcountrydt ();
Initcitydt ();
Initgrid ();
}

private void Initcitydt ()
{
String[] Citys = {"cn|1| Beijing", "cn|2| Tianjin", "cn|3| Shanxi", "Jp|4| Osaka", "jp|5| Yokohama", "Jp|6| Nagoya", "jp|7| Kobe", "us|8| New York
, "us|9| Los Angeles", "us|10| Chicago", "us|11| Houston", "us|12| Philadelphia", "us|13| San Francisco"};
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| China", "jp| Japan", "us| USA"};
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 = ten; i <; 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);
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.