2016年3月26日 星期六

vbnet dataset and datagridview 排序 (單純顯示運用)

一般在datagridview 直接支援排序,但是 dataset 卻沒跟著排序;即可採用此方法來做
------------
Private Sub DGridV_ColumnHeaderMouseClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DGridV.ColumnHeaderMouseClick
        Try
            If e.RowIndex <= -1 Then
                Dim ColumnName As String = DGridV.Columns(e.ColumnIndex).Tag
                Dim dv As DataView = odst.Tables(0).DefaultView
                Dim sort As String = ""
                Select Case DGridV.SortOrder
                    Case 1
                        dv.Sort = ColumnName & " ASC"
                        sort = ColumnName & " ASC"
                    Case 2
                        dv.Sort = ColumnName & " DESC"
                        sort = ColumnName & " DESC"
                End Select
                '--              
                odst.Tables.Clear()
                odst.Tables.Add(dv.ToTable)
                '-                
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

2016年3月16日 星期三

vbnet datagridview 合併儲存格

 Dim MyDataTable As New DataTable()
        With MyDataTable
            .Columns.Add("Col1")
            .Columns.Add("Col2")
            For i As Integer = 0 To 4
                .Rows.Add(i, Rnd())
            Next
        End With
        With DataGridView1
            .DataSource = MyDataTable
            .ReadOnly = True
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
            .RowHeadersVisible = False
        End With
    End Sub
    Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) _
                                                                                                      Handles DataGridView1.CellPainting
        With DataGridView1
            Dim Rectangle1 As Rectangle = .GetCellDisplayRectangle(0, 0, True)
            Dim Rectangle2 As Rectangle = .GetCellDisplayRectangle(0, 1, True)
            Dim Rectangle3 As Rectangle = .GetCellDisplayRectangle(0, 2, True)
            Dim Rectangle4 As Rectangle = .GetCellDisplayRectangle(0, 3, True)
            Dim Rectangle5 As Rectangle = .GetCellDisplayRectangle(0, 4, True)
            Rectangle1.X += 1
            Rectangle1.Width -= 2
            Rectangle1.Height = Rectangle1.Height + Rectangle2.Height + Rectangle3.Height + Rectangle4.Height + Rectangle5.Height - 1
            e.Graphics.FillRectangle(Brushes.White, Rectangle1)
            e.Graphics.DrawString("Merge", .Font, Brushes.Red, Rectangle3)
        end with
    end sub

2016年3月14日 星期一

ms sql pivot dynamic columns 初試

declare @name varchar(max)
set @name=''

select top 10 @name+='['+prod_sname+'],' from PRODUCT

set @name=left(@name,LEN(@name)-1)

--
declare @sql1  varchar(max)
set @sql1='
select *  from (
select top 10 prod_bar,prod_sname,sale_price from PRODUCT ) as v1
pivot
(
 sum(sale_price) for prod_sname in ('+@name+')
) as vp1 '

exec(@sql1)


mssql 名稱字串相加

在研究多層次的 pivot 用法時,發現 ...原來字串相加  這樣就行了


declare @name varchar(max)
set @name=''

select top 3 @name+=prod_sname+',' from PRODUCT

select left(@name,LEN(@name)-1)

-----


----

2016年3月13日 星期日

Pivoting on multiple columns - SQL Server

詳細請參考來源:Pivoting on multiple columns - SQL Server
---
SELECT
 Seq,
 ItemCode,
 ItemModel,
 ItemColour,
 ItemName
FROM TblPivot

-- Applying pivoting on multiple columns
SELECT
*
 FROM
 (
    SELECT
  Seq,
  ItemCode,
  ItemModel,
  ItemName,
  ItemColour
 FROM TblPivot
 ) AS P

 -- For ItemColour
PIVOT
(
  Count(ItemCode) FOR ItemColour IN ([Red], [Blue], [Green])
) AS pv1

-- For ItemName
PIVOT
(
  Count(ItemModel) FOR ItemName IN ([Samsung Mobile], [Nokia Mobile], [Motorola Mobile])
) AS pv2

GO