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的添加删除修改?全部内容,希望文章能够帮你解决如何实现DataGridView的添加删除修改?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)