datagridview

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
 
C

cj

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.
 
Z

zacks

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top