DataGridView initialization, data loading, and datagridview Initialization
1. Create a winform Application
2. Drag the DataGridView control to the interface.
3. Add Columns
4. Write 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 | philia ", "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 | 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 = maid as able;
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 maid ();
CountryCell. DataSource = CountryDt;
CountryCell. ValueMember = "countryCode ";
CountryCell. DisplayMember = "countryName ";
Maid [I]. Cells ["countryCode"] = countryCell;
}
}
Private void maid (object sender, maid e)
{
Var dt = this. dataGridView1.DataSource as DataTable;
If (maid [e. ColumnIndex]. Name = nameof (CountryCode ))
{
Var countryCode = maid [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 maid ();
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 = maid as able;
For (int I = 0; I <vdt. Rows. Count; I ++)
{
Var cell = new maid ()
{
DisplayMember = "countryName ",
ValueMember = "countryCode ",
DataSource = CountryDt
};
Maid [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 {
// Maid [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 = maid;
Var dt = maid as able;
If (selected. Count> 0)
{
For (var I = 0; I <selected. Count; I ++)
{
Var row = selected [I];
Dt. Rows. RemoveAt (row. Index );
}
}
}
The preceding Code adds, deletes, and modifies the DataGridView. If you need the source code, contact me: QQ 738469945.