• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

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?
0
KoldFuzun
Asked:
KoldFuzun
  • 7
  • 5
  • 4
2 Solutions
 
jitgangulyCommented:
Fire it in ItemData_Bound

This is some example code I use ( c# though)

public void ItemData_Bound (object sender,DataGridItemEventArgs e)
{
      if (e.Item.Cells[0].Text==" ")
      {
            e.Item.Cells[0].Text="Untitled";
      }
            
}
0
 
b1xml2Commented:
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

0
 
b1xml2Commented:
assuming the data source is either a datatable or a dataview.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
KoldFuzunAuthor Commented:
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
0
 
b1xml2Commented:
typo
===
<ItemTemplate><%# IIf(DirectCast(Container.DataItem,DataViewRow).Row.IsNull("data"),"Untitled",DirectCast(Container.DataItem,DataViewRow)("data").ToString) %></ItemTemplate>
0
 
KoldFuzunAuthor Commented:
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>
0
 
b1xml2Commented:
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
0
 
b1xml2Commented:
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>
0
 
KoldFuzunAuthor Commented:
System.Data.DataRowView ?

0
 
jitgangulyCommented:
Mine was much simple
0
 
b1xml2Commented:
1. sheesh, replace all references to DataViewRow to DataRowView (my sincerest apologies)
2.
<%@ Import Namespace="System.Data"%>


<Columns>

<asp:TemplateColumn>
<HeaderTemplate>docTitle</HeaderTemplate>
<ItemTemplate><%# IIf(DirectCast(Container.DataItem,DataRowView).Row.IsNull("DocTitle"),"Untitled",DirectCast(Container.DataItem,DataRowView)("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>
0
 
KoldFuzunAuthor Commented:
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.
0
 
b1xml2Commented:
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>
0
 
jitgangulyCommented:
>>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 :-)
0
 
KoldFuzunAuthor Commented:
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?
0
 
jitgangulyCommented:
Ok
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now