【问题标题】:How to display the count in datagridview using windows forms如何使用 Windows 窗体在 datagridview 中显示计数
【发布时间】:2017-11-30 04:37:38
【问题描述】:

在我的应用程序中,如果文件上传成功,我会显示消息,同时我会在消息框中显示文件未上传的消息。

问题是每次出现消息时我都需要在消息框中单击确定按钮。假设如果没有插入 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);
}

【问题讨论】:

  • 点击按钮有什么作用?你不能把它放在计时器里吗?
  • 不是点击按钮,是我们在消息框中得到的按钮,是正常的ok按钮
  • 嗯,是的,因为 messagebox.show 将暂停代码,直到您响应它......
  • 是的,我最终需要在 datagridview 中绑定它们,而不是在消息框中显示假设如果我上传 40 个文件并且插入了 15 个文件并且没有插入了 25 个文件,我需要在 datagridview 中用两列显示它们在插入和未插入时,在插入时我需要显示这 15 个文件,在未插入时,我最后也需要显示 25 个文件
  • 您似乎正在寻找一种方法来记录您的程序所执行的操作,为什么不创建一个包含这些消息的列表,或者一个您添加这些消息的文本框呢?另外,您在此处显示的代码是 foo 循环的一部分吗?

标签: c# datagridview


【解决方案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;
            }
        }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-28
  • 1970-01-01
  • 2015-04-18
相关资源
最近更新 更多