0
這太簡單了,我一定很愚蠢!只填寫來自數據源的更改的ado.net數據集
我有一個簡單的訪問數據庫,日誌記錄被寫入每小時幾次。
我試圖讓DataGridView顯示數據到達時。
我的「解決方案」很簡單;
當用戶點擊視圖 - >從數據庫中讀取(填充數據表) - >更新視圖。
不是我夢想的,但功能,如果完全次優。
但是,我的「解決方案」是一個爛攤子,即使屏幕上已經有599個,使用fill從數據庫中提取每一條記錄。
真的,我只想填寫一次數據表,並在他們到達時添加新記錄(或點擊需要時)。
如果您還可以解釋另一種方法(不經常調用該方法)來隱藏ID列,並將第1列(名爲DateTimeStamp)的標題更改爲TimeStamp,則爲加分點。
Public Class FormMain
Shared dataAdapter As OleDbDataAdapter
Shared logTable As New DataTable("log")
Shared commandBuilder As OleDbCommandBuilder
Shared queryString As String = "SELECT * FROM log"
Shared bindingSource As New BindingSource
Private Sub FormServerBridge_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
ConfigureDataSet()
ConfigureBindingSource()
ConfigureDataView()
Catch ex As Exception
' FIXME: Helpful for debugging purposes but awful for the end-user.
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub ConfigureDataSet()
dataAdapter = New OleDbDataAdapter(queryString, _Config.ConnectionString)
commandBuilder = New OleDbCommandBuilder(dataAdapter)
commandBuilder.GetUpdateCommand()
dataAdapter.Fill(logTable)
With logTable
.Locale = System.Globalization.CultureInfo.InvariantCulture
.PrimaryKey = New DataColumn() {logTable.Columns("ID")}
End With
End Sub
Private Sub ConfigureBindingSource()
With bindingSource
.DataSource = logTable
End With
End Sub
Private Sub ConfigureDataView()
With DataGridView
.DataSource = bindingSource
End With
End Sub
Private Sub DataGridView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView.Click
UpdateUI()
End Sub
Sub UpdateUI()
dataAdapter.Fill(logTable)
End Sub
Private Sub DataGridView_DataBindingComplete(ByVal sender As Object, ByVal e As DataGridViewBindingCompleteEventArgs) Handles DataGridView.DataBindingComplete
' FIXME: This code gets run as many times as there are rows after dataAdapter.Fill!
With DataGridView
.Columns("ID").Visible = False
.Columns(1).HeaderText = "Timestamp"
.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns(3).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End With
End Sub
End Class
p.s.對於網站和書籍的鏈接將不勝感激,即使是正確的MSDN頁面(如果你知道它在哪裏,我承認我覺得不適合細讀,我經常會迷路)。
大,這是真的清楚。 oDataTable中的o代表什麼?另外,m_wLastID(具體來說)是什麼意思? – bluekeys 2011-12-14 21:00:36