datagridview

  • Thread starter Thread starter cj
  • Start date Start date
C

cj

I need to make all but 2 of the columns in the datagridview readonly.
Can I do this? How? Below is my code.

Dim MySqlConnection As New SqlClient.SqlConnection
Dim MySqlCommand As New SqlClient.SqlCommand
Dim MySqlAdapter As New SqlClient.SqlDataAdapter

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
MySqlConnection.ConnectionString = "packet size=4096;user ....."
If ShowPostedToolStripMenuItem.Checked Then
MySqlCommand.CommandText = "select * from Checks"
Else
MySqlCommand.CommandText = "select * from Checks where Post
= 0"
End If
MySqlCommand.Connection = MySqlConnection


MySqlAdapter.SelectCommand = MySqlCommand


DataGridView1.DataSource = mydt

mydt.Clear()
mydt.Columns.Clear()
Try
MySqlAdapter.Fill(mydt)
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
Exit Sub
End Try
End Sub
 
Fixed it. I set the datatable columns to readonly before setting
datagridview1.datasource = mydt.

This way the datagridview seemed to know those columns were readonly.

Sometimes all I have to do is ask to figure out something.
 
I need to make all but 2 of the columns in the datagridview readonly.
Can I do this? How? Below is my code.

Dim MySqlConnection As New SqlClient.SqlConnection
Dim MySqlCommand As New SqlClient.SqlCommand
Dim MySqlAdapter As New SqlClient.SqlDataAdapter

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
MySqlConnection.ConnectionString = "packet size=4096;user ....."
If ShowPostedToolStripMenuItem.Checked Then
MySqlCommand.CommandText = "select * from Checks"
Else
MySqlCommand.CommandText = "select * from Checks where Post
= 0"
End If
MySqlCommand.Connection = MySqlConnection

MySqlAdapter.SelectCommand = MySqlCommand

DataGridView1.DataSource = mydt

mydt.Clear()
mydt.Columns.Clear()
Try
MySqlAdapter.Fill(mydt)
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
Exit Sub
End Try
End Sub

Column.ReadOnly property maybe?
 
Back
Top