Question

I have a gridview with edit and item templates. What I would like to do is based on a value of one of the columns when a row is in editmode make two textboxes either readonly or readwrite.

The Item template consists of two image buttons edit/delete three labels (Primary, Secondary and IsSecondary). The Edit template has two imagebuttons Cancel/Save, two TextBoxes (Primary and Secondary) and a dropdown to select Primary or Secondary)

What I want to do is set the two textboxes to readonly if on entering Edit Mode the Dropdown value is Secondary. Basically when the Value is Secondary the user can only switch to Primary but cannot edit the values in the textboxes. If the value is Primary they can edit there values.

I have tried adding the following into the RowDataBound event

If e.Row.RowState = DataControlRowState.Edit Then
    If ddl_IsSecondary.SelectedValue = 1
        tb_Primary.ReadOnly = True
        tb_Secondary.Readonly = True
    Else
        tb_Primary.ReadOnly = False
        tb_Secondary.Readonly = False
    End If
End If

Unfortunately the textboxes are readwrite whatever the value of the dropdown.

Any ideas on what else I can try?

Était-ce utile?

La solution 2

Thanks Pasty... I've not tried your answer but will have a look... I actually found my answer... My testing happened to be on an even row which is an Alternate Row. So my line

If e.Row.RowState = DataControlRowState.Edit Then...

Was not being triggered... changing the code to

If e.Row.RowState = DataControlRowState.Edit OR e.Row.RowState = DataControlRowState.Alternate + DataControlRowState.Edit Then...

works fine...

Autres conseils

IMO you need to use the DataGridView.CellBeginEdit event. MSDN states:

Occurs when edit mode starts for the selected cell.

Private Sub DataGridView1_CellBeginEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
    ' Check your ddl_IsSecondary here and act accordingly.
End Sub

You probably should also ignore the event if the cell that is being edited is the cell containing the drop down list.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top