[求助]DataGrid列数根据需要显示的列数动态变化,且可修改值,并且回传
现在想做一个通用页面,在页面加载初期根据设定的列数不同,显示相应的数据,并且可以对数据进行修改,回传到数据库。
使用ASP.NET+VB.NET+access自带数据库author.mdb,遇到的问题就是,怎么在datagrid中加入文本框,本人新手,多多指教。
现在的做法就是在页面加入一个datagrid,不作任何设置,然后再对应的.vb代码的page load方法内如下编写:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码()
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim i As Integer
Dim title(3) As String
Dim bField(3) As String
'---------将来会改成从数据库的配置表读取 start
title(0) = "编号"
title(1) = "姓名"
title(2) = "出生日期"
bField(0) = "Au_ID"
bField(1) = "Author"
bField(2) = "YearBorn"
'---------将来会改成从数据库的配置表读取 end
dgTest.AutoGenerateColumns = False
'对datagrid进行设置
For i = 0 To 2
Dim col = New BoundColumn
col.headertext = title(i)
col.datafield = bField(i)
Me.dgTest().Columns.Add(col)
Me.dgTest.Columns(i).Visible = True
Next
'绑定数据
Dim dbname As String
dbname = Server.MapPath("authors.mdb")
myConnection = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" & dbname)
myConnection.Open()
myCommand = New OleDbCommand("select * FROM Authors", myConnection)
dgTest.DataSource = myCommand.ExecuteReader()
dgTest.DataBind()
End Sub
------解决方案--------------------
肯定得得用模版列啊,然后在模版列的编辑项中加文本框啊,原理跟你这一样啊,你这样写是对的