I have a gridview which displays rows and columns all linked to an sql statement... and all works as normal.
I want to include a new column, that displays an image depending on what the value of the column is. If the column has a value - it will display an image. If the column value is null no image whill be displayed.
My sql is something like:
SELECT c.call_id, title, a.call_id as b_attach
FROM calls c
LEFT JOIN attachments a ON c.call_id = a.call_id
GROUP BY c.call_id,title,description, a.call_id
What's returns from this sql is:
Call_id | title | b_attach
1235 | title goes here | 1235
1382 | another title |NULL
So if there's something in b_attach - diplay image in gridview column, else display nothing in gridview column
My Gridview:
<asp:HyperLinkField SortExpression="call_id" HeaderText="Call id" DataTextField="call_id"
DataNavigateUrlFields="call_id" DataNavigateUrlFormatString="showcall.aspx?id={0}" />
<asp:HyperLinkField SortExpression="Title" HeaderText="Title" DataTextField="title"
DataNavigateUrlFields="call_id" DataNavigateUrlFormatString="showcall.aspx?id={0}" />
</Columns>
Any ideas on how to do this?
You can use a TemplateField, and, inside it something like this:
<asp:Image runat="server" id="myImg" ImageUrl='<%# GetImage(DataBinder.Eval(Container.DataItem, "b_attach")) >%' visible='<%# null != DataBinder.Eval(Container.DataItem, "b_attach") %> />
Related
Ok here is another thing is currently stumping me and I can not seem to find an answer that fits my need. On an aspx page I have a Grid View that gets it data from a SQL query and populates the Grid View, which is working fine. On this grid view though the first column (Column 0) has a line number that was returned from the query. There could one line or 10 or more lines in the grid view data that is returned.
What I am trying to do is add a hyperlink/LinkButton/Button (Not sure which) to the grid view in a column before the line number. I currently have a TemplateField with an asp link button which i can get to show up.
What I can't seem to figure out, and I have been looking for a couple of days now, is how to pull the cell with the line number to pass to my code behind (VB.NET) to have it run a secondary query that will populate another grid view with the line item details.
Here is how I currently have the gridview set up:
<asp:GridView ID="gvDetailSecondLevel" runat="server" AutoGenerateColumns="false" Width="1010px" CellPadding="4" ForeColor="#333333" GridLines="Horizontal" CssClass="SecLvlDtl" OnRowCommand="gvDetailSecondLevel_RowCommand">
<Columns>
<asp:TemplateField ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:LinkButton ID="btnLineDetail" runat="server" CssClass="dtlButtons" CommandName="Edit" Text="Line Detail" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Line #" HeaderText="Line #" />
<asp:BoundField DataField="Part Desc" HeaderText="Part Description" />
<asp:BoundField DataField="PCS" HeaderText="PCS" />
<asp:BoundField DataField="WT" HeaderText="WT" />
<asp:BoundField DataField="SF" HeaderText="SF" />
</Columns>
The rest of the grid view is formatting so I did not list it for brevity.
The current code behind (Which I am sure is wrong because it throws an error) is as follows:
Protected Sub gvDetailSecondLevel_RowCommand(sender As Object, e As GridViewCommandEventArgs)
'Setup Variables
Dim lineNumber As String = gvDetailSecondLevel.Rows(sender.RowIndex).Cells(0).Text
'Make controls visibile
btnClear.Visible = True
SOThirdLevel.Visible = True
'Retrieve Thrid Level SQL Data
bindThirdLevel(lineNumber)
End Sub
The error that gets thrown says "Public Member 'RowIndex' on type 'GridView' not found"
I should also mention that the second grid view is in a div tag that is on the same page. I am not trying to jump to another apsx page. (I can do that just fine)
So what I need is to figure out how to get the line number cell data passed to the code behind so that I can process it.
Anyone have any thoughts?
Thanks in advance for any help or ideas.
To get your row index, you need to use the GridViewCommandEventArgs object (e). Here is some code to get what you need:
Convert.ToInt32(e.CommandArgument)
Also, cells(0) will give you the cell containing your linkbutton. So, the line that gets the row number will look like this:
Dim lineNumber As String = gvDetailSecondLevel.Rows(Convert.ToInt32(e.CommandArgument)).Cells(1).Text
This is how I finally got it to work.
'Get RowIndex
Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)
'Reference Grid View Row
Dim row As GridViewRow = gvDetailSecondLevel.Rows(rowIndex)
'Get Cell value
Dim lineNumber As String = row.Cells(1).Text
I was able to then pass the variable lineNumber to my sub and it works great now!
Thanks Wenadin for the pointer!
This may sound simple, but I'm quite new to Visual Basic and ASP.net.
What I'm trying to do is create a new column in a ASP:GridView and create a link based on the value of another cell on that row.
Example Row:
Test Name | 123456 | http://www.testdomain.com/123456.pdf
123456 being the variable.
Hope you can help
You can use HyperLinkField (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlinkfield(v=vs.110).aspx)
<asp:HyperLinkField
DataTextField="EntityKey"
DataTextFormatString="http://www.testdomain.com/{0}.pdf"
DataNavigateUrlFields="EntityKey"
DataNavigateUrlFormatString="http://www.testdomain.com/{0}.pdf" />
You can put that link in an ItemTemplate
<ItemTemplate>
<asp:LinkButton ID="Button1" runat="server" PostBackUrl= 'http://www.testdomain.com' + '<%# Eval("ID") %>'></asp:LinkButton>
</ItemTemplate>
Inside GridView, then inside columns I have a bound field
<asp:BoundField DataField="Company Name" HeaderText="Company Name" SortExpression="Name" />
This displays a list of columns of the companys name (which is fine) however the headerText, is a clickable link that throws an error...how can i get the headertext just to display as a normal plan unclickable label
ta
You can use AllowSorting="False" to the gridview.
<asp:GridView AllowSorting="False">
<asp:BoundField DataField="Company Name" HeaderText="Company Name"/>
</asp:GridView>
Probably because you marked the bound field to be sortable and you didn't implement the sort on the server side... probably. Remove the SortExpression and see what's happening. You should post more info starting with the exception you have.
just remove the SortExpression
I have a gridview control in which there are two command buttons (linkButton), one is "select" and the other is "edit", now I have a session variable named as department and it contains values like "WRITING", "EDITING", "ADMIN" etc... Now the thing is I want the edit button to disappear when the department is "WRITING" but when it is "EDITING" or "ADMIN" I want edit button appear.
I was searching some forums for this problem and found this
row.Cells[0].Controls.Clear();
The problem with this code is that it hides the entire command column including "select" and "edit" buttons while I only want to control "edit" button, select should remain visible for all departments.
if I do it like (on row data bound event)
e.row.Cells[0].Controls[1].visible = false OR
e.row.Cells[0].Controls[0].visible = false
Specified argument was out of the range of valid values.
How can I do this properly ?
Thanks.
You can use the Visible property in the following way.
if (Session(mode) == "WRITING")
{
(e.Row.FindControl("btnEdit")).Visible = false;
}
First of all - your code does not work because your gridViewCell at index 0 contains only one control. Every CommandButton gets a single cell! Furthermore I am not quite sure why you make use of the RowDataBound event. As you are using a Session-Var this one may not change while binding your rows.
I tried the following piece of code in my pageLoad eventHandler - and it worked out for me:
/// just replace CAPITAL letters with your names and
/// (Session["department"] == "WRITING") with your boolean expression
gvYOURGV.Columns[INDEXOFSELECTCOLUMN].Visible = (Session["department"] == "WRITING");
In case you do want to have (need to have?) more flexibilty on modifying the appearance of your command columns - you may even consider, changing your command columns to templated columns. Doing so you may have the possibility for this code:
<asp:GridView ID="gvYOURGV" runat="server">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1"
visible='<%# Session["department"] != "WRITING" %>'
runat="server" CausesValidation="False"
CommandName="Select" Text="Auswählen"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
hth
How to bind one column to the gridview which is not present in the database?
I want to display the total unit in the last column named Total Unit but it is not present in database.
I got an argument exception:
Column 'tunit' does not belong to
table.
foreach(DataRow row in dt.Rows )
{
object[] obj=new object[2];
obj[0] = row["Transaction_Id"];
obj[1] = row["tunit"];
dtgrid.Rows.Add(obj);
}
The best solution to this problem is to implement unbound column as it is explained in the Unbound Columns topic.
if you are binding a list, you can add a property to that list and bind to the grid using eval("PropertyName")
When you prepare the select query make sure you have one more extra column with the total value you want for that particular row, then you can specify a column in the gridview as follows, make sure you specify
'AutoGenerateColumns = false;
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="sds_datasourceName">
<Columns>
<asp:TemplateField HeaderText="Total" >
<itemtemplate>
<asp:Label ID="Total" runat="server" Text='<%# Bind("Total") %>'></asp:Label>
</itemtemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>