Formatting Gridview display in ASPX page - asp.net

Scenario : I have an ASPX page with Gridview. Every row has one column heading and it's value. At the end of record there is a generic line.
My issue is with formatting and CSS.
I would like the output to be as below (I have tried to format but upon posting it is loosing)
| Customer Name | Microsoft |
| Customer City | Seattle |
| |
| Customer Name | DowJones |
| Customer City | NYC |
Above the customer name is centered to that cell. Similarly it's value also (Microsoft). Also CustomerName is colored with darkblack and "Microsoft" with just black.
The '-----' and "|" are actually table borders/seperators. It is typical table with borders and horizontal line and vertical line.
Please suggest. Below is my code.
<div style="width:400px; font-family:Arial; font-size:small">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="100%" GridLines= "Horizontal">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div style="color:Blue;font-weight:bold">
<br />
<tr>
<td><asp:Label ID="Label5" runat="server" Text='Customer NAME' cssclass ="blackboldhdr" ></asp:Label></td>
<td><asp:Label ID="Label1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"DealerName") %>' cssclass ="blackboldtxt"></asp:Label></td>
</tr>
<tr>
<td><asp:Label ID="Label2" runat="server" Text='Customer City'></asp:Label></td>
<td><asp:Label ID="Label3" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"City") %>'></asp:Label></td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<style>
.blackboldtxt {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color:black;
font-weight: bold;
}
.blackboldhdr
{
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
/*color:#FFFFFF;*/
background-color: #2A3C54;
width: 152px;
text-align:center;
}
</style>

