新手求助
vb.net吧
全部回复
仅看楼主
level 3
我之前学的是vb现在开始学习vb.net。现在请问各位前辈,如何设置DataGridview这个表格控件的行和列的数目,还有怎么向单个单元格中添加数据。 DataGridView1(1, 2).Value = 1这个为什么不对哪?谢谢!
2012年11月07日 05点11分 1
level 10
设置 是先清除 一般只清除行 给你一个我写的函数,用带逗号分隔的字符串来创建列名
Public Sub SetGridCol(ByVal Setvalue As String, ByVal Omm As DataGridView, Optional ByVal OrClear As Boolean = True)
Dim Lkk() As String = Split(Setvalue, ",")
If OrClear Then Omm.Columns.Clear()
For i% = 0 To Lkk.Length - 1
Omm.Columns.Add("AA" & i, Lkk(i))
Next
End Sub
下面这个是我把DataGridView的读取包装了一下
Public MainGrid As DataGridView
Public Function Cell(ByVal Row As Integer, ByVal Col As Integer) As Object
Return MainGrid.Rows.Item(Row).Cells(Col).Value.ToString.Trim
End Function
用法
maingrid=me.datagridview1
dim a$=cell(1,0)
希望对你有所帮助。
2013年01月10日 03点01分 3
ByVal Setvalue As String 这个参数 传入就像 "姓名,性别,年龄" 这样的
2013年01月10日 03点01分
level 10
这个是查找的 接上面
Public Function FindRow(ByVal Col As Integer, ByVal Str As String) As Integer
Dim Oll% = -1
For i = 0 To MainGrid.RowCount - 1
If Cell(i, Col) = Str Then
Oll = i
Exit For
End If
Next
Return Oll
End Function
2013年01月10日 03点01分 4
level 10
最后这个是 根据sql语句填充DataGridView
注意ConnStr要先初始化,
其中可以看到对行的 清除 添加 写入。
Imports System.Data
Imports System.Data.SqlClient
Public Function SetValue(ByVal SqlStr As String, ByVal Ocl As DataGridView) As String
Dim Myconn As New SqlConnection(ConnStr)
Dim Tmp As String, Ji% = 0
Dim this2 As New SqlClient.SqlDataAdapter(SqlStr, Myconn)
If Myconn.State <> ConnectionState.Open Then Myconn.Open()
Dim Omg2 As New DataSet
this2.Fill(Omg2, "Os")
Ocl.Rows.Clear()
If Omg2.Tables("os").Rows.Count > 0 Then
For I% = 0 To Omg2.Tables("os").Rows.Count - 1
Ocl.Rows.Add()
For J% = 0 To Omg2.Tables("OS").Columns.Count - 1
Ocl.Rows(I).Cells(J).Value = Omg2.Tables("OS").Rows(I).Item(J).ToString.Trim
Next
Next
Tmp = "A"
Else
Tmp = ""
End If
this2.Dispose()
Myconn.Close()
Return Tmp
End Function
2013年01月10日 03点01分 5
1