0
我試圖顯示數據,我從我的程序中的文本文件拖到DataGridView,但我很難找到如何做到這一點,另一個問題是,當窗體打開它停止運行代碼。C#顯示數據從程序到DataGridView
這裏是我的代碼的主要部分:
DataGridView dataGridView = new DataGridView();
IList<Machine> machines = new BindingList<Machine>();
dataGridView.DataSource = machines;
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = hostIP,
UserName = userName,
Password = passWord,
PortNumber = 22,
SshHostKeyFingerprint = "ssh-rsa 2048 96:48:96:52:8c:e7:de:c6:e1:00:08:7e:db:ad:e4:06"
};
using (Session session = new Session())
{
session.Open(sessionOptions);
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
session.GetFiles(remotePath, @"C:\Users\mark\Desktop\Project Dex\Temp\").Check();
}
DirectoryInfo directorySelected = new DirectoryInfo(@"C:\Users\mark\Desktop\Project Dex\Temp\PROCESSED\");
List<string> fileNames = new List<string>();
foreach (FileInfo fileInfo in directorySelected.GetFiles("*.zip"))
{
fileNames.Add(fileInfo.Name);
}
foreach (string fileName in fileNames)
{
string zipFilePath = localPath + fileName;
using (ZipFile zip1 = ZipFile.Read(zipFilePath))
{
var selection = (from e in zip1.Entries
where (e.FileName).StartsWith("01e")
select e);
Directory.CreateDirectory(zipTemp);
foreach (var e in selection)
{
e.Extract(zipTemp, ExtractExistingFileAction.OverwriteSilently);
}
}
DirectoryInfo dexDirect = new DirectoryInfo(@"C:\Users\mark\Desktop\Project Dex\zipTemp\");
List<string> dexName = new List<string>();
foreach (FileInfo dexInfo in dexDirect.GetFiles("*.dex"))
{
dexName.Add(dexInfo.Name);
}
foreach (string dexNames in dexName)
{
string dexFilePath = zipTemp + dexNames;
string[] lines = System.IO.File.ReadAllLines(dexFilePath);
foreach (string line in lines)
{
machineCashCount = Array.Find(lines,
element => element.StartsWith("VA1", StringComparison.Ordinal));
}
string[] MCC1 = machineCashCount.Split('*');
string[] nm = dexNames.Split('.');
int nam = int.Parse(nm[0], System.Globalization.NumberStyles.HexNumber);
//Console.WriteLine((nam + (":") + "Total cash count: ") + MCC1[1]);
//Console.WriteLine((nam + (":") + "Number of paid vends: ") + MCC1[2]);
Machine m = new Machine();
m.MacNum = nm[0];
m.CashCount = MCC1[1];
m.VendCount = MCC1[2];
machines.Add(m);
}
}
Application.Run(new Form1());
}
}
}
謝謝您的回覆,可惜我還沒有完全瞭解你的答案,因爲我不能讓這些值在實際顯示我的形式。我已經更新了代碼,我編寫了 –
@MarkBuckley,首先確保你確實有數據。其次,確保你的daragridview真的被添加到你的表單中,而不僅僅是聲明。 我已經添加了適用於我的最小樣本。 – 0xFF
我複製了你的代碼,它的工作,但我得到一個灰色的框在列的頂部,我已經添加了一個截圖,任何想法可能會導致它? –