我正在创建一个动态gridview函数,它将DB中的不同表绑定到数据表中,然后将数据表分配给gridview!这是它的工作原理,我有一个下拉列表,gridview和一个按钮,该按钮将根据下拉列表选择触发特定功能,然后gridview将绑定数据,我的问题是,当你第一次按下按钮时, gridview将绑定来自DB的数据,第二次按下,gridview将从第一次按下的数据中复制数据!如何清除gridview以避免数据重复?
Private Sub Login()
sSql = "" & _
"SELECT TYPE, SUBTYPE, LOGTS, ACTION, USERID, STAT1 " & _
"FROM i_LOG " & _
"WHERE TYPE = 'USR' AND SUBTYPE = 'LOG' " & _
"AND CONVERT(VARCHAR(20), LOGTS, 103) >= '" & txtDTFrom.Text & _
"' AND CONVERT(VARCHAR(20), LOGTS, 103) <= '" & txtDTTo.Text & "'"
DT = CreateDataTable(sSql) 'Retrieve from database.
Session(sSesDT) = DT
GVM.DataTable = DT
GVM.GVAddEmptyRow()
Dim seq As New BoundField
Dim Type As New BoundField
Dim SubType As New BoundField
Dim Logts As New BoundField
Dim action As New BoundField
Dim user As New BoundField
Dim status As New BoundField
seq.HeaderText = GVM.DTNumberField
seq.DataField = GVM.DTNumberField
Type.HeaderText = "Type"
Type.DataField = "TYPE"
SubType.HeaderText = "Subtype"
SubType.DataField = "SUBTYPE"
Logts.HeaderText = "Date and Time"
Logts.DataField = "LOGTS"
action.HeaderText = "Action"
action.DataField = "ACTION"
user.HeaderText = "User ID"
user.DataField = "USERID"
status.HeaderText = "Status"
status.DataField = "STAT1"
gv.AutoGenerateColumns = False
gv.Columns.Add(Type)
gv.Columns.Add(SubType)
gv.Columns.Add(Logts)
gv.Columns.Add(action)
gv.Columns.Add(user)
gv.Columns.Add(seq)
gv.Columns.Add(status)
gv.DataSource = Session(sSesDT)
gv.DataBind()
End Sub
Private Overloads Function CreateDataTable(ByVal sSql As String) As DataTable
Dim DA As New OleDb.OleDbDataAdapter
Dim dtDataTbl As New DataTable
Dim dcDataCol As New DataColumn
With DBMgr
.openCnn()
.SQL = sSql
.openRst()
DA.Fill(dtDataTbl, .rst)
End With
'==Adding Columns==
dcDataCol = New DataColumn 'No
With dcDataCol
.DataType = GetType(Int32)
.ColumnName = GVM.DTNumberField
.AutoIncrement = True
'.AllowDBNull = True
End With
dtDataTbl.Columns.Add(dcDataCol)
'**Adding Columns**
Return dtDataTbl
End Function
答案 0 :(得分:26)
抱歉,这是在C#中,但只是在绑定任何新数据之前调用它,它将清除所有现有数据......除非问题是您分配的数据源具有重复数据。
gridview.DataSource=null;
gridview.DataBind();
另外,请注意,每次调用该过程时都要添加每一列?你需要运行:
gridview.Columns.Clear();
答案 1 :(得分:1)
您可以重置其数据源并重新绑定它,例如
gv.datasource=""
gv.databind()
然后重复使用
答案 2 :(得分:0)
通常在重新绑定网格时不会发生这种情况,如:
grid.DataSource = 'NEW_DATA_SOURCE'
grid.DataBind
我认为问题在于您的函数获取数据:
CreateDataTable(sSql)
如果您每次都要将数据提取到新的DataTable中或使用相同的数据表(可能在全局范围内声明或从您的类传递到数据访问组件),请在此处进行检查。我整晚都失去了解决这个问题:(
答案 3 :(得分:0)
将您的gridview放在一个包含id
的表格中:
<table id="myTable" border="0">
<tr><td>
<asp:GridView Width="765px" ID="mygrid" runat="server">
...
...
</asp:GridView>
</td></tr></table>
然后从按钮调用单击您的javascript函数:
function clearGridview()
{
var rows = document.getElementById('myTable').getElementsByTagName('tbody') [0].getElementsByTagName('tr').length;
if(rows!=0)
document.getElementById("myTable").deleteRow(0);
}