如何在Linq内部使用条件

时间:2014-08-20 13:13:00

标签: c# asp.net linq datagridview

我正在开发一个用于搜索和绑定DataGrid的应用程序。 用户将要做的是输入文本的一部分,点击搜索按钮意味着它应该从DB中获取具有类似文本框值的数据并绑定到datagrid。

但我的要求是: - 在我的表中我有3个字段,即ID,ChargeName和IorE。在我的数据网格中,我有像id,Income和Expences这样的列。我需要的是,例如: 如果我在“服务”等文本框中输入费用名称,则点击搜索意味着一旦找到 chargeName字段,就应该在费用字段中检查该名称是否应该检入 IorE字段如果值为“我”,则ChargeName应绑定到Datagrid中的收入列,如果“E”,则ChargeName应绑定到< Data.grid中的strong> Expence Column 。我正在使用LinQ,我不知道如何在LINQ中使用这样的条件

这是我的代码。

using (LQMasterChargeDataContext DB = new LQMasterChargeDataContext())
{
    var Result = from C in DB.TB_MasterCharges
                 where C.mCHR_NUIsActive == 1 && SqlMethods.Like( C.mCHR_VCName,TxtChargeName.Text.Trim())
                 select new { C.mCHR_NUPKId,
                              Income = C.mCHR_VCName
                            };

    dgSailing.DataSource = Result;
    dgSailing.DataBind();
    int Count = Result.Count();
    if (Count == 0)
    {
        LBLEXcepceValue.BackColor = System.Drawing.Color.LightPink;
        LBLEXcepceValue.Font.Bold = true;
        LBLEXcepceValue.Font.Name = "Times New Roman";
        LBLEXcepceValue.Font.Size = 10;
        LBLEXcepceValue.Text = "NO VESSEL FOUND ! ! !";
        //ScriptManager.RegisterStartupScript(this, this.GetType(), "ALERT", "alert('No Vessel found');", true);
    }

我的aspx页面就像:

<asp:UpdatePanel ID="UPChargesGrid" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:DataGrid ID="dgDestination" runat="server" BorderWidth="1px" BorderColor="#FE9B00"
                          BorderStyle="Solid" BackColor="White" Font-Names="Verdana" Font-Size="XX-Small"
                          AutoGenerateColumns="False" ShowFooter="FALSE" CellPadding="3" align="center"
                           Width="700px" OnItemCommand="dgDestination_Select">
            <Columns>
                <asp:BoundColumn DataField="ID" HeaderText="mFDD_mFRD_NUPKId" Visible="False">
                </asp:BoundColumn>
                <asp:BoundColumn DataField="Income" HeaderText="Income"></asp:BoundColumn>
                    <TemplateColumn>
                        <ItemTemplate>
                            <asp:Label ID="LBLEXcepceValue" runat="server" Text='<%# Eval("Expence").ToString().Substring(0,5) %>' ></asp:Label>
                         </ItemTemplate>
                     </TemplateColumn>
                     <asp:TemplateColumn HeaderText="SAVE">
                         <ItemTemplate>
                             <asp:ImageButton runat="server" ID="IMGBTNSave" ImageUrl="~/AppImages/Save.gif" ToolTip="Save"  CommandName="Save" 
                                   AlternateText="Save" />
                         </ItemTemplate>
                     </asp:TemplateColumn>
                </Columns>
        <PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"></PagerStyle>
      </asp:DataGrid>
    </contenttemplate>
</asp:UpdatePanel>    

任何人都可以帮我解决这个问题。谢谢。

我现在限制DataGrid我想要的是如果值为null然后一个用户控件应该显示并保存按钮(在datagrid中)应该是Enable.Please帮我这样做。这是我的需要代表通过图像 enter image description here

结果

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以像这样更改

var Result = from C in DB.TB_MasterCharges
                 where C.mCHR_NUIsActive == 1 && SqlMethods.Like( C.mCHR_VCName,TxtChargeName.Text.Trim())
                 select new { C.mCHR_NUPKId,
                              Income = C.IorE == "I" ? C.mCHR_VCName : string.Empty,
                              Expense = C.IorE == "E" ? C.mCHR_VCName : string.Empty
                            };

然后您可以使用绑定列来支付费用,如下所示

<asp:BoundColumn DataField="Expense" HeaderText="Expense"></asp:BoundColumn>

<强>更新 我希望我能正确理解你的问题。您可以使用下面的代码切换控件的可见性

<TemplateColumn>
   <ItemTemplate>
       <asp:Label ID="LBLEXcepceValue" runat="server" Text='<%# Eval("Expence") %>' 
       Visible='<%# Eval("Expence") != null && Eval("Expence") != string.Empty %>' 
       ></asp:Label>
       <asp:TextBox ID="textBoxExpenses" runat="server" Visible='<%# Eval("Expence") == null || Eval("Expence") == string.Empty %>'></asp:TextBox>
   </ItemTemplate>
</TemplateColumn>
<asp:TemplateColumn HeaderText="SAVE">
   <ItemTemplate>
      <asp:ImageButton runat="server" ID="IMGBTNSave" ImageUrl="~/AppImages/Save.gif" ToolTip="Save"  CommandName="Save" 
      Visible='<%# Eval("Expence") == null || Eval("Expence") == string.Empty %>
      AlternateText="Save" />
   </ItemTemplate>
</asp:TemplateColumn>
相关问题