清除数据表

本文关键字:数据表 清除 | 更新日期: 2023-09-27 18:27:30

单击按钮后,我试图用新内容刷新数据表,但它再次显示了以前的值。我试过clear(),但它对我不起作用

    protected void btnListItems_Click(object sender, EventArgs e)
{
    lblMessage.Visible = false;
    //lblEnddatse.Visible = true;
    Boolean status = true;
    Util objUtil = new Util();
    String Message = "";
    DateTime SDate = new DateTime();
    DateTime EDate = new DateTime();
    string str = "";
    DataTable tbl = new DataTable();
    DataTable dt = new DataTable();
    DataRow dr;
    String[] s1;
    dt.Clear();
    //DirectoryInfo d = new DirectoryInfo();
    s1 = Directory.GetFiles(@"C:/TextFiles");
    for (int i = 0; i <= s1.Length - 1; i++)
    {
        if (i == 0)
        {
            //Add Data Grid Columns with name
            dt.Columns.Add("FileName");
            dt.Columns.Add("GeneratedTime");
        }
        //Get each file information
        FileInfo f = new FileInfo(s1[i]);
        FileSystemInfo f1 = new FileInfo(s1[i]);
        dr = dt.NewRow();
        //Get File name of each file name
        dr["FileName"] = f1.Name;
        dr["GeneratedTime"] = f1.CreationTime.Date.ToString("dd/MM/yyyy");
        string a = f1.CreationTime.Date.ToString("dd/MM/yyyy");
        //Insert collected file details in Datatable
        string fromdate = txtFromDate.Text.ToString();
        string todate = txtToDate.Text.ToString();
        if ((DateTime.ParseExact(a.ToString(),"dd/MM/yyyy",System.Globalization.CultureInfo.InvariantCulture)) >= DateTime.ParseExact(fromdate.ToString(),"dd/MM/yyyy",System.Globalization.CultureInfo.InvariantCulture))
        {
        if ((DateTime.ParseExact(a.ToString(), "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)) <= DateTime.ParseExact(todate.ToString(), "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture))
        {
        dt.Rows.Add(dr);
        }
        }

        if ((f.Length / 1024) > 5000)
        {
            lblMessage.Text = "" + f1.Name + " had reach its size limit.";
        }
        else
        { }
    }
    if (dt.Rows.Count > 0)
    {
        gvFileGenStatus.DataSource = dt;
        gvFileGenStatus.DataBind();
    }
}

如何在切换日期后每次单击按钮时刷新datagridview显示的数据。谢谢你提前提供的帮助。。

清除数据表

尝试:

gvFileGenStatus.Rows.Clear();
dt.Dispose();
or in 
DataTable dt_null = new DataTable();
if (dt.Rows.Count > 0)
{
    gvFileGenStatus.DataSource = dt_null;
    gvFileGenStatus.DataBind();
}