繁体   English   中英

如果值为true,则更新SQL列

[英]Update a SQL column if value is true

我有一个值(在“状态”中),我想在gridview中显示其状态,好像是true,该行显示“ Active”,否则该行显示“ Inactive”,这是我的代码,但是结果本身显示True或false。 并且基于Active或非Inactive ,“操作”列必须显示“ Deactivate或“ Activate

ShopNumber  ShopName    Address Website   Status    Action  Settings
2             abc        Kuwait xyz.com     True    Deactivate  Edit
3             def        Kuwait deuuy       False   Activate    Edit
4            Major       Minor  Usra.com    True    Activate    Edit
5             Isys       Kuwait isys.com    False   Activate    Edit
6            Avenues     Kuwait avenues.com False   Activate    Edit

下面是代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            Boolean bitCheck = Convert.ToBoolean(e.Row.Cells[4].Text);
            if (bitCheck)
            {
                e.Row.Cells[4].Text = "Active";
            }
            else
            {
                e.Row.Cells[4].Text = "Inactive";
            }

        }

这是ASP代码:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
         AllowSorting="True" 
         onselectedindexchanged="GridView1_SelectedIndexChanged" 
         DataKeyNames="TempID">

        <Columns>
            <asp:BoundField DataField="ShopNumber" HeaderText="ShopNumber" SortExpression="ShopNumber" >
            <ItemStyle Width="150px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="ShopName" HeaderText="ShopName" SortExpression="ShopName" >
            <ItemStyle Width="170px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" >
            <ItemStyle Width="170px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="Website" HeaderText="Website" SortExpression="Website" >
            <ItemStyle Width="150px"></ItemStyle>
            </asp:BoundField>

            <asp:TemplateField HeaderText="Status" SortExpression="Status">
                <EditItemTemplate>
                    <asp:TextBox ID="textStatus" runat="server" Text='<%# Bind("Status") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Width="150px" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Action" SortExpression="Action">
                <EditItemTemplate>
                    <asp:TextBox ID="textAction" runat="server" Text='<%# Bind("Action") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Action") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Width="150px" />
            </asp:TemplateField>
            <asp:CommandField HeaderText="Settings" ShowEditButton="True" >
            <ItemStyle Width="150px"></ItemStyle>
            </asp:CommandField>

        </Columns>
    </asp:GridView>

您正在使用项目模板,而不仅仅是将其绑定到列。 因此,您必须转换Label才能编辑Text

Label status = (Label)e.Row.Cells[4].FindControl("Label2");
Label action = (Label)e.Row.Cells[5].FindControl("Label1");

Boolean bitCheck = Convert.ToBoolean(status.Text);
if (bitCheck)
{
    status.Text = "Active";
    action.Text = "Activated";
}
else
{
    status.Text = "Inactive";
    action.Text = "Deactivated";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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