我不知道为什么会这样?
var tradeList = client.getTradeReports(criteria);
tradeGridView.DataSource = tradeList;
int rowCount = tradeGridView.Rows.Count;
foreach (DataGridViewRow row in tradeGridView.Rows)
{
for (int counter = 0; counter < columnNameAndFlag.Length; counter++)
{
if (row.Cells[columnNameAndFlag[counter].Split('-')[0]].Value == null || row.Cells[columnNameAndFlag[counter].Split('-')[0]].Value == DBNull.Value || String.IsNullOrWhiteSpace(row.Cells[columnNameAndFlag[counter].Split('-')[0]].Value.ToString()))
tradeData.Add("");
else
{
if (columnNameAndFlag[counter].Split('-')[0] == "TransactTime")
tradeData.Add(dt.AddSeconds(Convert.ToDouble(row.Cells[columnNameAndFlag[counter].Split('-')[0]].Value)).ToLocalTime().ToString());
else
tradeData.Add(row.Cells[columnNameAndFlag[counter].Split('-')[0]].Value.ToString());
}
}
}
如果我不使用foreach循环,一切都很好,数据网格显示数据。但是当我使用foreach循环时,每个单元格值都返回空/ null。
MessageBox.Show(row.Cells[anyColumn].Value.ToString()); // messagebox shows nothing
这就是为什么只有符合条件的这条线:
if (row.Cells[columnNameAndFlag[counter].Split('-')[0]].Value == null || row.Cells[columnNameAndFlag[counter].Split('-')[0]].Value == DBNull.Value || String.IsNullOrWhiteSpace(row.Cells[columnNameAndFlag[counter].Split('-')[0]].Value.ToString()))
编辑:
这是我创建列并手动设置每个新单元格值的方法:
List<string> tradeData = new List<string>();
string columnSettings = ConfigurationManager.AppSettings["columnSettings"];
string[] columnNameAndFlag = columnSettings.Split(',');
var tradeList = client.getTradeReports(criteria);
tradeGridView.DataSource = tradeList;
int rowCount = tradeGridView.Rows.Count;
foreach (DataGridViewRow row in tradeGridView.Rows)
{
for (int counter = 0; counter < columnNameAndFlag.Length; counter++)
{
if (row.Cells[columnNameAndFlag[counter].Split('-')[0]].Value == null || row.Cells[columnNameAndFlag[counter].Split('-')[0]].Value == DBNull.Value || String.IsNullOrWhiteSpace(row.Cells[columnNameAndFlag[counter].Split('-')[0]].Value.ToString()))
tradeData.Add("");
else
{
if (columnNameAndFlag[counter].Split('-')[0] == "TransactTime")
tradeData.Add(dt.AddSeconds(Convert.ToDouble(row.Cells[columnNameAndFlag[counter].Split('-')[0]].Value)).ToLocalTime().ToString());
else
tradeData.Add(row.Cells[columnNameAndFlag[counter].Split('-')[0]].Value.ToString());
}
}
}
tradeGridView.DataSource = null;
tradeGridView.Columns.Clear();
for (int counter = 0; counter < columnNameAndFlag.Length; counter++)
{
tradeGridView.Columns.Add(columnNameAndFlag[counter].Split('-')[0], columnNameAndFlag[counter].Split('-')[0]);
if (columnNameAndFlag[counter].Split('-').Last() == "1")
tradeGridView.Columns[columnNameAndFlag[counter].Split('-')[0]].Visible = true;
else
tradeGridView.Columns[columnNameAndFlag[counter].Split('-')[0]].Visible = false;
}
for (int index = 0; index < rowCount; index++)
tradeGridView.Rows.Add();
int i = 0;
foreach (DataGridViewRow row in tradeGridView.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
cell.Value = tradeData[i];
i++;
}
}
答案 0 :(得分:0)
你应该写tradeGridView.DataBind(); tradeGridView.DataSource = tradeList之后的命令;并检查它是否正常工作。