2015-12-31 54 views
1

我有一些奇怪的行爲,我沒有得到一個錯誤,但它不像我打算。WinForms的DataGridView和使用DataGridViewColumns

我正在使用datagridview和2個datagridviewcomboboxcolumns。

第一個組合框工作正常,但是當我在第二個組合框上添加列綁定時,第一個組合框退出工作,因爲它看起來不可選,下拉不起作用,第二個繼續工作,但是沒有返回值(這是一個線索,我認爲;))。我只是不完全理解我失蹤或不正確的做法。

圖像:view of program

這裏是有關編碼:

數據表中填寫:

public static DataTable ParseCitiesToDataTable() 
{ 
DataTable dataCities = new DataTable(); 
dataCities.Clear(); 
dataCities.TableName = "CityList"; 
dataCities.Columns.Add("Type"); 
dataCities.Columns.Add("City"); 
dataCities.Columns.Add("Coord"); 
dataCities.Columns.Add("BattleCity"); 
dataCities.Columns.Add("Dist"); 
dataCities.Columns.Add("Speed"); 
dataCities.Columns.Add("TimeSec"); 
dataCities.Columns.Add("SendTime"); 
dataCities.Columns.Add("TimeTilSend"); 

foreach (City city in userCities) 
{ 
    DataRow _cityDetails = dataCities.NewRow(); 
      _cityDetails["Type"] = "None"; 
      _cityDetails["City"] = city.strCityName; 
      _cityDetails["Coord"] = city.strCityCoord; 
      _cityDetails["BattleCity"] = ""; 
      _cityDetails["Dist"] = "0"; 
      _cityDetails["Speed"] = "Ram"; 
      _cityDetails["TimeSec"] = ""; 
      _cityDetails["SendTime"] = ""; 
      _cityDetails["TimeTilSend"] = "0"; 
      dataCities.Rows.Add(_cityDetails); 
     }} 

按鈕點擊功能:

private async void btn_GetMyCities_Click(object sender, EventArgs e) 
    { 
     btnGetCities.Hide(); // hide button pressed and show progress bar 
     await Task.Delay(1000); 
     // 
     userCities.Clear(); 
     if (dtCities != null && dtCities.Rows.Count > 0) 
      dtCities.Clear(); 
     int intPages; 
     strUserID = txtUserID.Text; 
     if (strUserID.Length > 0) 
     { 
      pBarWebPull = new ProgressBar(); 
      gbxInternetLookup.Controls.Add(pBarWebPull); 
      pBarWebPull.Width = 260; 
      pBarWebPull.Top = 15; 
      pBarWebPull.Left = 5; 
      pBarWebPull.Value = 0; 
      pBarWebPull.Visible = true; 
      pBarWebPull.BringToFront(); 
      pBarWebPull.Show(); 
      string strWorld = txtUserWorld.Text.ToLower(); 
      string strWebPageURLBase = "http://tw2stats.com/world/" + strWorld + "/player/" + strUserID + "/towns"; 
      // get page count 
      intPages = ReturnNumberOfLookupPages(strWebPageURLBase); 
      string[] strCitiesArray; 
      strCitiesArray = GetWebPageCities(strWebPageURLBase, intPages); 
      dtCities = ParseCitiesToDataTable(); 
      bindingSourceCities.DataSource = dtCities; 
      dGVCities.DataSource = bindingSourceCities; 
      dGVCities.Sort(this.dGVCities.Columns["City"], ListSortDirection.Ascending); 
      dGVCities.Columns[1].ReadOnly = true; 
      gbxInternetLookup.Controls.Remove(pBarWebPull); 

     } 
     else 
     { 
      MessageBox.Show("Please Enter your Tribal Wars 2 ID"); 
     } 
     btnGetCities.Show(); 
    } 

private void InitCitiesDataGridView() 
    { 
     DataGridViewComboBoxColumn comboBoxCol = new DataGridViewComboBoxColumn(); 
     comboBoxCol.HeaderText = "Attk Type"; 
     comboBoxCol.DataSource = AttackTypeChoice.GetChoices(); 
     comboBoxCol.DisplayMember = "Name"; 
     comboBoxCol.ValueMember = "Value"; 
     comboBoxCol.DefaultCellStyle.NullValue = "None"; 
     comboBoxCol.DefaultCellStyle.DataSourceNullValue = "None"; 
     comboBoxCol.ValueType = typeof(string); 
     dGVCities.Columns.Add(comboBoxCol); 
     dGVCities.Columns[0].Name = "Type"; 
     dGVCities.Columns[0].Width = 65; 
     dGVCities.Columns[0].SortMode = DataGridViewColumnSortMode.Programmatic; 
     dGVCities.Columns.Add("City", "City"); 
     dGVCities.Columns[1].SortMode = DataGridViewColumnSortMode.Programmatic; 
     dGVCities.Columns[1].Width = 120; 
     dGVCities.Columns.Add("Coord", "Coord"); 
     dGVCities.Columns[2].Width = 60; 
     dGVCities.Columns[2].SortMode = DataGridViewColumnSortMode.Programmatic; 
     dGVCities.Columns.Add("BattleCity", "Attk City"); 
     dGVCities.Columns[3].Width = 75; 
     dGVCities.Columns[3].SortMode = DataGridViewColumnSortMode.Programmatic; 
     dGVCities.Columns.Add("Dist", "Dist"); 
     dGVCities.Columns[4].Width = 60; 
     dGVCities.Columns[4].SortMode = DataGridViewColumnSortMode.Programmatic; 
     DataGridViewComboBoxColumn comboBoxCol1 = new DataGridViewComboBoxColumn(); 
     comboBoxCol1.HeaderText = "Speed"; 
     comboBoxCol1.DataSource = TroopTypeChoice.GetChoices(); 
     comboBoxCol1.DisplayMember = "Name"; 
     comboBoxCol1.ValueMember = "Value"; 
     comboBoxCol1.DefaultCellStyle.NullValue = "Ram"; 
     comboBoxCol1.DefaultCellStyle.DataSourceNullValue = "Ram"; 
     comboBoxCol1.ValueType = typeof(string); 
     dGVCities.Columns.Add(comboBoxCol1); 
     dGVCities.Columns[5].Name = "Speed"; 
     dGVCities.Columns[5].Width = 80; 
     dGVCities.Columns[5].SortMode = DataGridViewColumnSortMode.Programmatic; 
     dGVCities.Columns.Add("TimeSec", "Time (sec)"); 
     dGVCities.Columns[6].Width = 50; 
     dGVCities.Columns[6].SortMode = DataGridViewColumnSortMode.Programmatic; 
     dGVCities.Columns.Add("Span", "Span d:h:m:s"); 
     dGVCities.Columns[7].Width = 65; 
     dGVCities.Columns[7].SortMode = DataGridViewColumnSortMode.Programmatic; 
     dGVCities.Columns.Add("SendDT", "Send Date"); 
     dGVCities.Columns[8].Width = 120; 
     dGVCities.Columns[8].SortMode = DataGridViewColumnSortMode.Programmatic; 
     dGVCities.Columns.Add("TimeTilSend", "Till Send"); 
     dGVCities.Columns[9].Width = 70; 
     dGVCities.Columns[9].SortMode = DataGridViewColumnSortMode.Programmatic; 
     dGVCities.Columns[0].DataPropertyName = "Type"; 
     dGVCities.Columns[1].DataPropertyName = "City"; 
     dGVCities.Columns[2].DataPropertyName = "Coord"; 
     dGVCities.Columns[3].DataPropertyName = "BattleCity"; 
     dGVCities.Columns[4].DataPropertyName = "Dist"; 
     dGVCities.Columns[5].DataPropertyName = "Speed"; 
     dGVCities.Columns[6].DataPropertyName = "TimeSec"; 
     dGVCities.Columns[7].DataPropertyName = "SendTime"; 
     dGVCities.Columns[8].DataPropertyName = "TimeTilSend"; 
} 

當將它添加在它停止工作。所以問題是在第一個comboboxcolumn或第二個。

dGVCities.Columns [5] .DataPropertyName =「Speed」;

回答

0

dGVCities.Columns[1].ReadOnly = true;

是問題所在。列[1]應該是城市名稱。當我與

dGVCities.Columns["City"].ReadOnly = true;

取代了它的列開始工作。在我注意到城市名稱欄允許輸入的情況下,我決定嘗試一下,這是我不想要的,修改後,問題就消失了。

我想從這裏得到的是,儘可能使用命名列,所以你不會無意中訪問錯誤的列。

對不起。