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
-----------------------
In myDatagrid, there is 3 columns, account code, debit,credit.
If the user input some particular a account code in column1 , i need to
based on the contect of column1, to disable column2(debit),column3(credit)
I try processcmdkey , onKeypress, onKeydown event already ( set e.handled =
false , just like textbox event)
However, it still let the user input anyfield ( after use press'tab', the
field will gone blank)
My aim is, user press any key, the column should shown nothing .
I try the keypress event in Textbox, (it is sucessful, but I am fail in
Datagrid)