注册 登录
编程论坛 VB.NET论坛

哪位帮帮忙

yudan1130 发布于 2005-05-13 21:15, 1313 次点击

'我动态建立了一个数据集和一个表还有2列.想往表里添加多条记录.一次添一条记录,每添完一次就刷新一下,在DATAGRID中显示出来.但是每次刷新在DATAGRID中得到的都是这一次添加的记录,以前添加的都没显示出来.只有显示一条.这是怎么回事.是不是我的数据集没有公用.哪位好人能帮一下.很感激

Dim myDataSet As DataSet Dim myDataTable As DataTable = New DataTable("attendtimetable") Dim myDataColumn As DataColumn Dim myDataRow As DataRow

'第一列 myDataColumn = New DataColumn myDataColumn.DataType = System.Type.GetType("System.String") myDataColumn.ColumnName = "date" myDataTable.Columns.Add(myDataColumn)

'第二列

myDataColumn = New DataColumn myDataColumn.DataType = System.Type.GetType("System.String") myDataColumn.ColumnName = "time" myDataTable.Columns.Add(myDataColumn)

myDataSet = New DataSet myDataSet.Tables.Add(myDataTable)

myDataRow = myDataTable.NewRow()

myDataRow("date") = TextBox7.Text myDataRow("time") = DateTime.Now myDataTable.Rows.Add(myDataRow) myDataSet.AcceptChanges() DataGrid3.DataSource = myDataSet.Tables("attendtimetable")

3 回复
#2
论坛元老2008-04-02 18:55
新手,学习中,支持
#3
fairy42008-04-03 10:58
Dim myDataSet As DataSet
 Dim myDataTable As DataTable = New DataTable("attendtimetable")
Dim myDataColumn As DataColumn
Dim myDataRow As DataRow

'第一列 myDataColumn = New DataColumn myDataColumn.DataType = System.Type.GetType("System.String")
 myDataColumn.ColumnName = "date"
 myDataTable.Columns.Add(myDataColumn)

'第二列

myDataColumn = New DataColumn myDataColumn.DataType = System.Type.GetType("System.String") myDataColumn.ColumnName = "time"
myDataTable.Columns.Add(myDataColumn)

myDataSet = New DataSet myDataSet.Tables.Add(myDataTable)
把以上內容寫到一個函數裡面,load時,調用它,

以下的放在按鈕點擊函數裡面
myDataRow = myDataTable.NewRow()
myDataRow("date") = TextBox7.Text
myDataRow("time") = DateTime.Now
myDataTable.Rows.Add(myDataRow)
myDataSet.AcceptChanges()
DataGrid3.DataSource = myDataSet.Tables("attendtimetable")
上面的有幾個變量可能要定義公共變量,我沒仔細去看,我只把你代碼分開了下自己搞下,你每添加一次,就新建了一個表,當然永遠只有一行
#4
fairy42008-04-03 11:00
我倒。2樓的還把這么老的帖子翻上來,夠無聊的!
汗!
1