简体   繁体   中英

Edit Gridview cell value based on it's value (VB.NET)

I'm displaying a table in a Gridview. Some fields contain 0 and 1 , but mean Yes and No . When displaying the table in a Gridview I would like to edit the 0's and 1's to show Yes or No .

Any suggestions on how this could work?

Thanks!


<asp:BoundField HeaderText="Gearchiveerd?" InsertVisible="false" DataField="BestellingGearchiveerd" SortExpression="BestellingGearchiveerd">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>

Gearchiveerd? = Header

BestellingGearchiveerd = column name

Please refer the below implementation. This might really help you.

Design

<asp:gridview ID="Gridview1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Yes/No">
<ItemTemplate>
<asp:Label ID="lblYesNo" Text='<%#GetText(Eval("BitColumnName")) %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>

C# Code

protected string GetText(object value)
{
    int textValue;
    if (int.TryParse(Convert.ToString(value), out textValue))
    {
        if (textValue == 0)
            return "No";
        else
            return "Yes";
    }
    else
    {
        return "&nbsp;";
    }
}

VB Code

Protected Function GetText(value As Object) As String
    Dim textValue As Integer
    If Integer.TryParse(Convert.ToString(value), textValue) Then
        If textValue = 0 Then
            Return "No"
        Else
            Return "Yes"
        End If
    Else
        Return "&nbsp;"
    End If
End Function

You can change the Yes and No as required. Hope this is helpful

I don't know if there is a way to change 1 or 0 to yes or no with string.format, but you may want to look at the DataFormatString property of the bound field.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx

EDIT: Here is additional info w/ possible solutions: http://forums.asp.net/t/1127044.aspx/1?Boolean+Format+String+Yes+No+instead+of+True+False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM