How to use OnItemDataBound to disable a drop down box at run time on Datagrid 1.1

How can i disable/enable an item in a datagrid using the OnItemDataBound method ?
shelbyinfotechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
You can use FindControl from the DataGridItem to get a reference to the DropDownList embedded in the DataGrid, and then set Enabled = True/False.

Bob
0
samtran0331Commented:
Was putting this example together when Bob posted...but it's basically what he describes.
aspx:
    <form id="form1" runat="server">
        <div>
            <asp:DataGrid ID="Datagrid1" runat="server" AutoGenerateColumns="true">
                <Columns>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
            </asp:DataGrid>
        </div>
    </form>
 
codebehind:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            'fake data to give some rows
            Dim dt As New DataTable
            Dim col As New DataColumn("MyColumn")
            dt.Columns.Add(col)
            For i As Integer = 0 To 9
                Dim r As DataRow
                r = dt.NewRow
                r.Item("MyColumn") = i
                dt.Rows.Add(r)
            Next
            'bind to datagrid
            With Me.Datagrid1
                .DataSource = dt
                .DataBind()
            End With
        End If
    End Sub
 
    Private Sub Datagrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles Datagrid1.ItemDataBound
        'make sure we're in the grid, not header/footer
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            'disable all:
            ''DirectCast(e.Item.FindControl("DropDownList1"), DropDownList).Enabled = False
 
            'disable based on data criteria:
            If e.Item.DataItem("MyColumn") = "5" Then
                DirectCast(e.Item.FindControl("DropDownList1"), DropDownList).Enabled = False
            End If
        End If
    End Sub

Open in new window

0
Bob LearnedCommented:
Was a resolution found?  Is this question no longer needed, because it was answered, and there was no response.

Bob
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.