2014-02-14 51 views
0

我在代碼隱藏中使用ASP.NET(4.0 Framework)和VB.NET。我有這樣的SqlDataSource這我的GridView控件使用的數據源:使用SqlDataSource SelectParameters顯示列爲NULL的gridview行

<asp:SqlDataSource ID="repairApproverData" runat="server" 
     ConnectionString="<%$ ConnectionStrings:SystemsConnectionString %>" 
     SelectCommand="SELECT CASE(dateFixed) WHEN ISNULL(dateFixed,1) THEN DATEDIFF(day,[date],[dateFixed]) ELSE DATEDIFF(day,[date],GETDATE()) END AS daysWaited,[id],[date],[lot],[stock],[vin],[makeModel],[issue],[approved],[dateFixed],[comments] FROM [repairApprover] WHERE [lot] = IsNull(@lot,lot)" 
     DeleteCommand="DELETE FROM [repairApprover] WHERE id = @id" 
     CancelSelectOnNullParameter="false" 
     > 

正如你可以在SelectCommand中的最後見我有WHERE [lot] = IsNull(@lot,lot)它引用此ControlParameter:

<SelectParameters> 
     <asp:ControlParameter ControlID="lotBox" Name="lot" PropertyName="Text" Type="String" /> 
    </SelectParameters> 

的控制參數得到值從DropDownList過濾很多:

<asp:DropDownList ID="lotBox" runat="server" ClientIDMode="Static" AutoPostBack="True"> 
    <asp:ListItem Value="">All</asp:ListItem> 
    <asp:ListItem Value="1">1</asp:ListItem> 
    <asp:ListItem Value="2">2</asp:ListItem> 
    <asp:ListItem Value="3">3</asp:ListItem> 
    <asp:ListItem Value="5">5</asp:ListItem> 
    <asp:ListItem Value="6">6</asp:ListItem> 
    <asp:ListItem Value="40">40</asp:ListItem> 
</asp:DropDownList> 

這一切都很好。但是,我不知道如何添加另一個DropDownList,它允許用戶在名爲「dateFixed」的列中過濾/在具有NULL值的行中。

基本上我需要能夠做類似的東西上面,將確定以下三個字符串添加到我的SelectCommand

AND [dateFixed] IS NOT NULL 
AND [dateFixed] IS NULL 

和第三個字符串將是空的。

回答

0

您需要動態構建您的SELECT。類似這樣的:

var sel = "Select ......FROM ...... "; 
bool hasWhere; 
if (combo1.SelectedIndex > -1) 
{ 
    if (!hasWhere) 
    { 
     hasWhere = true; 
     sel += " where "; 
    } 
    sel += "<your where condition 1>"; 
} 

if (combo2.SelectedIndex > -1) 
{ 
    if (!hasWhere) 
    { 
     hasWhere = true; 
     sel += " where "; 
    } 
    else 
     sel += " and "; 
    sel += "<your where condition 1>"; 
} 

if (combo3.SelectedIndex > -1) 
{ 
    if (!hasWhere) 
    { 
     hasWhere = true; 
     sel += " where "; 
    } 
    else 
     sel += " and "; 
    sel += "<your where condition 2>"; 
} 

希望這是你所問的。請注意這個粗糙的例子。您可能需要使用StringBuilder

0

@dataFixed放慢參數

AND ([dateFixed] = @dateFixed OR @dateFixed IS NULL)