如何更新/刷新数据网格视图?

时间:2018-09-10 11:03:58

标签: c# windows windows-forms-designer

Windows窗体应用程序有两个按钮。

第一个按钮打开文件浏览器,并在GridView中加载文件列表。 第二个按钮根据创建年份创建目录,然后移动文件。我试图在Move-Op之后使用Update()/ Refresh(),以便它显示更新的DGV。

一旦我移动了文件,我想更新/刷新datagridview。我尝试使用Update()/ Refresh()网格视图。没用而且,它没有触发任何错误。

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }
    OpenFileDialog Ofd = new OpenFileDialog();
    FolderBrowserDialog Fbd = new FolderBrowserDialog();
    DataTable DT = new DataTable();

    private void BrowserButton_Click(object sender, EventArgs e)
    {
        if (Fbd.ShowDialog() == DialogResult.OK)
        {
            SourceTextBox.Text = Fbd.SelectedPath;
        }
        try
        {
            string SrcPath = SourceTextBox.Text;
            string[] GetFiles = Directory.GetFiles(SrcPath);

            DT.Columns.Add("File Path");
            DT.Columns.Add("File Size");
            DT.Columns.Add("Date of Creation");

            for(int i = 0; i< GetFiles.Length; i++)
            {
                FileInfo FlInfo = new FileInfo(GetFiles[i]);
                long FlSize = FlInfo.Length;
                DateTime CTime = FlInfo.CreationTime;
                DT.Rows.Add(FlInfo, FlSize, CTime);
            }
            FileGridView.DataSource = DT;
        }
        catch(Exception Ex)
        {
            MessageBox.Show(Ex.ToString());
        }
    }
    private void MoveButton_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (DataGridViewRow row in FileGridView.SelectedRows)
            {
                DateTime YearColumn = Convert.ToDateTime(row.Cells[2].Value.ToString());
                string DirYear = Convert.ToString(YearColumn.Year);

                //Obtain Directory Name from Full Path
                string FPath = row.Cells[0].Value.ToString();
                string DirName;
                DirName = Path.GetDirectoryName(FPath);

                //Obtain File Name
                string FName = Path.GetFileName(FPath);

                //Creating Directory using
                if (!Directory.Exists(DirName + "\\" + DirYear))
                {
                    Directory.CreateDirectory(Path.Combine(DirName,DirYear));
                    MessageBox.Show(DirYear + " Created!");
                    File.Move(FPath, DirName+ "\\" + DirYear + "\\" + FName);
                    this.FileGridView.Update();
                    this.FileGridView.Refresh();
                    MessageBox.Show("All moved!");


                }
                else if (Directory.Exists(DirName+ "\\"+ DirYear))
                {
                    File.Move(FPath, DirName + "\\" + DirYear + "\\" + FName);
                    **this.FileGridView.Update();
                    this.FileGridView.Refresh();**
                }
                else
                {
                    MessageBox.Show("Exists!");
                }
            }

        }
        catch (Exception Ex)
        {
            MessageBox.Show(Ex.ToString());
        }
    }
}

0 个答案:

没有答案