c# - 当列具有空值时,如何在 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;
}
但这似乎不起作用。
谢谢
解决方案
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:'
推荐阅读
- excel - 使用宏对文件夹中的多个文件应用冻结到excel文件
- c# - MassTransit 的 Kafka Producer - IBusInstance 尚未注册
- asp.net - Blazor WASM 授权不适用于 AAD 角色
- python - 使用 py 文件导入压缩库失败
- c++ - 如何让 operator<< 用于特定模板实例化 std::tuple
? - android - Flutter 应用程序无法在发布模式下运行
- java - 错误 - :不兼容的类型:从 int 到 short 的可能有损转换
- ios - 查找字符串的宽度(快速)
- powershell - Get-MsolDevice RegisteredOwners 导出到 CSV 失败
- javascript - 在浏览器扩展中实现 web worker - Uncaught ReferenceError: importScripts is not defined