Wie implementiert man das Hinzufügen, Löschen und Ändern von DataGridView?

零下一度
Freigeben: 2017-06-24 09:31:03
Original
3685 Leute haben es durchsucht

1, erstellen Sie eine Winform-Formularanwendung

2, ziehen Sie das DataGridView-Steuerelement in die Schnittstelle

3, fügen Sie die entsprechenden Spalten wie gezeigt hinzu:

4, beginnen Sie mit dem Schreiben des folgenden Codes:

private DataTable CountryDt = new DataTable();
private DataTable CityDt = new DataTable();

public Main( )
                                                        InitializeComponent() jing“, „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 < columns.Length;
🎜> 🎜> private void initcountrydt ()

{

String [] Länder = {"cn | China", "JP | Japan", "US | USA"};
CountryDt. Columns.Add("countryCode");
CountryDt.Columns.Add("countryName");
for (int i = 0; i < states.Length; i++)
{
var newRow = CountryDt.NewR ow();
newRow["countryCode"] = states[i].Split('|')[0];
newRow["countryName"] = states[i].Split ('|')[1];
CountryDt.Rows.Add(newRow);
}

}
        dt.Columns > .Rows.add (newrow);
}
datagridView1.dataSource = dt; 🎜>
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 = 🎜>                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.Colum nIndex].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"];
                                             newCityDt.Rows.Add(newRow);
 }
var cityCell = new DataGridViewComboBoxCell();

cityCell.DataSource = newCityDt;
             cityCell.DisplayMember = "cityName";

               cityCell.ValueMember = "cityCode";

              dataGridView1.Rows[e .RowIndex].Cells["CityCode"] = cityCell;
           GridView1.DataSource als DataTable;
           for (int i = 0; ich < 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);
               }
           }
       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);
}
}
}

Das obige ist der detaillierte Inhalt vonWie implementiert man das Hinzufügen, Löschen und Ändern von DataGridView?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!