WinForms DataGridView, and Using DataGridViewColumns
本文关键字:Using DataGridViewColumns and DataGridView WinForms | 更新日期: 2023-09-27 18:28:39
我有一些奇怪的行为,我没有得到错误,但它并没有像我想要的那样工作。
我使用的是一个datagridview和两个datagridviewcomboxcolumns。
第一个组合框工作正常,但当我在第二个组合框上添加列绑定时,第一个组合盒停止工作,因为它似乎不可选择,下拉框不起作用,第二个继续工作,但没有返回值(我认为这是一条线索;))。我只是不太明白我错过了什么或做得不对。
图像:程序视图
这是相关的编码:
数据表填充:
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";
}
当将其添加到中时,它将停止工作。因此,问题要么出现在第一个组合框xcolumn中,要么出现在第二个组合框中。
dGVCities.Columns[5]。DataPropertyName="速度";
dGVCities.Columns[1].ReadOnly = true;
是个问题。列[1]应该是城市名称。当我把它换成时
dGVCities.Columns["City"].ReadOnly = true;
柱子开始工作了。在我注意到城市名称列允许输入后,我决定尝试一下,这是我不想要的,修改后,问题消失了。
我想这样做的好处是,尽可能使用命名列,这样就不会无意中访问错误的列。
抱歉打扰了。