如何從DataTable中取出Distinct的資料
當我們已經把資料放入DataTable之後,我們想從裡面取出類似SQL DISTINCT的資料(不重複的資料),這個時候我們沒辦法像SQL一樣直著下語法得到。
我們利用DataView的ToTable的方法中,有個參數是設定是否要distinct資料,透過這樣的方式就能夠得到我們想要的效果,以下是範例程式
範例中假設已經取得資料,要把LastName這個欄位做Distinct
星期四, 3月 25, 2010
顯示 DataGridView 的行號,可在 DataGridView 的 RowPostPaint 事件中加入以下程式碼
Private Sub DataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
Dim rectangle As New Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, DataGridView1.RowHeadersWidth – 4, e.RowBounds.Height)
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), DataGridView1.RowHeadersDefaultCellStyle.Font, rectangle, DataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter Or TextFormatFlags.Right)
End Sub
Private Sub DataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
Dim rectangle As New Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, DataGridView1.RowHeadersWidth – 4, e.RowBounds.Height)
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), DataGridView1.RowHeadersDefaultCellStyle.Font, rectangle, DataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter Or TextFormatFlags.Right)
End Sub
訂閱:
文章 (Atom)