problem with displaying radiobuttonlist value in gridview

B

Ben

Hi,

The gridview contains a radiobuttonlist with boolean values (true/false)
coming from a database. In normal mode, 'True' or 'False' is dispalyed. In
edit mode, the radiobuttonlist appears.

What i want is to change True' and 'False' by 'Yes' and 'No' in normal mode.

I tried this code below and i get Yes/No in normal mode, but also in edit
mode instead of the radiobuttonlist which has gone ...

Thanks for help

Ben

<asp:TemplateField>
<EditItemTemplate>
<asp:RadioButtonList ID="r1" SelectedValue='<%# Bind("myfield") %>'
runat="server">
</asp:RadioButtonList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label31" runat="server" Text='<%# Bind("myfield")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>


Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
If (e.Row.RowState And DataControlRowState.Normal) =
DataControlRowState.Normal Then
If e.Row.RowType = DataControlRowType.DataRow Then
Dim st As String
st = e.Row.Cells(5).Text
If st = "True" Then
e.Row.Cells(5).Text = "Yes"
Else
e.Row.Cells(5).Text = "No"
End If
End If
End If
End Sub
 
S

Stan

Hi Ben

The cause of your problem lies in this fragment of code:
        If (e.Row.RowState And DataControlRowState.Normal) =
DataControlRowState.Normal Then ...

You'll see why if you look at the list below which is the
DataControlRowState enumeration:

Normal = 0 (binary 0000)

Alternate = 1 (binary 0001)

Selected = 2 (binary 0010)

Edit = 4 (binary 0100)

Insert = 8 (binary 1000)

This means that

e.Row.RowState And DataControlRowState.Normal ...

... will always evaluate to 0 (x And 0 = 0 whatever x happens to be)
and hence will always equate to DataControlRowState.Normal,
which in turn means the overall boolean expression will always return
true even for rows that are in Edit mode.

Try this instead

if(e.Row.RowState And DataControlRowState.Edit) = 0 then ...

For a row in Edit mode Row.RowState can be 4 or 5 because it may be an
alternate row. Either way the result will be non-zero and hence return
false as required.
 
B

Ben

Hi, thanks for replying.

I tried this, but now, i get everywhere "No" in normal mode and the
radiobuttonlist has gone in edit mode ...


"Stan" <[email protected]> schreef in bericht
Hi Ben

The cause of your problem lies in this fragment of code:
If (e.Row.RowState And DataControlRowState.Normal) =
DataControlRowState.Normal Then ...

You'll see why if you look at the list below which is the
DataControlRowState enumeration:

Normal = 0 (binary 0000)

Alternate = 1 (binary 0001)

Selected = 2 (binary 0010)

Edit = 4 (binary 0100)

Insert = 8 (binary 1000)

This means that

e.Row.RowState And DataControlRowState.Normal ...

.... will always evaluate to 0 (x And 0 = 0 whatever x happens to be)
and hence will always equate to DataControlRowState.Normal,
which in turn means the overall boolean expression will always return
true even for rows that are in Edit mode.

Try this instead

if(e.Row.RowState And DataControlRowState.Edit) = 0 then ...

For a row in Edit mode Row.RowState can be 4 or 5 because it may be an
alternate row. Either way the result will be non-zero and hence return
false as required.
 
S

Stan

Ben

I should have spotted this before but there is another problem with
your code.

You are reading and assigning to the Text property of the cells. The
columns are templated. That means you have to retrieve the Text
property of the label controls that are bound to the data not the Text
property of the grid cell containing them.

You should be doing it something like this:

Dim MyFieldLabel as Label = e.row.cells(5).FindControl("label31")

If MyFieldLabel.Text = "True" Then
MyFieldLabel.Text = "Yes"
Else
MyFieldLabel.Text = "No"
End if

As it is I cannot understand why it worked at all previously since the
Text property of the underlying table cell could not have yielded a
meaningful result. I'd have expected "No" everywhere because the test
for "True" would necessarily have been false.

Regards
Stan
 
S

Stan

Further to my last post I have thought of an alternative approach to
this whole problem that would be simpler (I have used this technique
myself in the past).

In the item template of the "MyField" column use two labels instead of
one (One saying "Yes" the other saying "No") and bind their visibily
property to the Data instead - which I assume is Boolean.

The binding expressions will be like this:

<asp:Label ID="YesLabel" ... Text="Yes" Visible='<%# Eval("MyField")
%>' ...

<asp:Label ID="NoLabel" ... Text="No" Visible='<%# Not
Eval("MyField") %>' ...

No need for separate layout or positioning tags since only one is
rendered at a time.

This approach has the following advantages:

(1) No need for any code
(2) Only one stage to the DataBinding process instead of two.
(3) The Edit template controls are unaffected.

HTH
 
B

Ben

Thanks, your solution works.
But i also found one:

If Not ((e.Row.RowState And DataControlRowState.Edit) =
DataControlRowState.Edit) Then
If e.Row.RowType = DataControlRowType.DataRow Then
Dim myfield As Boolean = DataBinder.Eval(e.Row.DataItem, "myfield")
Dim lbl As Label = e.Row.FindControl("Label31")
.....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top