2011-10-12 74 views
1

VB.Net的DataGridView的支持命名列,但是,我想知道,如果它能夠以某種方式做行一樣的,所以我們可以使用這樣的事情:將名稱設置爲DataGridView控件中的行?

With DataGridView1 
    .Rows.Add(4) 

    .Rows(0).Name = "Setting1" 
    .Rows(1).Name = "Setting2" 

    'Added two columns at design time 
    .Columns("Key").Row("Setting1") = "Key1" 
    .Columns("Value").Row("Setting1") = "Value1" 
    .Columns("Key").Row("Setting2") = "Key2" 
    .Columns("Value").Row("Setting2") = "Value2" 
End With 

你確認這是不可能的,我們必須使用數字索引來引用行號?

+0

它必須是整數。看看:http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrowcollection.item.aspx#Y0 – Ryan

回答

1

DataGridViewRow沒有Name財產,所以不,這是不可能的。

你總是可以使用Dictionary綁一個名稱爲DataGridViewRow

Dim dictRows As New Dictionary(Of String, DataGridViewRow) 

'Map names to corresponding rows 
dictRows.Add("Setting1", DataGridView1.Rows(0)) 
dictRows.Add("Setting2", DataGridView1.Rows(1)) 

'Access rows with names, using dictionary 
dictRows("Setting1").Cells("Key").Value = "Key1" 
dictRows("Setting1").Cells("Value").Value = "Value1" 
+0

感謝您的想法。 – Gulbahar

0

存在的方式來做到這一點使用.value的

For Each dr As DataGridViewRow In DataGridView1.Rows 
Dim hashrow As String = hst("SQLServer") 
Dim cellvalue As String = dr.Cells(hashrow).Value.ToString 
         values = values & "'" & cellvalue & "' , " 
+1

請問您能解釋一下您的解決方案嗎? – fast

+0

當你有一個DataGridViewRow你可以使用Cells屬性訪問單個值時,只需要將該列的名稱或索引放在這裏,就像dr.Cells('Column1')。Value.ToString – VladimirEncina

相關問題