Change a column of the DataGrid to readonly during run time ?

xstefanr

Newcomer
Joined
Feb 13, 2003
Messages
5
I want to change a column of the DataGrid to ReadOnly depending on some other data, so I need to do this in runtime.
What I am looking for is a kind of:
MyDataGrid.Columns.Item(0).ReadOnly = True
which unfortunately is not accepted, but:
MyDataGrid.Columns.Item(0).Visible = True
works fine...
Thanks for your help.
Regards, xstefanr
 
You can dynamically build the datagri with code. When you do this you can add or not add columns and set any properties you want.

James
 
Could you please give me an example of how to dynamically change a column's ReadOnly property?
Thanks for your efforts.
xstefanr
 
Yes. In the Edit Event (before MyDataGrid.EditItemIndex = CInt(e.Item.ItemIndex)) I want to check what columns are allowed to be changed and here I want to set the ReadOnly property for each column.
Thanks, xstefanr
 
I haven't tested this code but try:

e.Item.Cells(0).Enabled = False

or


Dim myColumn As DataGridColumnStyle
Dim myDataColumns As DataColumnCollection
' Get the columns for a table bound to a DataGrid.
myDataColumns = dataSet1.Tables("Suppliers").Columns
Dim dataColumn As DataColumn
For Each dataColumn In myDataColumns
dataGrid1.TableStyles(0).GridColumnStyles(dataColumn.ColumnName).ReadOnly = dataColumn.ReadOnly
Next dataColumn

James
 
James,
although it doesn't produce any error, it doesn't show any effect at all. All colums are editable as before.
The setting needs not occur in the EditEvent necessarily. I can do it anytime. Maybe this makes it easier(?)...

Thanks anyway.
Stefan
 
xstefanr said:
James,
although it doesn't produce any error, it doesn't show any effect at all. All colums are editable as before.
The setting needs not occur in the EditEvent necessarily. I can do it anytime. Maybe this makes it easier(?)...

Thanks anyway.
Stefan


Have you succeeded? I'm looking for the same!!
 
xstefanr said:
Strange, but this works...

CType(MyDataGrid.Columns(6), BoundColumn).ReadOnly = False


Thank you!

..But i think there is no solution for me :(
because i need to change the "ReadOnly" propety of a TemplateColumn. This colum show a dropdown list when user
clicked the EDIT button. But i want to give edit permission only to 1 group of users and to block it to others. The readonly property doesn't belong here:

CType(MyDataGrid.Columns(6), TemplateColumn).ReadOnly = False. (No such property! here)

so i guess there is no way to control the TemplateColumn
ReadOnly property in runtime! I hope i'm wrong.. :confused:
 
you can hide the dropdownlist

for example
in the itemdatabound event
CType(e.Item.Cells(0).FindControl("controlname"),DropDownList).Visible=0

Cells(0)...0 is the cell number which contains the ddl
 
wessamzeidan said:
you can hide the dropdownlist

for example
in the itemdatabound event
CType(e.Item.Cells(0).FindControl("controlname"),DropDownList).Visible=0

Cells(0)...0 is the cell number which contains the ddl


Thank you!

But i have a little problem...
This my code:
Code:
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        CType(e.Item.Cells(5).FindControl("Dropdownlist2"), DropDownList).Visible = False
    End Sub

I get this error:
'Object not set to an instance of an object" ..
I have about 7 dropdownlist in my datagrid, one of them calld "Dropdownlist2".
I guess he doesn't find 'Dropdownlist2'

I didn't understand the cells property. Does cells(5) applies to column 5 in the datagrid?
 
Code:
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
        CType(e.Item.Cells(5).FindControl("Dropdownlist2"), DropDownList).Visible = False
End If
    End Sub
 
wessamzeidan said:
Code:
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
        CType(e.Item.Cells(5).FindControl("Dropdownlist2"), DropDownList).Visible = False
End If
    End Sub

OK. It works!! thank you very much!
I have one more question:
Now i after i hide the dropdownlist i get a blank cell.
How do i show the text in the cell?? I don't want the user to see a blank cell.
I thought of:
I tried to add a label to ItemTemplate(thru the 'edit template' utility of my datagrid) - find a way to recive the text of the cell and write something like that:

Code:
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        If e.Item.ItemType = ListItemType.EditItem Then
            CType(e.Item.Cells(5).FindControl("Dropdownlist2"), DropDownList).Visible = False
            CType(e.Item.Cells(5).FindControl("lblModem"), Label).Text = "xx"
        End If
    End Sub

But realy it doesn't work, i get an error in earlier stage of my application, probably didn't like the label i added...

Any idea? how i can show the user to actual value and hidethe dropdowlist?
 
wessamzeidan said:
Whats the error you're getting?

Again the:
"Object reference not set to an instance of an object."

In this procedure:

'
Code:
=======================================================================
    ' This function is called after the user clicked the Edit buttonlink
    ' The purpose of this function is to display the current item of a 
    ' dropdown list while in edit mode (and not to disply thr default first item)
    ' The function return the current selectedIndex
    '========================================================================
    Public Function GetSelectedIndex(ByVal item As String, ByVal table As String, ByVal col As String) As Integer
        'loop through the dataset ddlDataSet
        Dim i As Integer
        Dim dt As DataTable = ddldataset.Tables(table) 
        For i = 0 To dt.Rows.Count - 1
            If Trim(item) = Trim(dt.Rows(i)(col).ToString()) Then
                Return i
            End If
        Next
    End Function


I think dt get the value of nothig. I haven't see way yet.
I will check it...
 
After i removed the label, and fixed the html (it was mixed up after i added the label) - it run w/o problems.

Now i'm tring to give the user something(the last cell value) instead of a blank cell (after i hide the dropdownlist control)...

Any ideas?

Thank you!
 
Thanks, i tried it now. it works fine.
But,.. is it a way that the text in cell(5) will be the last selected item of the dropdown list (or in other words: to show the same text in normal mode as well as in edit mode)?
 
Last edited:
Back
Top