I doing a web page in asp.net and in a module have select a Excel archive with FileUpLoad
and click in a button import. Hitherto I'm fine. But in the moment than I want select other Excel archive and click in the button import, don't clear the GridView
and show error. I attempt with this because I see in other questions similar to this.
我在asp.net中做一个网页,在一个模块中,我选择了一个Excel存档文件,并在一个按钮导入中进行上传。到目前为止我很好。但此时我想选择其他Excel归档文件并单击按钮导入,不清除GridView并显示错误。我尝试用这个,因为我看到了其他类似的问题。
With this I load the Grid
我用这个加载网格
Conn = string.Format(Conn, DireccionArchivo, MostrarHDR);
OleDbConnection ConnExcel = new OleDbConnection(Conn);
OleDbCommand CmdExcel = new OleDbCommand();
OleDbDataAdapter Oda = new OleDbDataAdapter();
DataTable Dt = new DataTable();
CmdExcel.Connection = ConnExcel;
ConnExcel.Open();
CmdExcel.CommandText = "SELECT * From ["Page1$"]";
Oda.SelectCommand = CmdExcel;
Oda.Fill(Dt);
ConnExcel.Close();
grdResultados.Caption = Path.GetFileName(DireccionArchivo);
grdResultados.DataSource = Dt;
grdResultados.DataBind();
And with this I want to clear the GridView
and last y called of new the method of load the GridView
有了这个,我想清除GridView最后y调用了新加载GridView的方法
DataTable ds = new DataTable();
ds = null;
grdResultados.DataSource = ds;
grdResultados.DataBind();
The error than show me is in the grdResultados.DataBind();
when called the second time.
显示的错误在grdresultado.net . databind()中;第二次叫的时候。
7 个解决方案
#1
23
Just use null value:
只使用null值:
grdResultados.DataSource = null;
grdResultados.DataBind();
#2
8
I resolved the problem, in the moment than clear the GridView with
我解决了这个问题,在那一刻用GridView清除了这个问题
DataTable ds = new DataTable();
ds = null;
grdResultados.DataSource = ds;
grdResultados.DataBind();
this clear the GridView but dont clear the names of columns, and this was the error, also have to clean the names of the columns. To remove the columns:
这样就清除了GridView,但不清除列的名称,这就是错误所在,还必须清除列的名称。删除列:
for (int i = 0; grdResultados.Columns.Count > i; )
{
grdResultados.Columns.RemoveAt(i);
}
and in the method of load th GridView must be generate the columns automatically with this property:
在加载GridView的方法中,必须使用该属性自动生成列:
grdResultados.AutoGenerateColumns = true;
I leave this in case anyone else has the same problem
我把它留给别人,免得别人也有同样的问题。
#3
2
int gvHasRows = grdResultados.Rows.Count;
if (gvHasRows > 0)
{
grdResultados.Columns.Clear();
grdResultados.DataBind();
}
First check that there is data in the Gridview before trying to clear. Rows has no Clear function.
在尝试清除之前,首先检查Gridview中是否有数据。行没有明确的函数。
#4
1
try this
试试这个
grdResultados.DataSource = null;
or
或
grdResultados.Rows.Clear();
then rebind the gridview
然后重装gridview
#5
1
If you use a Session clear the session Example:
如果使用会话,请清除会话示例:
DataTable ds = new DataTable();
ds = null;
GV.DataSource = ds;
GV.DataBind();
for (int i = 0; GV.Columns.Count > i; )
{
GV.Columns.RemoveAt(i);
}
ViewState["CurrentData"] = null;
#6
0
Make an empty data table with just your column names and re bind
创建一个只有列名的空数据表并重新绑定
#7
0
You can simply do this:
你可以这样做:
GridView1.SelectedIndex = -1;
#1
23
Just use null value:
只使用null值:
grdResultados.DataSource = null;
grdResultados.DataBind();
#2
8
I resolved the problem, in the moment than clear the GridView with
我解决了这个问题,在那一刻用GridView清除了这个问题
DataTable ds = new DataTable();
ds = null;
grdResultados.DataSource = ds;
grdResultados.DataBind();
this clear the GridView but dont clear the names of columns, and this was the error, also have to clean the names of the columns. To remove the columns:
这样就清除了GridView,但不清除列的名称,这就是错误所在,还必须清除列的名称。删除列:
for (int i = 0; grdResultados.Columns.Count > i; )
{
grdResultados.Columns.RemoveAt(i);
}
and in the method of load th GridView must be generate the columns automatically with this property:
在加载GridView的方法中,必须使用该属性自动生成列:
grdResultados.AutoGenerateColumns = true;
I leave this in case anyone else has the same problem
我把它留给别人,免得别人也有同样的问题。
#3
2
int gvHasRows = grdResultados.Rows.Count;
if (gvHasRows > 0)
{
grdResultados.Columns.Clear();
grdResultados.DataBind();
}
First check that there is data in the Gridview before trying to clear. Rows has no Clear function.
在尝试清除之前,首先检查Gridview中是否有数据。行没有明确的函数。
#4
1
try this
试试这个
grdResultados.DataSource = null;
or
或
grdResultados.Rows.Clear();
then rebind the gridview
然后重装gridview
#5
1
If you use a Session clear the session Example:
如果使用会话,请清除会话示例:
DataTable ds = new DataTable();
ds = null;
GV.DataSource = ds;
GV.DataBind();
for (int i = 0; GV.Columns.Count > i; )
{
GV.Columns.RemoveAt(i);
}
ViewState["CurrentData"] = null;
#6
0
Make an empty data table with just your column names and re bind
创建一个只有列名的空数据表并重新绑定
#7
0
You can simply do this:
你可以这样做:
GridView1.SelectedIndex = -1;