image as header for each column in grid view - c#

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>

Related

Is it possible to add image buttons in DataGridView?

I am now developing windows application using c#.net.
now i want to add four image buttons in datagridview cell like following picture.
Is it possible to do it? is there any control to do as the following picture.
Best Rgds,
DF
You need to add a template field as a column in your grid view. Then in the item template you can put asp image buttons. For example:
<asp:GridView runat="server" id="gdv_gridviewname" >
<Columns>
<asp:TemplateField HeaderText="Image Buttons">
<ItemTemplate>
<asp:ImageButton runat="server" ID="imgBtnSomething" ImageURL="Path to your image" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

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>

store image in database and retrieve in datagridview

I am trying out new things myself in .net.I want to store image url in database and retrieve it dynamically in gridview.I have tried using blob.But i do not want to store image in database but just the urls.Some solutions that i have found that we can use template field in gridview and somehow use bind function.Also I can store all images in project folder and retrieve the images from folder.On button click i want to display images in gridview.But still i am not able to proceed forward.This is my code so far....
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Model_Id" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Model_Id" HeaderText="Model_Id"
InsertVisible="False" ReadOnly="True" SortExpression="Model_Id" />
<asp:BoundField DataField="Model_Name" HeaderText="Model_Name"
SortExpression="Model_Name" />
<asp:BoundField DataField="Max_seats" HeaderText="Max_seats"
SortExpression="Max_seats" />
<asp:BoundField DataField="Image" HeaderText="Image" SortExpression="Image" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Model]"></asp:SqlDataSource>
</div>
inside your GridView add a template field i.e.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Model_Id" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="Photo">
<ItemTemplate>
<img src='<%# Eval("Image") %>' />
</ItemTemplate>
</asp:TemplateField>
//other bound columns
</Columns>
</asp:GridView>
where Image is the column inside your table [Model], containing Image URL.
if Images don't show, check the relative url structure, you might need to prefix/adjust it like this:
/images/image1.jpg to ../images/image1.jpg (depends on relative location of your folder containing images)
it should work for you.
store the images in a folder in solution explorer of your project, if you want to save the link to the database then set the data type of the coloumn to varchar(MAX) and store the link of the image to database table like below.
1.If your folder name is Image, then store the url as ../Image/picture.jpg
2.go to quick task menu of the gridview and click edit fields and from the AVAILABLE FIELDS list choose "ImageField" and click add, the image field is then added to SELECTED FIELDS
3.move the "ImageField" in SELECTED FIELDS up or down to place it between other columns
4.Click the "ImageField" and ImageField properties will be displayed on the right side, GOTO
DATA and in "DataImageUrlField" select the Database table column name from where you want to retrieve the image .
Thats Should Work, ImageField will automatically generate the HTML code for your images to be displayed.

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

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>

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