开发者

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

开发者 https://www.devze.com 2023-03-30 00:41 出处:网络
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.

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="M开发者_运维技巧iddle" />
</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

0

精彩评论

暂无评论...
验证码 换一张
取 消