我有一個返回值的2列的SQL查詢: 顯示DataGridView的價值觀 - vb.net
country | number
NA | 1
IN | 2
CN | 3
DE | 4
等。
我試圖做下列之一:
- 分配這些變量賦值我可以複製到Excel工作簿
- 或者僅使用DGV作爲媒介中的值複製到文本框。
例如,我有一個窗體的國家標籤和文本框旁邊。我想單擊一個按鈕並將數據複製到匹配的文本框中。
其中DGV行值= CN的DGV編號值將爲3,該值將被複制到CN值文本框中。
我有一個返回值的2列的SQL查詢: 顯示DataGridView的價值觀 - vb.net
country | number
NA | 1
IN | 2
CN | 3
DE | 4
等。
我試圖做下列之一:
例如,我有一個窗體的國家標籤和文本框旁邊。我想單擊一個按鈕並將數據複製到匹配的文本框中。
其中DGV行值= CN的DGV編號值將爲3,該值將被複制到CN值文本框中。
如果您僅將DGV用作介質,但實際上不顯示它,請改用字典。使用SQLcommand(cmd)輸出SQLDataReader,其中國家代碼是鍵,數字是值。那麼它的那樣簡單:
Dim dc As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim cmd As SqlCommand = "your query string"
cmd.Connection.ConnectionString = "your con string"
Using sqlrdr As SqlDataReader = cmd.ExecuteReader()
While (sqlrdr.Read())
dc.Add(sqlrdr(0), sqlrdr(1))
End While
End Using
然後就輸出到文本框:
txtNA.Text = dc.Item("NA")
如果各國都固定爲你的問題是指,那麼你可以使用這樣的事情:
第一,使用國家的名稱命名文本框(txtNA,txtIN,txtCN,...)
然後你可以把這個代碼:
Try
For i = 0 To DataGridView1.RowCount - 1
Me.Controls("txt" & DataGridView1.Rows(i).Cells(0).Value.ToString).Text = _
DataGridView1.Rows(i).Cells(1).Value.ToString()
Next
Catch
End Try
謝謝你 - 這不是什麼我正在尋找,但實際上會幫助我正在處理的其他事情=) – Fredderf81
@RyanOtto Np ..如果你解釋更多關於你在找什麼,也許我將能夠幫助你:) –
我道歉!重新閱讀你的代碼和評論後 - 你說的實際上是完美的!所有3個都很有用。你實際上最適合我,因爲它留下沒有價值的空白文本框。 – Fredderf81
下面將不起作用「是」如果經由從在IDE數據源窗口拖動表使用類。我們需要將對象轉換爲DataTable,而不是TableAdapter,DataSet和BindingSource中的類定義。
這是一種從SQL-Server數據庫表讀取數據的方法,將數據放入DataTable中,數據表成爲BindingSource的數據源,該BindingSource成爲DataGridView的數據源。使用BindingSource,我們現在使用BindingSource來訪問行數據而不是DataGridView,並且最好轉到數據源而不是用戶界面。
BindingSource.Current是一個DataRowView,向下鑽取Row屬性,然後使用字段語言擴展方法爲當前行獲取強類型數據,如果存在當前行,您會注意到我使用了兩種形式的斷言,我們是否有數據源,並且是填充的數據源,然後查看是否確實存在當前行。
從這裏我們可以將變量,屬性或控制文本設置爲當前行的字段值。
請注意,表格加載我尋找一個特定的國家(完全可選),然後如果找到該行。
最少但不是最後,我喜歡在代碼中使用SQL時使用xml文字,所以沒有字符串連接,我們可以很好地格式化語句。
Public Class Form1
''' <summary>
''' Permits obtaining row data in DataGridView
''' </summary>
''' <remarks></remarks>
Dim bsCountries As New BindingSource
Public Property Country As String
Public Property CountryNumber As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable
Using cn As New SqlClient.SqlConnection With
{
.ConnectionString = My.Settings.KarenDevConnectionString
}
Using cmd As New SqlClient.SqlCommand With {.Connection = cn}
' xml literal to make command text
cmd.CommandText =
<SQL>
SELECT [ID],[Country],[Number]
FROM [Countries]
</SQL>.Value
cn.Open()
dt.Load(cmd.ExecuteReader)
dt.Columns("ID").ColumnMapping = MappingType.Hidden
bsCountries.DataSource = dt
DataGridView1.DataSource = bsCountries
' let's try and move to a country
Dim index As Integer = bsCountries.Find("Country", "CN")
If index > -1 Then
bsCountries.Position = index
End If
End Using
End Using
End Sub
''' <summary>
''' Put field values into TextBoxes
''' </summary>
''' <remarks></remarks>
Private Sub DoWork()
If bsCountries.DataSource IsNot Nothing Then
If bsCountries.Current IsNot Nothing Then
Dim row As DataRow = CType(bsCountries.Current, DataRowView).Row
TextBox1.Text = row.Field(Of String)("Country")
TextBox2.Text = row.Field(Of Integer)("Number").ToString
' we can also do this
Me.Country = row.Field(Of String)("Country")
Me.CountryNumber = row.Field(Of Integer)("Number")
End If
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
DoWork()
End Sub
End Class
謝謝你這個凱倫!從閱讀代碼來看,它有點凌駕於我的頭上! =)但是我仍然會玩弄它,並且更好地瞭解正在發生的事情! – Fredderf81
謝謝你這個Ethilium!我對這一切都很陌生,並沒有意識到字典。這實際上完美地爲我所需要的! – Fredderf81