In a Web application, a Detailsview sometimes shows empty rows because no data has been entered for some data fields, not because there is some kind of SQL error. Is there a way to hide these rows? I've seen something similar for VB.NET in these forums.
Can the Detailsview be customized this way?
You could try mofidying your select statement on your detailsview's datasource so that it doesn't include records where all of the fields displayed by the detailsview are null.
I love to display the non-secure items...
Charlie Asbornsen
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
In a Web application, a Detailsview sometimes shows empty rows because no data has been entered for some data fields, not because there is some kind of SQL error. Is there a way to hide these rows? I've seen something similar for VB.NET in these forums.
Can the Detailsview be customized this way?
... WHERE ([AlbumID] = @AlbumID) AND (PhotoID = @PhotoID) AND ([PhotoID] IS NOT NULL) AND ([AlbumID] IS NOT NULL) AND ([Caption] IS NOT NULL) AND ([BytesThumb] IS NOT NULL)...
etc.
Do that for each field and if any of them are not null you'll get a record. This is OK if all these fields are nullable. It may be that things like imgSize are defaulting to 0 or you may have an empty string for something like the imgPurchaseURL instead,
so you have to make sure they aren't equal to the default value instead of NULL.
But I think naturehermit may have an easier solution.
I love to display the non-secure items...
Charlie Asbornsen
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
Thank you for your suggestion, Naturehermit. I've seen the EmptyDataTemplate, but it's not the solution I need. That will customize empty rows, but they will still be visible. What I need to do is change visibility to false for any rows that are empty. The
datatypes are either strings (nvarchar) or integers (Int). This is the closest solution I've found.
However, the only row it actually hides is one with a boolean value (a checkbox). For all the other empty cells, which are either strings or integers, the rows are still visible.
Thank you again, Charles. I don't think the SQL statement will achieve the result I need. There is no problem getting records. The problem is the empty column cells. In Detailsview, as you know, each row represents a table column and its data. So I need
a way to hide that row and show in the Detailsview only those rows with data to show. Is this even possible?
Naturehermit made a suggestion, and my response is copied below. Perhaps you know how I can modify the line ((string.IsNullOrEmpty(Row.Cells[1].Text) | (Row.Cells[1].Text == "0"))) so that it applies not just to text, but to integers as well. This code
seems to hold the most promise. It's just that I don't know how to modify it so that it hides rows with numbers and strings (text). I would expect it already to hide rows with text, but it doesn't. It hides only one row, which contains a boolean value for
a form checkbox.
What I need to do is change visibility to false for any rows that are empty. The datatypes are either strings (nvarchar) or integers (Int). This is the closest solution I've found.
However, the only row it actually hides is one with a boolean value (a checkbox). For all the other empty cells, which are either strings or integers, the rows are still visible.
I love to display the non-secure items...
Charlie Asbornsen
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
seanbud
0 Points
1 Post
Detailsview -- hide empty rows using C#
Sep 28, 2007 02:00 PM|LINK
In a Web application, a Detailsview sometimes shows empty rows because no data has been entered for some data fields, not because there is some kind of SQL error. Is there a way to hide these rows? I've seen something similar for VB.NET in these forums. Can the Detailsview be customized this way?
Thank you for any suggestions you may offer.
Charles Asbo...
Contributor
5512 Points
1186 Posts
Re: Detailsview -- hide empty rows using C#
Sep 28, 2007 02:58 PM|LINK
You could try mofidying your select statement on your detailsview's datasource so that it doesn't include records where all of the fields displayed by the detailsview are null.
Charlie Asbornsen
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
naturehermit
Star
14610 Points
3046 Posts
Re: Detailsview -- hide empty rows using C#
Sep 28, 2007 03:16 PM|LINK
Try using EmptyDataTemplate property of detailsview http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.emptydatatemplate.aspx (perhaps using
if (row.RowType != DataControlRowType.EmptyDataRow)
more consult below
http://www.koders.com/csharp/fid0EE90D526C0D7C735DEDD4BEF59E62849EF1CCE8.aspx?s=mdef%3Ainsert
sbudlong
Member
65 Points
124 Posts
Re: Detailsview -- hide empty rows using C#
Sep 28, 2007 03:30 PM|LINK
Thank you, Charles. This is the SELECT statement for the Detailsview.
SelectCommand="SELECT [PhotoID], [AlbumID], [Caption], [BytesThumb],
[imgFileName], [imgDescription], [imgPrice], [imgSize], [imgResolution],
[imgPurchaseURL], [imgUser], [imgPurchaseDate], [imgContractLength],
[imgExpirationDate], [imgPlannedUse], [imgTotalImpressions], [imgPath],
[imgWidth], [imgHeight], [imgSortOrder], [imgInclude] FROM [Photos] WHERE
([AlbumID] = @AlbumID) AND PhotoID = @PhotoID">
What would I add to it so that the fields that are empty don't show up in the Detailsview?
Charles Asbo...
Contributor
5512 Points
1186 Posts
Re: Detailsview -- hide empty rows using C#
Sep 28, 2007 03:45 PM|LINK
... WHERE ([AlbumID] = @AlbumID) AND (PhotoID = @PhotoID) AND ([PhotoID] IS NOT NULL) AND ([AlbumID] IS NOT NULL) AND ([Caption] IS NOT NULL) AND ([BytesThumb] IS NOT NULL)...
etc.
Do that for each field and if any of them are not null you'll get a record. This is OK if all these fields are nullable. It may be that things like imgSize are defaulting to 0 or you may have an empty string for something like the imgPurchaseURL instead, so you have to make sure they aren't equal to the default value instead of NULL.
But I think naturehermit may have an easier solution.
Charlie Asbornsen
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
sbudlong
Member
65 Points
124 Posts
Re: Detailsview -- hide empty rows using C#
Sep 28, 2007 03:48 PM|LINK
Thank you for your suggestion, Naturehermit. I've seen the EmptyDataTemplate, but it's not the solution I need. That will customize empty rows, but they will still be visible. What I need to do is change visibility to false for any rows that are empty. The datatypes are either strings (nvarchar) or integers (Int). This is the closest solution I've found.
protected void Page_Load(object sender, EventArgs e)
{
foreach (DetailsViewRow Row in Products.Rows)
{
if ((string.IsNullOrEmpty(Row.Cells[1].Text) | (Row.Cells[1].Text == "0")))
{
Row.Visible = false;
}
}
}
However, the only row it actually hides is one with a boolean value (a checkbox). For all the other empty cells, which are either strings or integers, the rows are still visible.
sbudlong
Member
65 Points
124 Posts
Re: Detailsview -- hide empty rows using C#
Sep 28, 2007 05:58 PM|LINK
Thank you again, Charles. I don't think the SQL statement will achieve the result I need. There is no problem getting records. The problem is the empty column cells. In Detailsview, as you know, each row represents a table column and its data. So I need a way to hide that row and show in the Detailsview only those rows with data to show. Is this even possible?
Naturehermit made a suggestion, and my response is copied below. Perhaps you know how I can modify the line ((string.IsNullOrEmpty(Row.Cells[1].Text) | (Row.Cells[1].Text == "0"))) so that it applies not just to text, but to integers as well. This code seems to hold the most promise. It's just that I don't know how to modify it so that it hides rows with numbers and strings (text). I would expect it already to hide rows with text, but it doesn't. It hides only one row, which contains a boolean value for a form checkbox.
What I need to do is change visibility to false for any rows that are empty. The datatypes are either strings (nvarchar) or integers (Int). This is the closest solution I've found.
protected void Page_Load(object sender, EventArgs e)
{
foreach (DetailsViewRow Row in Products.Rows)
{
if ((string.IsNullOrEmpty(Row.Cells[1].Text) | (Row.Cells[1].Text == "0")))
{
Row.Visible = false;
}
}
}
However, the only row it actually hides is one with a boolean value (a checkbox). For all the other empty cells, which are either strings or integers, the rows are still visible.
Charles Asbo...
Contributor
5512 Points
1186 Posts
Re: Detailsview -- hide empty rows using C#
Sep 28, 2007 06:01 PM|LINK
Try an eval?
Visible='<%# !Convert.ToBoolean(Eval("ColumnName")=Null) %>'
Charlie Asbornsen
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
sbudlong
Member
65 Points
124 Posts
Re: Detailsview -- hide empty rows using C#
Sep 28, 2007 06:06 PM|LINK
I'll give it a try. Do I put the Eval at the end of this
foreach (DetailsViewRow Row in Products.Rows)
{
if ((string.IsNullOrEmpty(Row.Cells[1].Text) | (Row.Cells[1].Text == "0")))
{
Row.Visible = false;
}
}
so that it reads Row.Visible = Visible='<%# !Convert.ToBoolean(Eval("ColumnName")=Null) %>' ;
sbudlong
Member
65 Points
124 Posts
Re: Detailsview -- hide empty rows using C#
Sep 28, 2007 06:09 PM|LINK
Here's another idea. Is it possible to use conditional logic to hide rows that have an empty data cell?