在GridView中不显示NULL值行

时间:2016-12-02 10:54:41

标签: c# sql asp.net data-binding sqldataadapter

我有一个带有ContactID的事件表作为外键。当我从联系表中删除联系人时,我将事件表中的ContactID设置为NULL。

在Event GridView中,我将显示事件信息以及联系人表中的ContactName和Number。

GridView的:

<asp:GridView ID="GridView4" runat="server" ShowHeaderWhenEmpty="true" CssClass="table table-striped" style="font-family:sans-serif; font-size:medium" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
    <asp:BoundField DataField="StartDate" HeaderText="Date" />
    <asp:HyperLinkField DataTextField="Title" DataNavigateUrlFields="ID" DataNavigateUrlFormatString="~/Views/Portal/ViewEvent.aspx?evtID={0}" HeaderText="Event" ItemStyle-Width="150px" ></asp:HyperLinkField>
    <asp:BoundField DataField="Name" HeaderText="Contact Name" />
    <asp:BoundField DataField="Cell" HeaderText="Contact #" />
    <asp:BoundField DataField="EventType" HeaderText="Type" />
    <asp:BoundField DataField="Confirmed" HeaderText="Status" />
    <asp:BoundField DataField="Comments" HeaderText="Comments" />
</Columns>

查询:

SELECT Event.ID, Event.StartDate, Event.Title,Contact.Name,Contact.Cell,Event.EventType, Event.Confirmed,Event.Comments 
FROM Event 
LEFT JOIN Contact ON Event.ContactID=Contact.ID 

aspx.cs:

string myConnection = dbController.connectionString;
SqlConnection conn = new SqlConnection(myConnection);
string query = query = "SELECT Event.ID, Event.StartDate, Event.Title, Contact.Name, Contact.Cell, Event.EventType, Event.Confirmed, Event.Comments FROM Event LEFT JOIN Contact ON Event.ContactID=Contact.ID";

GridView4.DataSource = null;
GridView4.DataBind();

DataTable dt = new DataTable();
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(query, conn);
da.Fill(dt);
conn.Close();

if (dt.Rows.Count > 0)
{
     GridView4.DataSource = dt;
     GridView4.DataBind();
}

这些行在Sql Server Management Studio中显示得很好。但是在Web界面上,ContactID为NULL的行不会显示。

请指引我朝正确的方向前进。

0 个答案:

没有答案