从datagridview更改为简单文本框

时间:2013-06-12 15:02:45

标签: vb.net datagridview textbox oledb

我有一个程序,它从excel文件中放入一个数据单元并将其写入datagridview单元格。但是,由于我只想写一个数据单元,我认为将excel文件中的数据写入一个简单的文本框可能更好。

我无法找到如何将简单文本框与excel文件连接。如果有人对如何实现这一点有一些建议,我将不胜感激。

以下是我目前使用datagridview的代码。

'This code sample here uses a for next loop to match the excel column and rows up with the datagridview's rows and columns. 
Dim rowindex As Integer
Dim columnindex As Integer
For rowindex = 1 To DataGridView3.RowCount
    For columnindex = 1 To DataGridView3.ColumnCount
        objworksheet3.Cells(rowindex + 3, columnindex + 1) =     DataGridView3(columnindex - 1, rowindex - 1).Value
    Next
Next

'This code uses the oledatadapter to pull only the cells that I want from the excel file to populate the datagridview cell. 
MyCommand3 = New OleDbDataAdapter("select * from [myWorksheet$B3:B4]", MyConnection)

ds3 = New System.Data.DataSet()
MyCommand3.Fill(ds3)

2 个答案:

答案 0 :(得分:1)

  

对于rowindex = 1到DataGridView3.RowCount

我建议,对于rowindex = 0到DataGridView3.RowCount -1

答案 1 :(得分:0)

您可以直接从数据集中获取数据;例如,从第一列的第一行检索数据:

Textbox1.text = ds.Tables(0).Rows(0)(0).ToString

如果你想获取更多的单元格,你可以循环遍历行和列