GridView - display '0' for all headers when empty data set - c#

I'm using a GridView to display some ifnromation in ASP.NET. I need it to simply display 0 in the gridview fields (there's only ever one row returned) if the dataset is empty, plus of course display the headers. How do I do this?

Yes bind a dummy dataset with one row, with all zeros as fields, to the grid with zeros as the field labels. That would be the best way.

Bind your grid and add the EmptyDataTemplate like so:
<asp:GridView ID="gridview1" runat="server" ShowFooter="true">
<Columns>
<asp:BoundField DataField="id" HeaderText="ID field" />
</Columns>
<EmptyDataTemplate>put whatever you want in here</EmptyDataTemplate>
</asp:GridView>

Related

Values GridView in cells

I am trying to get values of string that I keep inside GridView.
When I use regular BoundFields, things work get. I get whatever is needed with:
string my_value = myGrid.Rows [rowIndex].Cells[1].Text;
However, one grid needs to have hyperlinked entries in one of the columns. I did:
<asp:BoundField DataField="domainName"
HeaderText="Domain"
SortExpression="domainName"
HtmlEncode="false"
DataFormatString="<a href=DomainConfiguration.aspx?suffix={0}>{0}</a>"
My ASPX page shows the correctly formed hyperlinks. However, if I retrieve Text for the cell, it returns "<\a href=DomainConfiguration.aspx?suffix=example.com>example.com</a>" [without the two extra slashes], instead of "example.com"
What do I need to do to get GridView working the way I want? [Yes, I would rather use GridView and not another control.]
Thank you.
Instead of asp:BoundField to show hype link it is better use asp:HyperLinkField like:
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="domainName" HeaderText="domainName" DataNavigateUrlFormatString="DomainConfiguration.aspx?suffix={0}" DataTextField="domainName" />
</Columns>
and to get string value of that column you should try this:
string my_value= ((HyperLink)myGrid.Rows[rowIndex].Cells[1].Controls[0]).Text;
instead your asp:Bounfield use
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="editLink" runat="server" onclick='<%#Eval("EditLink") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

How can i make my grid view show multiple values (the number of values varies) in a single column cell?

i have to display data like this in grid view:
the problem is that:
for each unique set of manufacturer , model and type, the number of values associated with watts column changes ... that is, there is no fixed numbers of values in this single column. how can i display such a format in my grid view? Kindly help ...
Could you just place another (nested) gridView in that column? You'd have to use a template column in your parent grid, but then for each row, the number of records in that column could be different.
<asp:GridView ID="parentGridView" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Manufacturer" />
<asp:BoundField DataField="ModelNo" HeaderText="Model No" />
<asp:BoundField DataField="Type" />
<asp:TemplateField HeaderText="Watts">
<ItemTemplate>
<asp:GridView ID="nestedWattsView" runat="server" AutoGenerateColumns="true" ShowHeader="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

image as header for each column in grid view

I have a data table which is displayed as a gridview in ASP.NET. It has a column: temperature . I want to put picture of thermometer instead of writing temperature. I want this picture only for this column and not for the whole grid view. How do i do that?
This can be done easily in a GridView by using the HeaderImageUrl property on the column.
For Example:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField HeaderImageUrl="icon.png" />
<asp:TemplateField HeaderImageUrl="icon.png" />
</Columns>
</asp:GridView>

Sorting Specific Column in a GridView after a DataBound

I have given an access to stored procedure, which i'm not able to edit.
This Stored Procedure returns a Table with 2 Column, what I did is set a GridView's DataSource using SQLDataSource in this stored procedure. but I want this GridView to Sort an specific column to descending whenever this GridView Loads.
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource2" ForeColor="Black"
Width="58%" Height="125px" AllowPaging="True" AllowSorting="True"
PageSize="5" >
<Columns>
<asp:BoundField DataField="DateOccur" HeaderText="Login Date"
SortExpression="DateOccur" />
<asp:BoundField DataField="TotalMinutesPlayed" HeaderText="Total Minutes"
SortExpression="TotalMinutesPlayed" />
</Columns>
<AlternatingRowStyle BackColor="#EFEFEF" />
</asp:GridView>
Thanks!~
You could also use the Gridview.Sort method
See Here.
If you can't sort it by editing the SQL itself, you can sort it programmatically by binding a DataView object to the datasource, specifying a Sort expression for the DataView, and then binding the DataGrid to the DataView object.
For a walkthrough of some example code, see here.
I was using a slightly modified version of the example code I found on MSDN.
Gridview Sort Example on MSDN
By combining the example I found with the SortExpression property I can sort on any column even using proprietary sorts my specification called for.

Gridview Column Removing

I have a web application that I am working on(ASP.NET 2.0 C#). In it I have a GridView whose data source is an Oracle database. I get the data into the gridview in my codebehind, and don't set the datasource directly.
I wanted to create a hyperlink field (NAME) that takes me to a details page about a specific record. What ends up happening is that it creates the Hyperlink field as well as the regular field that it gets from the datasource, which I don't want. If I remove the field from my SELECT statement, it gives an error saying something like: "NAME" not found in datasource.
How can I eliminate the regular field, and get a hyperlink field instead? I have tried Gridview.Columns.Remove(columnlocation), but that won't work coz the columns don't exist there originally.
Please Help
Thank you.
Instead of removing columns, disable AutoGenerateColumns property of the gridview and set your hyperlink column manually like that :
<asp:gridview id="GridView1"
autogeneratecolumns="false"
runat="server">
<asp:HyperLinkField DataNavigateUrlFields="UserID"
DataNavigateUrlFormatString="UserDetails.aspx?id={0}"
DataTextField="UserName" />
</asp:gridview>
Sounds like you have AutoGenerateColumns property set to TRUE on your grid. This means that a column is generated for EVERY column you return from your query.
If you want to have some custom columns, you should set AutoGenerateColumns="false" and add all the columns to the GirdView as asp:BoundField and your Hyperlink column as asp:TemplateField
Let me know if I'm off the mark with this
here's some code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Name" />
<asp:BoundField DataField="Whatever" />
<asp:TemplateField>
<ItemTemplate>
<a href='<%# Eval("UserId", "URL_TO_USER?userId={0}") %>'>Details</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Categories