Making a datagrid cell readonly

  • Thread starter Thread starter Tricia Colwell via DotNetMonster.com
  • Start date Start date
T

Tricia Colwell via DotNetMonster.com

Here is what i have:
I have a datagrid with two columns one is a combo box style and the other
is a textbox style. If the combo box is "Between" or "Like" i want that the
next cell to be readonly..which is the textbox one.

How do i do this???

Tricia
 
Hi,

You have to make your own datagrid column style to do that. Here is
a simple example. You need a datagrid1 on a form. This will only allow you
to edit the name of product in the northwind database if it isnt
discontinued.

Imports System.Data.SqlClient

Public Class Form1

Inherits System.Windows.Forms.Form



Dim ds As New DataSet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim conn As SqlConnection

Dim strConn As String

Dim strSQL As String

Dim da As SqlDataAdapter

'strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

'strConn &= "Data Source = Northwind.mdb;"

strConn = "Server = (local);"

strConn &= "Database = NorthWind;"

strConn &= "Integrated Security = SSPI;"



conn = New SqlConnection(strConn)

da = New SqlDataAdapter("Select * From Products", conn)

da.Fill(ds, "Products")

Dim ts As New DataGridTableStyle

ts.MappingName = ds.Tables("Products").TableName

Dim colDiscontinued As New DataGridBoolColumn

With colDiscontinued

..MappingName = "Discontinued"

..HeaderText = "Discontinued"

..Width = 80

End With

Dim colName As New EditSomeTimes

With colName

..MappingName = "ProductName"

..HeaderText = "Product Name"

..Width = 180

End With



ts.GridColumnStyles.Add(colName)

ts.GridColumnStyles.Add(colDiscontinued)

DataGrid1.TableStyles.Add(ts)

DataGrid1.DataSource = ds.Tables("Products")

ts = Nothing

colDiscontinued = Nothing

colName = Nothing

End Sub

End Class



Public Class EditSomeTimes

Inherits DataGridTextBoxColumn



Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)

Dim dt As DataTable

Try

dt = CType(Me.DataGridTableStyle.DataGrid.DataSource, DataTable)

Dim dr As DataRow

dr = dt.Rows.Item(source.Position)

If Not CBool(dr.Item("Discontinued")) Then

MyBase.Edit(source, rowNum, bounds, [readOnly], instantText, cellIsVisible)

End If

Catch ex As Exception

End Try

End Sub



End Class



Ken

-----------------------
message Here is what i have:
I have a datagrid with two columns one is a combo box style and the other
is a textbox style. If the combo box is "Between" or "Like" i want that the
next cell to be readonly..which is the textbox one.

How do i do this???

Tricia
 
Back
Top