Link to home
Start Free TrialLog in
Avatar of KoldFuzun
KoldFuzun

asked on

ASP.NET Datagrid, populating empty rows from recordset

ASP.NET newbie here. I am using a text editor to develop my application as I havent had the time to learn VS.NET yet, so this may give you an idea of how new I am.

Onto the problem at hand:

I am returning a recordset to a datagrid, but some of the rows have null values. I want to use a conditional so that if the record is empty I can return a string, namely "Untitled". Do I have to custom build my datagrid if I want to do this?
SOLUTION
Avatar of jitganguly
jitganguly

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
in this case, you have two options:
=======================
<asp:DataGrid Id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>Use Id</HeaderTemplate>
<ItemTemplate><%# IIf(DirectCast(Container.DataItem,DataRowView).Row.IsNull("data"),"Untitled",DirectCast(Container.DataItem,DataRowView)("data").ToString) %></ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

or

<asp:DataGrid Id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>Use Id</HeaderTemplate>
<ItemTemplate><%# ProcessItem(Container.DataItem,"data") %></ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

...
Public Function ProcessItem(ByVal dataItem As Object, ByVal column As String) As String
      Dim viewRow As DataViewRow = DirectCast(dataItem,DataViewRow)
      Dim value As String = "Untitled"
      If Not viewRow.Row.IsNull(column) Then
            value = viewRow(column).ToString()
      End If
      
      Return value
End Function

assuming the data source is either a datatable or a dataview.
Avatar of KoldFuzun
KoldFuzun

ASKER

Thanks guys. This is actually a recordset from an  Index Server query. I assume it is not the same, since after implementing b1xml2's code I get

Type 'DataRowView' is not defined

*extreme newbie* my apologies and thanks for the help
typo
===
<ItemTemplate><%# IIf(DirectCast(Container.DataItem,DataViewRow).Row.IsNull("data"),"Untitled",DirectCast(Container.DataItem,DataViewRow)("data").ToString) %></ItemTemplate>
Here's where i frustrate you :)

Same error. here is the code I am using

<Columns>

<asp:TemplateColumn>
<HeaderTemplate>docTitle</HeaderTemplate>
<ItemTemplate><%# IIf(DirectCast(Container.DataItem,DataViewRow).Row.IsNull("DocTitle"),"Untitled",DirectCast(Container.DataItem,DataViewRow)("DocTitle").ToString) %></ItemTemplate>
</asp:TemplateColumn>

<asp:HyperLinkColumn HeaderText="Title" DataNavigateUrlField="VPATH" DataNavigateUrlFormatString="" DataTextField="DocTitle" Target="_blank" ItemStyle-Font-Size="11px" />

<asp:BoundColumn DataField="create" HeaderText="Created" ItemStyle-Font-Size="11px"></asp:BoundColumn>
<asp:BoundColumn DataField="write" HeaderText="Modified" ItemStyle-Font-Size="11px"></asp:BoundColumn>
<asp:BoundColumn DataField="characterization" HeaderText="Abstract" ItemStyle-Font-Size="11px"></asp:BoundColumn>

</Columns>
Whats the data source type? a datatable, or data reader??

do us a favour:
<asp:TemplateColumn>
<HeaderTemplate>docTitle</HeaderTemplate>
<ItemTemplate><%# Container.DataItem.GetType() %></ItemTemplate>
</asp:TemplateColumn>

This will tell me the data type of the container
The ugly way is this:
<asp:TemplateColumn>
<HeaderTemplate>docTitle</HeaderTemplate>
<ItemTemplate><%# IIf(DataBinder.Eval(Container.DataItem,"DocTitle").ToString() = "" ,"Untitled",DataBinder.Eval(Container.DataItem,"DocTitle").ToString()) %></ItemTemplate>
</asp:TemplateColumn>
System.Data.DataRowView ?

Mine was much simple
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent, this works very well, thank you. Oddly enough I am running into rows which still appear blank though. Any ideas?

jitganguly, i would not disagree with you, however I am such a newbie in .NET I was not able to determine how to implement your solution.
in that case, there are empty string values...

<ItemTemplate><%# IIf(DirectCast(Container.DataItem,DataRowView).Row.IsNull("DocTitle") OrElse DirectCast(Container.DataItem,DataRowView)("DocTitle").ToString().Trim() = "","Untitled",DirectCast(Container.DataItem,DataRowView)("DocTitle").ToString) %></ItemTemplate>
>>I was not able to determine how to implement your solution.

Item data bound fires just before data bound so thats the last chance you could tweak with your data. If you had used VS.net, it would have been easier for you and accpeted my comment :-)
Sweet, thanks b1xml2, that worked perfectly.

jitanguly, i realize that if I had more knowledge or a better understanding of the VS.NET IDE i would have known how to implement your solution, so it is primarily my problem for not being able to implement your solution.

If if it acceptable to you both I will split the points and give jitanguly 150 points for being the first to answer and providing an assisted solution that others (who arent newbs like me!) will be able to use. Is this ok?