I don't think you can use <tr> and <td> elements inside the grid's itemTemplate. Since the grid will already render <tr>...</tr> for each row and <td> {Item template's content} </td>, the result would be invalid HTML code.
I think you should use a Repeater instead of the Grid, e.g. something like this:
<asp:Repeater>
<HeaderTemplate><table class="..."></HeaderTemplate>
<ItemTemplate>
<tr>
<td class="blackboldhdr">
<asp:Label runat="server" Text='Customer NAME' cssclass="blackboldhdr"/>
</td>
<td>
<asp:Label runat="server" cssclass ="blackboldtxt"
Text='<%#DataBinder.Eval(Container.DataItem,"DealerName") %>' />
</td>
</tr>
<tr>
... same as above, but for customer address
</tr>
</ItemTemplate>
<SeparatorTemplate><tr><td colspan="2"> </td></tr>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>

Related

How to hide first column of datagridview add still get data

I want to hide the first column of my gridview with css but am not successful.
Here is part of the html that is generated.
<table class="mGrid" cellspacing="0" cellpadding="3" rules="all" ridlines="None" border="1" id="PageContent_gvLocation" style="border-collapse:collapse;">
<tbody><tr>
<th scope="col">ubicacion id</th><th scope="col">Armario</th><th scope="col">Cajon</th><th scope="col"> </th>
</tr><tr>
<td>
<span id="PageContent_gvLocation_lblLocationID_0" class="gridTextbox">46</span>
</td><td>
<span id="PageContent_gvLocation_lblCloset_0" class="gridTextbox">testarmariio</span>
</td><td>
<span id="PageContent_gvLocation_lblDrawer_0" class="gridTextbox">testcajon</span>
</td><td>
<input type="image" name="ctl00$PageContent$gvLocation$ctl02$ctl00" title="Edit" src="../Images/edit.png" style="height:20px;width:20px;">
<input type="image" name="ctl00$PageContent$gvLocation$ctl02$ctl01" title="Delete" src="../Images/delete.png" style="height:20px;width:20px;">
</td>
</tr><tr class="alt">
<td>
Here is my asp.net code.
<asp:GridView ID="gvLocation" runat="server" AutoGenerateColumns="false" ShowFooter="true"
ShowHeaderWhenEmpty="true" AllowPaging="True" OnPageIndexChanging="gvLocation_PageIndexChanging"
OnRowEditing="gvLocation_RowEditing" OnRowCancelingEdit="gvLocation_RowCancelingEdit"
AllowSorting="true" onsorting="gvLocation_Sorting" ridLines="None" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" CellPadding="3">
<%-- Theme Properties --%>
<Columns>
<asp:TemplateField HeaderText="ubicacion id" SortExpression="ubicacion_componente_id">
<ItemTemplate>
<asp:Label CssClass="gridTextbox" ID="lblLocationID" Text='<%# Eval("ubicacion_componente_id") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox CssClass="gridTextbox" ID="txtLocationID" Text='<%# Eval("ubicacion_componente_id") %>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox CssClass="gridTextbox" ID="txtLocationIDFooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
How can I hide the first column of my gridview using css.
if you want to implement only in CSS according to your code, you can try following
table.mGrid tr td:first-child{
display:none;
}
table.mGrid tr th:first-child{
display:none;
}
Pager is generally available in last row, you can always have option to exclude last row of the table, use following code :
table.mGrid tr:not(:last-child) td:first-child {
display: none;
}
table.mGrid tr:not(:last-child) th:first-child {
display: none;
}

Style table in repeater from code behind

Hey guys I need some help with an application that I'm developing.
I'm trying to add a css class / styling to a table depending on a certain value that I'm getting from the database ( e.g. 0 - 2).
This is the code where I need to change the styling of the table
Public Function projectType(ByVal value As Integer)
Dim projectName As String
If value = 0 Then
projectName = "Project"
mytable.AddAttributes("Style", "Background-color:#444444")
ElseIf value = 1 Then
projectName = "Support"
Else
projectName = "Not available"
End If
Return projectName
End Function
Markup:
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" RepeatColumns="1"
RepeatDirection="Vertical" CellPadding="0" CellSpacing="0">
<ItemTemplate>
<table cellpadding="0" cellspacing="0" style="width: 100%; text-align: left; border-bottom: 1px solid #999999;
padding-bottom: 15px;">
<tr>
<td style="width: 110px; vertical-align: top; padding-left: 20px; padding-top: 5px;
padding-bottom: 5px;">
<asp:Label ID="Label5" runat="server" Text='<%# projectType(Eval("Type")) %>' Font-Names="Verdana"
Font-Size="9pt" EnableTheming="false" />
</td>
<td style="width: 110px; vertical-align: top; padding-left: 20px; padding-top: 5px;
padding-bottom: 5px;">
<asp:Label ID="Label2" runat="server" Text='<%# hoursCheck(Eval("Duration")) %>'
Font-Names="Verdana" Font-Size="9pt" Style="text-align: right" EnableTheming="false" />
</td>
</tr>
<tr>
<td colspan="2" style="padding-bottom: 5px; padding-top: 5px; text-align: center">
<asp:Label ID="Label7" runat="server" Text='<%# Eval("FullName") %>' Font-Names="Verdana"
Font-Size="7pt" EnableTheming="false" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
From this function I need to access the table by getting the parent table of the sender since I can't explicitly say I want Datalist1 since I have 5 of them, how can I do this?
First of all, please get rid of inline styles and replace them with CSS classes. Will save you a lot of headache later on. For instance, have two CSS classes, one for "Project", another for "Support" and "Not available"
As for the actual question, the easiest thing that comes to mind is to split the logic into two functions, one for text and one for css class:
Public Function projectTypeText(ByVal value As Integer)
...
Return projectName
End Function
Public Function projectTypeClass(ByVal value As Integer)
...
Return projectCssClass
End Function
And then use it exactly as you did:
<%-- This might actually need runat="server", not sure --%>
<table cellpadding="0" cellspacing="0" style='<%# projectTypeClass(Eval("Type")) %>'
<asp:Label ID="Label5" runat="server" Text='<%# projectType(Eval("Type")) %>'
Note: if you absolutely have to deal with inline classes, you can have projectTypeStyle which returns Background-color:#444444, but then inside the markup you will need to do nasty stuff like that:
<table cellpadding="0" cellspacing="0" style='<%# "width: 100%; rest of styles; " + projectTypeStyle(Eval("Type")) %>'
This is horrible, so please avoid at all cost.

How can I display records in multiple tables dynamically using repeater or datalist control in ASP.NET?

What I would like to do is show the records I get in datatable in different HTML tables. The records in the datatable have 3 columns and I can have a max of 6 records.
The datatable I am getting from the database is as follows:
Name Age
-----------
XYZ 30
XY1 31
XY2 32
XY3 33
And I want to show the records in the following way:
Name Age Name Age
-----------------------
XYZ 30 XY1 31
Name Age Name Age
-----------------------
XY2 32 XY3 33
Total Strength of employees : 4
What I am currently doing is as follows (although this is not what I want, but have tried):
<asp:Repeater ID="RptNCode" runat="server" OnItemCommand="RptNcode_ItemCommand"
OnItemDataBound="RptNCode_ItemDataBound" >
<HeaderTemplate>
<tr style="background-color: #507CD1; color:
White; font-weight: bold; text-align: center; height: 20px">
<td style="color: White;">
Sr.No
</td>
<td style="color: White; text-align: left">
Name
</td>
<td style="color: White; text-align: right">
Age
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color: #ffffff; height: 20px; font-family: Arial">
<td align="left" style="font-size: 15;">
<asp:LinkButton ID="lnkNCode" ForeColor="#336699" runat="server" Text='<%# Eval("Name") %>'
CommandArgument='<%# Eval("Name") %>' CommandName="NCode"></asp:LinkButton>
</td>
<td align="center" style="font-size: 15; text-align: right;">
<%# Eval("Age")%>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style="background-color: #ffffff; height: 20px; font-family: Arial">
<td align="left" style="font-size: 15;">
<asp:LinkButton ID="lnkNCode" ForeColor="#336699" runat="server" Text='<%# Eval("Name") %>'
CommandArgument='<%# Eval("Name") %>' CommandName="NCode"></asp:LinkButton>
</td>
<td align="center" style="font-size: 15; text-align: right;">
<%# Eval("Age")%>
</td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
<tr>
<td align="right" height="40px">
<asp:Label ID="lblTotal" runat="server" Font-Bold="true" ForeColor="#336699"></asp:Label>
</td>
</tr>
</FooterTemplate>
</asp:Repeater>
I know this is not correct but it is what I have done so far. Is what I am trying to do possible or not?
Thanks.
You are actually pretty close with what you have now. The trick is when you close off your table rows. Try something more like this:
<asp:Repeater ID="RptNCode" runat="server"
OnItemCommand="RptNcode_ItemCommand"
OnItemDataBound="RptNCode_ItemDataBound">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
Name
</td>
<td>
Age
</td>
<td>
Name
</td>
<td>
Age
</td>
</tr>
<tr>
<td>
<%# Eval("Name") %>
</td>
<td>
<%# Eval("Age") %>
</td>
</ItemTemplate>
<AlternatingItemTemplate>
<td>
<%# Eval("Name") %>
</td>
<td>
<%# Eval("Age") %>
</td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Notice how the AlternatingItemTemplate keeps the same table row going for another two columns, placing the next record in your datatable on the same row. Then when it reaches the ItemTemplate again, it adds another row of header information and starts all over again.

how to remove space under a table?

Here what I've done and whats my problem. First of all i got a big table with a lot of td the first td on top of my table containt anoter table and (here come my problem) theres a space under that little table and I don't know why.
Here's the code for my table:
<div runat="server" class="ReportPage" >
<table runat="server" class="ListReportBigTable" align="center">
<tr>
<td class="style13" colspan="3" >
<table width="46%" align="center"style="height:50%; "cellpadding="0">
<tr>
<td align="left">
<asp:Label ID="LB_ChooseReport" runat="server"
Text="Choisissez un dossier médical: " Font-Size="Small">
</asp:Label>
</td>
</tr>
<tr>
<td style="vertical-align:bottom" align="left">
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<div id="div_Filter" runat="server" visible="false" align="left">
<asp:Label ID="LBL_FilteredBy" runat="server" width="18%" Text="Patient : "
Font-Size="Small" style="margin-left: 0px"></asp:Label>
<asp:DropDownList ID="DDL_FilterSelect" runat="server" AutoPostBack="true"
Width="25%" CssClass="DDL_Filter" Font-Size="Small" Height="18px"></asp:DropDownList>
</div>
</ContentTemplate>
</asp:UpdatePanel></td></tr></table>
<br />
</td>
and here the CSS:
.ListReportBigTable
{
height:25%;
width:55%;
text-align:center;
vertical-align:middle;
border: thick solid black;
margin-left: 0px;
}
thx in advance
I don't know if it's a typo but you are closing with a </td> after your closing </table> tag. In addition you have a <br /> before the closing misplaced </td> which would obviously add space to the bottom.
Try changing the closing </td> to </table></div> and removing the <br />
In addition, in your CSS try adding margin-bottom: 0px; and padding-bottom: 0px; to see if this has an effect.
Also, what is after the table? Maybe whatever is being displayed after the table has it's own top margin/padding?
Do you have a link to the actual page where this is occuring so we can examine with Firebug or similar and try to figure it out?

Visual Studio 2010 does not follow my CSS ... help pls

here is my CSS:
.body
{
font-family : Segoe UI;
}
.table{
width : 50%;
font-size:medium;
border-spacing:5px;
}
.leftCol
{
width:15%
}
.RightCol
{
width:35%
}
.header
{
width:35%;
font-weight:900;
text-align:left;
}
.dates
{
color:GrayText;
font-size:small
}
.pictures
{
width:10%;
}
.userName
{
width:10%;
text-align:left;
}
.smallTable
{
width:20%;
}
Here is the HTML markup:
<table class=".table">
<tr>
<td rowspan="3" class=".leftCol">
<asp:Label ID="ImageLabel" runat="server" Text='<%# Eval("Image") %>' />
</td>
<td class=".header">
<asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' />
</td>
</tr>
<tr>
<td class=".rightCol">
<asp:Label ID="TextLabel" runat="server" Text='<%# Eval("Text") %>' />
</td>
</tr>
<tr>
<td class=".dates">
<asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
</td>
</tr>
</table><hr />
the other one:(this table width actually works)
<table class=".smallTable">
<tr >
<td class=".pictures">
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Pictures") %>' /></td>
<td class=".userName" align="left">
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>' /></td>
</tr>
</table><hr />
so, instead of following the rules, VS uses 100% width for the table using .table class, and then it just arranges in justified. The dates does not appear in gray colour ... and basically nothing follows. But i can assure you that the class names i wrote and everything is correct as i check. It just doesnt work . Do you know anyway i can force VS 2010 to do this ?
In all class that you declare on the tags you must place them with out the dot. Eg:
<table class="smallTable">
and change the class name .table to something else because the table is reserve for all tables and probably this type of variables lead to bugs. Do not mix up the tag names with the css declare.
The css table{ width : 50%; } change all the tables on the page.
The css body{ font-family : Segoe UI; } change all the body fonts.
The css .body{ font-family : Segoe UI; } is change only what you declare on class as body but eventually can lead to bugs and errors .

Resources