我有一个数组列表public List<string[]> logList = new List<string[]>();
这是从用户从打开的文件中选取的一些自定义系统日志文件(文件中的制表符分隔单元格,每行是一个新行)读入的对话框。
我正在尝试将所有这些加载到Datagrid视图中进行观察,但我的主要问题是:
1)列数可根据字符串[X]的大小而变化,其中X是列
2)行数根据
中读取的文件大小而变化所以我的Datagrid视图开始很小,但可能有1100行,每行长253列。
正在发生的事情是,当我尝试用这些数据填充Datagrid视图时,整个GUI被锁定并且按钮不可访问,Graph不会直观地更新直到完成。我尝试使用backgroundWorker,但由于DataGridView正在被操纵,因此抛出了crossThread异常。以下是当前的,缓慢的实施:
public List<string[]> logList = new List<string[]>();
//...
//...
private void button_loadFile_Click(object sender, EventArgs e)
{
dataGridView1.Columns.Clear();
logList.Clear();
int index=0;
openFileDialog1.ShowDialog();
index = openFileDialog1.FileName.LastIndexOf("\\");
if(index>0)
openFileDialog1.InitialDirectory = (openFileDialog1.FileName.Substring(0,index));
main();
}
//...
//...
public void main()
{
readInData();
expandCollumns(findCollumnCount());
dataGridView1.Rows.Insert(0, logList.Count);
for (int i = 0; i < logList.Count; i++)
{
fillInRow(logList[i], i);
}
markRows();
}
private void readInData()
{
string FileName = openFileDialog1.FileName;
string[] masterFile = System.IO.File.ReadAllLines(FileName);
for (int i = 0; i < masterFile.Length; i++)
{
string[] splitMaster = masterFile[i].Split('\t');
logList.Add(splitMaster);
}
}
private int findCollumnCount()
{
int maxCollumns = 0;
for (int i = 0; i < logList.Count; i++)
{
if (maxCollumns < logList[i].Length)
{
maxCollumns = logList[i].Length;
}
}
return maxCollumns;
}
private void expandCollumns(int count)
{
for(int i=1;i<=count;i++)
{
dataGridView1.ColumnCount = dataGridView1.ColumnCount + 1;
dataGridView1.Columns[(dataGridView1.ColumnCount)-1].HeaderText= "data_"+ Convert.ToString(i);
}
}
private void fillInRow(string[] logLine, int rowValue)
{
for (int j = 0; j < logLine.Length; j++)
{
dataGridView1[j, rowValue].Value = logLine[j];
}
}
private void markRows()
{
for (int i = 0; i < (dataGridView1.RowCount -1); i++)
{
caseSearch(dataGridView1[4, i]);
}
}
private void caseSearch(DataGridViewCell currentCell)
{
string text = Convert.ToString(currentCell.FormattedValue);
if (text.Contains("LoadPort")) dyeCell(Color.FromArgb(193, 155, 2), currentCell); //dark mustard
else if (text.Contains("ProcessJobManager")) dyeCell(Color.FromArgb(153,153,153), currentCell); //grey
else if (text.Contains("Chamber")) dyeCell(Color.FromArgb(131,240,252), currentCell); //baby blue
else if (text.Contains("exception")) dyeCell(Color.FromArgb(255,0,0), currentCell); //red
else dyeCell(Color.White, currentCell); //white
}
private void dyeCell(Color C, DataGridViewCell currentCell)
{
//change the cell colour based on some key word searches
currentCell.Style.BackColor = C;
dataGridView1[currentCell.ColumnIndex - 1, currentCell.RowIndex].Style.BackColor = C;
dataGridView1[currentCell.ColumnIndex + 1, currentCell.RowIndex].Style.BackColor = C;
}
理想情况下,我有expandCollumns
,fillInRow
和markRows
没有挂起GUI,但在视觉上我希望看到每一行都弹出,并且每个单元都被标记为发生(当前实现dataGridView闪烁灰色,直到处理并标记所有数据)