首页 > 解决方案 > 当列具有空值时,如何在 Gridview 中删除一行?

问题描述

在我的GridView我有几行有 4 列。某些行在 1 列中有空值。例子:

-----------------------------------------------------
| Column 1   | Column 2   | Column 3   | Column 4   |
-----------------------------------------------------
| text       | Text       | Text       | Text       |
-----------------------------------------------------
| text       |            | Text       | Text       |
-----------------------------------------------------
| text       | Text       | Text       |            |
-----------------------------------------------------

所以在上面的例子中,我想删除或隐藏第 2 行和第 3 行。

这是我目前的代码:

页面.aspx

<asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" 
   BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
  <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
  <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
  <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
  <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
  <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
  <SortedAscendingCellStyle BackColor="#FFF1D4" />
  <SortedAscendingHeaderStyle BackColor="#B95C30" />
  <SortedDescendingCellStyle BackColor="#F1E5CE" />
  <SortedDescendingHeaderStyle BackColor="#93451F" />
 </asp:GridView>

页面.aspx.cs

DataTable GetData()
    {
        DataTable dt = new DataTable();
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["OfficeConnection"].ConnectionString))
        {
            con.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT DisplayName 'Display Name', Replace(PrimaryEmailAddress,'SMTP:', ' ') 'Email Address', Replace(Licenses,'text:', ' ') 'License Type', LastPasswordChangeTimestamp 'Last Password Reset' FROM table ", con))
            {
                foreach (DataRow row in dt.Rows)
                {
                    bool IsEmpty = false;
                    foreach (object obj in row.ItemArray)
                    {
                        if (String.IsNullOrEmpty(obj.ToString()))
                        {
                            IsEmpty = true;
                        }
                        else
                        {
                            IsEmpty = false;
                        }
                    }
                    if (IsEmpty)
                    {
                        dt.Rows.Remove(row);
                    }
                }
                dt.AcceptChanges();
                SqlDataAdapter adpt = new SqlDataAdapter(cmd);
                adpt.Fill(dt);
            }

        }
        return dt;
    }

但这似乎不起作用。

谢谢

标签: c#asp.net

解决方案


public DataTable GetData()
{
    DataTable dt = new DataTable();

    string constr = ConfigurationManager.ConnectionStrings["OfficeConnection"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT DisplayName 'Display Name', Replace(PrimaryEmailAddress,'SMTP:', ' ') 'Email Address', Replace(Licenses,'text:', ' ') 'License Type', LastPasswordChangeTimestamp 'Last Password Reset' FROM table"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (dt = new DataTable())
                {
                    sda.Fill(dt);
                    for (int i = dt.Rows.Count - 1; i >= 0; i--)
                    {

                        if (dt.Rows[i]["mycolumn"].ToString() == "")
                            dt.Rows[i].Delete();
                    }
                    //You have to specify All the column ....which you want to check  

                    dt.AcceptChanges();

                }
            }
        }
    }
    return dt;
}

注意:-如果您想使用相同的查询...首选此其他...

否则使用此查询....不允许来自数据库的空值...

  SELECT 
      DisplayName AS  'Display Name', 
      PrimaryEmailAddress  AS 'Email Address', 
      Licenses AS  'License Type', 
      LastPasswordChangeTimestamp AS 'Last Password Reset' 
    FROM TableName 
    WHERE
    PrimaryEmailAddress <> 'SMTP:'
    and Licenses <> 'text:'

推荐阅读