C# 清除数据表

C# 清除数据表,c#,asp.net,gridview,C#,Asp.net,Gridview,我试图在单击按钮后用新内容刷新DataTable,但它也会再次显示以前的值。我试过clear()但对我不起作用 protected void btnListItems_Click(object sender, EventArgs e) { lblMessage.Visible = false; //lblEnddatse.Visible = true; Boolean status = true; Util objUtil = new Util();

我试图在单击按钮后用新内容刷新DataTable,但它也会再次显示以前的值。我试过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();
    }

}
protectedvoid btnListItems\u单击(对象发送方,事件参数e)
{
lblMessage.Visible=false;
//lblEnddatse.Visible=true;
布尔状态=真;
Util objUtil=new Util();
字符串消息=”;
DateTime SDate=新的DateTime();
DateTime EDate=新的日期时间();
字符串str=“”;
DataTable tbl=新的DataTable();
DataTable dt=新的DataTable();
数据行dr;
字符串[]s1;
dt.Clear();
//DirectoryInfo d=新的DirectoryInfo();
s1=Directory.GetFiles(@“C:/TextFiles”);
for(int i=0;i=DateTime.ParseExact(fromdate.ToString(),“dd/MM/yyyy”,System.Globalization.CultureInfo.InvariantCulture))
{
if((DateTime.ParseExact(a.ToString(),“dd/MM/yyyy”,System.Globalization.CultureInfo.InvariantCulture))5000)
{
lblMessage.Text=“+f1.Name+”已达到其大小限制。“;
}
其他的
{ }
}
如果(dt.Rows.Count>0)
{
gvFileGenStatus.DataSource=dt;
gvFileGenStatus.DataBind();
}
}
如何在切换日期后每次单击按钮时刷新datagridview显示的数据。 提前感谢您的帮助。

请尝试:

gvFileGenStatus.Rows.Clear();

您使用的是microsoft的
DataGridView
还是它的另一个组件?
dt.Dispose();

or in 

DataTable dt_null = new DataTable();

if (dt.Rows.Count > 0)
{
    gvFileGenStatus.DataSource = dt_null;
    gvFileGenStatus.DataBind();
}