Dim con = new SqlConnection("Server=localhost;Database=master;User Id=sa;Password=whatever;")
Dim sqlCmd = new SqlCommand()
With sqlCmd
.Connection = con
.CommandType = CommandType.Text
.CommandText = "Select * from spt_monitor"
End With
Dim sqlDataAdap = new SqlDataAdapter(sqlCmd)
Dim dtRecord = new DataTable()
sqlDataAdap.Fill(dtRecord)
Dim dgv = new System.Windows.Forms.DataGridView()
With dgv
.DataSource = dtRecord
.Dock = System.Windows.Forms.DockStyle.Fill
End With
AddHandler dgv.DataBindingComplete, Sub(sender, e)
'' access the columns in the DataBindingComplete event
dgv.Columns("connections").ReadOnly = True
dgv.Columns(2).ReadOnly = True
End Sub
Dim f = new System.Windows.Forms.Form()
f.Controls.Add(dgv)
f.ShowDialog()
1条答案
按热度按时间ygya80vv1#
在哪里填充数据并不重要
DataGridView
来自(为什么应该?)。使用
ReadOnly
财产DataGridViewColumn
要将其设置为只读:注意:如果您使用数据绑定(即使用
DataSource
你得等到DataBindingComplete
在更改列之前激发。下面是一个简短的运行示例: