In my GridView
I have a few rows with 4 columns. Some of the rows has a null value in 1 column. Example:
-----------------------------------------------------
| Column 1 | Column 2 | Column 3 | Column 4 |
-----------------------------------------------------
| text | Text | Text | Text |
-----------------------------------------------------
| text | | Text | Text |
-----------------------------------------------------
| text | Text | Text | |
-----------------------------------------------------
So in the above example I would like to delete or hide row 2 and row 3.
This is the code I have at the moment:
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>
Page.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;
}
But this does not seem to be working.
Thanks
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;
}
Note:- If you want to use same Query...the Prefer this else...
Else use this query....Not Allowing the null value from Database...
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:'
Why don't you change your query
SELECT
DisplayName 'Display Name',
Replace(PrimaryEmailAddress,'SMTP:', ' ') 'Email Address',
Replace(Licenses,'text:', ' ') 'License Type',
LastPasswordChangeTimestamp 'Last Password Reset' FROM table
to
SELECT
DisplayName 'Display Name',
PrimaryEmailAddress 'Email Address',
Licenses 'License Type',
LastPasswordChangeTimestamp 'Last Password Reset'
FROM
table
WHERE
PrimaryEmailAddress != 'SMTP:'
and Licenses != 'text:'
There is no benefit to select the rows which you don't want to display
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.