C# 当列具有空值时,如何删除Gridview中的行?
在我的C# 当列具有空值时,如何删除Gridview中的行?,c#,asp.net,C#,Asp.net,在我的GridView中,我有几行4列。有些行在1列中有空值。 例如: 因此,在上面的示例中,我想删除或隐藏第2行和第3行 这是我目前掌握的代码: Page.aspx <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
GridView
中,我有几行4列。有些行在1列中有空值。
例如:
因此,在上面的示例中,我想删除或隐藏第2行和第3行
这是我目前掌握的代码:
Page.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>
但这似乎不起作用
谢谢为什么不更改您的查询
SELECT
DisplayName 'Display Name',
Replace(PrimaryEmailAddress,'SMTP:', ' ') 'Email Address',
Replace(Licenses,'text:', ' ') 'License Type',
LastPasswordChangeTimestamp 'Last Password Reset' FROM table
到
选择不想显示的行没有任何好处
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:'
选择
显示名称为“显示名称”,
PrimaryEmailAddress作为“电子邮件地址”,
作为“许可证类型”的许可证,
LastPasswordChangeTimestamp作为“上次密码重置”
从表名
哪里
主电子邮件地址“SMTP:”
和许可证的文本:'
对不起,您是否在代码末尾填写数据表?在对数据表进行任何更改之前,可能需要先填充数据表。
SELECT
DisplayName 'Display Name',
PrimaryEmailAddress 'Email Address',
Licenses 'License Type',
LastPasswordChangeTimestamp 'Last Password Reset'
FROM
table
WHERE
PrimaryEmailAddress != 'SMTP:'
and Licenses != 'text:'
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:'