如何使用Windows窗体在datagridview中显示计数

时间:2017-06-27 07:54:03

标签: c# datagridview

在我的应用程序中,我正在显示消息,如果文件上传成功,同时我显示的消息是没有上传到消息框中。

问题是我需要在消息出现时每次都在消息框中单击“确定”按钮。假设如果没有插入40个文件,我需要单击“确定”按钮40次。我需要在datagridview中显示插入的文件和一次不插入的文件。我怎么能这样做。

if (ErrorMessage == 0)
{
    Ffname += path + "-" + "Uploaded successfully" + "\n";
}
else
{
     NotInsFiles += path + " - " + "Not Inserted" + "\n";
}
lbluplodedfile.Text = TabNotIns;
if (Ffname != null || Ffname != "")
{
    MessageBox.Show(Ffname);
    lbluplodedfile.Text = Ffname;
}
else
{
    MessageBox.Show(NotInsFiles);
}

1 个答案:

答案 0 :(得分:1)

我认为您必须循环上传文件,并且必须在此循环中添加

if (ErrorMessage == 0)
{
    Ffname += path + "-" + "Uploaded successfully" + "\n";
}
else
{
     NotInsFiles += path + " - " + "Not Inserted" + "\n";
}

当循环结束时尝试显示消息框

要在datagridview中显示图像,您必须插入DataGridViewImageColumn类型的列,然后才能在里面显示图像。

        private void ImgToDataGridView()
        {
            /* List of path of img */
            List<string> pathImgUpload = new List<string>();
            List<string> pathNotInsert = new List<string>();

            /* Just for my test */
            pathImgUpload.Add("./abc.png");
            pathImgUpload.Add("./abc.png");
            pathImgUpload.Add("./abc.png");
            pathImgUpload.Add("./abc.png");

            pathNotInsert.Add("./abc.png");
            pathNotInsert.Add("./abc.png");
            pathNotInsert.Add("./abc.png");
            pathNotInsert.Add("./abc.png");
            pathNotInsert.Add("./abc.png");

            /* Creation of columns for the good and bad img */
            DataGridViewImageColumn colImgUpload = new DataGridViewImageColumn();
            DataGridViewImageColumn colImgNotInsert = new DataGridViewImageColumn();
            dataGridView1.Columns.Add(colImgUpload);
            dataGridView1.Columns.Add(colImgNotInsert);

            /* Max of size of pathImgUpload and pathNotInsert */
            var lineadd = pathImgUpload.Count > pathNotInsert.Count ? pathImgUpload.Count : pathNotInsert.Count;

            /* Create the good number of line (-1 because a first line is already in datagridview)*/
            for(int i = 0; i <lineadd - 1; i++)
            {
                dataGridView1.Rows.Add();
            }

            /* adding good img */
            for (int i = 0; i < pathImgUpload.Count(); i++)
            {
                string path = pathImgUpload[i];
                var img = new Bitmap(path);
                dataGridView1.Rows[i].Cells[0].Value = img;
            }

            /* adding bad img */
            for(int i = 0; i < pathNotInsert.Count();i++)
            {
                string path = pathNotInsert[i];
                var img = new Bitmap(path);
                dataGridView1.Rows[i].Cells[1].Value = img;
            }
        }