我有一些奇怪的行为,我没有收到错误,但它不像我想要的那样工作。
我正在使用datagridview和2 datagridviewcomboboxcolumns。
第一个组合框正常工作,但是当我在第二个组合框上添加列绑定时,第一个组合框退出工作,因为它似乎不可选,并且下拉列表不起作用,第二个组合框继续工作但是没有没有返回值(这是一个线索,我认为;))。我只是不太了解我错过了什么或者做得不好。
这是相关的编码:
DataTable Fill:
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";
}
添加时,停止工作。所以问题出在第一个组合框或第二个组合框中。
dGVCities.Columns [5] .DataPropertyName =“Speed”;
答案 0 :(得分:0)
dGVCities.Columns[1].ReadOnly = true;
是问题所在。列[1]应该是城市名称。当我用
替换它 dGVCities.Columns["City"].ReadOnly = true;
该列开始工作。在我注意到城市名称列允许输入后,我决定尝试这个,这是我不想要的,并且在修改之后,问题就消失了。
我想从中得到的结论是,尽可能使用命名列,这样您就不会无意中访问错误的列。
很抱歉打扰。