简体   繁体   中英

Get data from each row of a column in C #

It's as follows, I have a DataGridView with multiple columns. In each row of the DataGridView will be the version of the installation path of a program. With this value, I would like to use for the size of the folder and put it in the new column.

I have a method to calculate the size of a string:

private static long ObterTamanhoDiretorio(string tamanhoDir)
{
    DirectoryInfo dire = new DirectoryInfo(tamanhoDir.ToString());
    return dire.EnumerateFiles("*.*", SearchOption.AllDirectories).Sum(fi => fi.Length);
}

I do not know if it's something like that that I have to use.

foreach (DataGridViewRow item in dataGridView1.Rows)
{

}

This is what I got:

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;

What I would like to know is how to get this value seeing on each line. If you have any ideas, thank you.

OK, based on our comments, I believe the below is what you are after.

            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;

So we iterate through your results from searcher8 and add a new row to your datatable for each entry. The size column is then set from the value in "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);
        }

Hope that helps.

var existingValues = dataGridView1.Rows
                                  .OfType<DataGridViewRow>()
                                  .Where(x => x.Cells[installPathColumnPos].Value != null)
                                  .Select(x => x.Cells[installPathColumnPos].Value.ToString())

you can get the values by Linq please check this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM