顯示具有 BindingSource 標籤的文章。 顯示所有文章
顯示具有 BindingSource 標籤的文章。 顯示所有文章

2013年1月4日 星期五

[技巧] BindingSource.item 透過[欄位名稱] 取代 [索引值] 來取值...

假設DB一筆Row共用100個欄位(從1開始~100)
而你想要的值在第37個位置(從0開始~99)
則bs的屬性item取值如下:

(法一) 這種方式是必須去算所要的值在第幾個索引,算到頭都昏了!!
bindingsource.item(0)(37).tostring

(法二) 這種方式方便多了,當你將DB bind到 bs時,也會有欄位名稱!!
bs.item(0)("欄位名稱").tostring

ex:欄位名稱為 "匯率"
bs.item(0)("匯率").tostring

Note:
datatable,dataset,datagridview,etc... 等同道理!

[C#]
((DataRowView)bs_印字[bs_印字.Position])["MARK1"]

參考:
BindingSource.Item 屬性
http://hi.baidu.com/1981633/item/88a5cb89c071d02b110ef30c
BindingSource使用模式 - Data Binding基礎知識 (一)

2012年12月20日 星期四

[技巧] DataGridView 實現 CurrentRow 上移 下移

After:


BeFore:



解決:
        bs.DataMember = "Head"
        bs.DataSource = Me.MyCallForm.DataGridView3.DataSource '另一個視窗的DGV3
        DataGridView1.DataSource = bs

(運用bindingsource)
With DataGridView1
     If .Rows.Count > 0 Then
          '到底了 沒辦法再往下移 so 跳開副程式
          If .CurrentRow.Index = .Rows.Count - 1 Then Exit Sub
                Dim a As String = Nothing
                '交換上下 Row的資料
                For i As Integer = 0 To .ColumnCount - 1
                    a = bs.Item(bs.Position + 1)(i).ToString
                    bs.Item(bs.Position + 1)(i) = bs.Item(bs.Position)(i).ToString
                    bs.Item(bs.Position)(i) = a
                Next
          'refresh DGV的資料
          .Refresh()
          'CurrentRow 位置 +1
          .CurrentCell = .Rows(bs.Position + 1).Cells(5)
     End If
End With


(運用datatable)
With DataGridView1
     If .Rows.Count > 0 AndAlso .CurrentRow IsNot Nothing Then
         If .CurrentRow.Index = 0 Then Exit Sub '到頂了 沒辦法再往上移 so 跳開副程式
         dr.ItemArray = DSquotation.Tables("Memo").rows(.CurrentRow.Index - 1).ItemArray
         DSquotation.Tables("Memo").rows(.CurrentRow.Index - 1).ItemArray = DSquotation.Tables("Memo").rows(.CurrentRow.Index).ItemArray
         DSquotation.Tables("Memo").rows(.CurrentRow.Index).ItemArray = dr.ItemArray
      End If

      .Refresh()
      .CurrentCell = .Rows(.CurrentRow.Index - 1).Cells("說明")
End With

參考:

DataGridView 控制項 (Windows Form)
C#中,DataGridView 有 Binding DataSource 的 Rows Add/ Remove 作法
C# Winform DataGridView实现行[Row]的上下移动........
BindingSource Methods
如何手動移動Datagridview的列
求 DataGridview Row 资料任意上下移动对调,该怎么解决
DataGridView手動新增、修改資料列
當控制項已繫結資料時 無法以程式設計的方式將資料列加入 DataGridView 的資料列集合
DataGridView加入欄位
[C#]DataGridView的RowChanged event

兩行 / 兩列 / 行與列 交換
有關資料表的排序問題
如何将DataTable中的某两行记录调换顺序?
dataTable交换两行数据
交换DataTable中的行列位置
DataTable实现列位置交换,用于SQL语句无法解决字段页面显示顺序问题

[C#]
((DataRowView)bs_印字[bs_印字.Position])["MARK1"]

(待運用...)
.select
.copyto