Making Rows in an Editable DataGrid Un-Editable
original article by : Scott Mitchell
http://datawebcontrols.com/faqs/Editing/MakingRowsUneditable.shtml
Private Sub dgSchedules_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgSchedules.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
'Columns {templateColumn = 0, Date, Amount, BU_Name, BU_Id = 4}
Dim bu As String = e.Item.Cells(4).Text
Dim BU_Id As Integer = CInt(bu)
Dim objCommon As New CommonFuncs
Dim isWritable As Boolean = objCommon.BusinessUnitIsWritable(BU_Id)
If Not isWritable Then
'USER CANNOT EDIT THIS ROW then
'Hide the Edit column control
'(not applicable here, we have just text, no control)
e.Item.Cells(0).Controls(0).Visible = False
'disable does not work! (looks disabled but you can still click on it!)
e.Item.Cells(0).Enabled = False
'do not make invisible (pulls columns from right to left, ugh!)
'e.Item.Cells(0).Visible = False
'wipe the "Edit" text instead
e.Item.Cells(0).Text = ""
'-------------------------------------------------------------------------------
'set a custom "hide" Attribute
'-------------------------------------------------------------------------------
e.Item.Cells(0).Attributes.Add("hide", "true")
End If
End If
End Sub
Private Sub CheckOrUncheckTheCheckbox(ByVal Checked As Boolean)
Dim dataGridItem As DataGridItem
'-------------------------------------------------------------------------------
'if not hide attribute, then check/uncheck
'-------------------------------------------------------------------------------
For Each dataGridItem In Me.dgLetters.Items
'-------------------------------------------------------------------------------
'locate the checkbox
'-------------------------------------------------------------------------------
Dim checkBox As CheckBox
checkBox = dataGridItem.FindControl("cbxSelect")
'-------------------------------------------------------------------------------
'locate the Attribute
'-------------------------------------------------------------------------------
Dim attrib As String = dataGridItem.Cells(0).Attributes("hide")
If attrib = "" Then
'If Not obj Is Nothing Then
If checkBox.Visible Then
checkBox.Checked = Checked
End If
Else
'-------------------------------------------------------------------------------
'else hide attribute = true, hite the checkBox
'-------------------------------------------------------------------------------
checkBox.Visible = False
End If
Next
End Sub