如果value为true,则更新SQL列 [英] Update a SQL column if value is true
本文介绍了如果value为true,则更新SQL列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一点值(在状态中)我想在gridview中显示其状态,如果为true,则行显示Active,否则行显示Inactive,这是我的代码,但结果显示为True或虚假本身。并且基于Active或Inactive,Action列必须显示Deactivate或Activate
ShopNumber ShopName地址网站状态操作设置
2 abc Kuwait xyz.com True取消激活编辑
3 def科威特deuuy False激活编辑
4 主要次要Usra.com True激活编辑
5 Isys Kuwait isys.com False激活编辑
< span class =code-digit> 6 Avenues Kuwait avenues.com False激活编辑
这是代码隐藏:
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 ]。文本= 无效;
}
}
这是ASP代码:
< asp:GridView < span class =code-attribute> ID = GridView1 runat = server AllowPaging = True
AutoGenerateColumns = False DataSourceID = SqlDataSource1
AllowSorting = True
< span class =code-attribute> onselectedindexchanged = GridView1_SelectedIndexChanged
DataKeyNames = TempID >
< 列 >
< asp:BoundField DataField = ShopNumber HeaderText = ShopNumber SortExpression = ShopNumber >
< ItemStyle 宽度 = 150px > < / ItemStyle >
< / asp:BoundField >
< < span class =code-leadattribute> asp:BoundField DataField = ShopName HeaderText = ShopName SortExpression = ShopName >
< ItemStyle 宽度 = 170px > < / ItemStyle >
< / asp:BoundField >
< asp:BoundField DataField = 地址 HeaderText = 地址 SortExpression = 地址 >
< ItemStyle 宽度 = 170px > < / ItemStyle >
< / asp:BoundField >
< asp:BoundField DataField = 网站 HeaderText = 网站 SortExpression = 网站 >
< ItemStyle 宽度 = 150px > < / ItemStyle >
< / asp:BoundField >
< asp:TemplateField HeaderText < span class =code-keyword> = 状态 SortExpression = 状态 >
< EditItemTemplate >
< asp:TextBox ID = textStatus runat = 服务器 文字 =' <% #Bind( 状态)%>' > < / asp:TextBox >
< / EditItemTemplate >
< ItemTemplate >
< asp:Label ID = Label2 runat = server 文本 =' <% #Bind( 状态)%>' > < / asp:Label >
< / ItemTemplate >
< ItemStyle 宽度 = 150px / >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 行动 SortExpression = 行动 < span class =code-keyword>>
< EditItemTemplate >
< asp:TextBox ID = textAction runat = 服务器 正文 =' <% #Bind( 操作)%>' > < / asp:TextBox >
< / EditItemTemplate >
< ItemTemplate >
< asp:标签 ID = Label1 runat = server 文本 =' <% #Bind( 操作)%>' > < / asp:标签 >
< / ItemTemplate >
< ItemStyle 宽度 = 150px < span class =code-attribute> / >
< / asp:TemplateField >
< asp: CommandField HeaderText = 设置 ShowEditButton = True >
< ItemStyle 宽度 = 150px > < / ItemStyle >
< / asp:CommandField >
解决方案
您好,
试试这个。它会工作。
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.Row) .RowType == DataControlRowType.DataRow)
{
Label Label2 =(Label)e.Row.FindControl(Label2);
Label Label1 =(Label)e.Row.FindControl(Label1);
if(Convert.ToBoolean(Label2.Text.Trim()))
{
Label2.Text =Active;
Label1.Text =激活;
}
else if(!Convert.ToBoolean(Label2.Text.Trim()))
{
Label2.Text =Inactive;
Label1.Text =停用;
}
}
}
遗漏:OnRowDataBound =GridView1_RowDataBound
在你的Gridview控件中
< asp:GridView ID = < span class =code-string> GridView1 runat = server AllowPaging = True
AutoGenerateColumns = False DataSourceID = SqlDataSource1
AllowSorting = True
onselectedindexchanged = GridView1_SelectedIndexChanged
DataKeyNames = TempID跨度>>
I have a bit value (in Status) i want to display its status in gridview as if true, the row display "Active", otherwise the row display "Inactive", this is my code, but the result displays True or false itself. And based on the Active or Inactive, the Action column must display Deactivate or 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
Here is the codebehind:
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";
}
}
Here is ASP code:
<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>
解决方案
Hi,
Try this. It will work.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Label Label2 = (Label)e.Row.FindControl("Label2"); Label Label1 = (Label)e.Row.FindControl("Label1"); if (Convert.ToBoolean(Label2.Text.Trim())) { Label2.Text = "Active"; Label1.Text = "Activate"; } else if(!Convert.ToBoolean(Label2.Text.Trim())) { Label2.Text = "Inactive"; Label1.Text = "Deactivate"; } } }
Just Missing : OnRowDataBound="GridView1_RowDataBound"
In your Gridview control
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AllowSorting="True" onselectedindexchanged="GridView1_SelectedIndexChanged" DataKeyNames="TempID">
这篇关于如果value为true,则更新SQL列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文