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

Yes..

To show a dropdown list in edit mode, and in order to stand on the last selected item i use this HTML code:

Code:
[COLOR=Blue]<asp:TemplateColumn HeaderText="Modem">
				<ItemTemplate>
					<%# DataBinder.Eval(Container.DataItem,"Modem") %>
				</ItemTemplate>
				<EditItemTemplate>
					<asp:DropDownList id=Dropdownlist2 datasource='<%# GetDataSet("Modem_Catalog","Name") %>' Runat="server" DataValueField="Name" DataTextField="Name" SelectedIndex='<%#GetSelectedIndex(Container.Dataitem("Modem"),"Modem_Catalog","Name") %>'>
					</asp:DropDownList>
				</EditItemTemplate>
			</asp:TemplateColumn[/COLOR]>
The GetDataSet function will return a datatable with from a database.
The GetSelectedIndex(scroll right) function return the selected item index of the dropsownlist. This function purpose is to stand on the last selected item in the dropdown list while in edit mode.
Here is the code behind of this function:

Code:
[COLOR=Blue]Public Function GetSelectedIndex(ByVal item As String, ByVal table As String, ByVal col As String) As Integer
       hTable(table) = item ' [COLOR=YellowGreen]this is to put string instead of dropdownlist while in edit mode[/COLOR]     
   '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[/COLOR]


Now the parameter "item" is the text that i'm looking for.
So i just keep it in a public variable (or viewstate).
(I used a public hash table. because i have more than 1 dropdownlist to hide).

To use this string (item) instead of a dropdownlist i use the ItemDataBound event:

Code:
[COLOR=Blue]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 [COLOR=YellowGreen]'hide the dropdownlist[/COLOR]
            e.Item.Cells(5).Text = hTable("Modem_Catalog") [COLOR=YellowGreen]'put the lastr selected string instead of the dropdown list (in edit mode)[/COLOR]     
   End If
    End Sub[/COLOR]


i need to make the code more generic...
 
Last edited:
Actally, no need to make the dropdownlist.visibal=false.
Just to override the cell, with a dropdownlist, with some text is enough.
 
Back
Top