[英]Get data from each row of a column in C #
如下,我有一個帶有多個列的DataGridView
。 在DataGridView
每一行中,將是程序安裝路徑的版本。 有了這個值,我想使用文件夾的大小並將其放在新列中。
我有一種方法來計算字符串的大小:
private static long ObterTamanhoDiretorio(string tamanhoDir)
{
DirectoryInfo dire = new DirectoryInfo(tamanhoDir.ToString());
return dire.EnumerateFiles("*.*", SearchOption.AllDirectories).Sum(fi => fi.Length);
}
我不知道這是否是我必須使用的東西。
foreach (DataGridViewRow item in dataGridView1.Rows)
{
}
這就是我得到的:
ObjectQuery query8 = new ObjectQuery("SELECT Description, Version, InstallDate, Vendor, InstallLocation FROM Win32_Product");
ManagementObjectSearcher searcher8 = new ManagementObjectSearcher(scope, query8);
DataTable dt = new DataTable();
dt.Columns.Add("Program");
dt.Columns.Add("Version");
dt.Columns.Add("Installing Date");
dt.Columns.Add("Company");
dt.Columns.Add("Install Location");
dt.Columns.Add("Size");
foreach (DataGridViewRow item in dataGridView1.Rows)
{
foreach (ManagementObject queryObj in searcher8.Get())
{
var paths = new List<string>();
var installPathColumnPos = 0;
string s = item.Cells[installPathColumnPos].Value.ToString();
dt.Rows.Add(new object[] { queryObj["Description"], queryObj["Version"], FormatDateTime(queryObj["InstallDate"]), queryObj["Vendor"], queryObj["InstallLocation"], s });
}
}
dataGridView1.DataSource = dt;
我想知道的是如何在每一行上看到該值。 如果您有任何想法,謝謝。
好的,根據我們的評論,我相信以下是您所追求的。
DataTable dt = new DataTable();
dt.Columns.Add("Program", typeof(string));
dt.Columns.Add("Version", typeof(double));
dt.Columns.Add("InstallDate", typeof(DateTime));
dt.Columns.Add("Company", typeof(string));
dt.Columns.Add("InstallLocation", typeof(string));
dt.Columns.Add("Size", typeof(long));
foreach (ManagementObject queryObj in searcher8.Get())
{
var newRow = dt.NewRow();
newRow["Program"] = queryObj["Description"];
newRow["Version"] = queryObj["Version"];
newRow["InstallDate"] = FormatDateTime(queryObj["InstallDate"]);
newRow["Company"] = queryObj["Vendor"];
newRow["InstallLocation"] = queryObj["InstallLocation"];
newRow["Size"] = ObterTamanhoDiretorio(queryObj["InstallLocation"]);
dt.Rows.Add(newRow);
}
dataGridView1.DataSource = dt;
因此,我們遍歷searcher8的搜索結果,並為每個條目向您的數據表添加一個新行。 然后從“ InstallLocation”中的值設置大小列
private static long ObterTamanhoDiretorio(string tamanhoDir)
{
if (string.IsNullOrEmpty(tamanhoDir))
return 0;
DirectoryInfo dire = new DirectoryInfo(tamanhoDir.ToString());
return dire.EnumerateFiles("*.*", SearchOption.AllDirectories).Sum(fi => fi.Length);
}
希望能有所幫助。
var existingValues = dataGridView1.Rows
.OfType<DataGridViewRow>()
.Where(x => x.Cells[installPathColumnPos].Value != null)
.Select(x => x.Cells[installPathColumnPos].Value.ToString())
您可以通過Linq獲取值,請選中此復選框。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.