Gridview Column Width in ASP.NET 2.0 - asp.net

How do you control the column width in a gridview control in ASP.NET 2.0?

You can use the HeaderStyle-Width, ItemStyle-Width or FooterStyle-Width properties. These can be applied to all the columns or per-column basis.
<asp:GridView ID="GridView1" runat="server">
<HeaderStyle Width="10%" />
<RowStyle Width="10%" />
<FooterStyle Width="10%" />
<Columns>
<asp:BoundField HeaderText="Name" DataField="LastName"
HeaderStyle-Width="10%" ItemStyle-Width="10%"
FooterStyle-Width="10%" />
</Columns>
</asp:GridView>

I do it using the header style for the column:
<asp:BoundField HeaderText="Name" DataField="LastName">
<HeaderStyle Width="20em" />
</asp:BoundField>

Here's the C# code to do it programatically:
columnName.ItemStyle.Width = Unit.Percentage(someDouble);

Gridview.Columns[1].ItemStyle.Width = 100;
This will set the with in pixel.

Related

Why do the gridview still appears even if the datasource is empty?

<asp:GridView ID="gv_Info" runat="server" Width="1240px" BorderWidth="1px"
CellPadding="4" BorderColor="Gray" AlternatingItemStyle-BackColor="#f6f6eb"
BackColor="#F3372A" AutoGenerateColumns="False" ShowFooter="false">
<AlternatingRowStyle BackColor="White" Wrap="false" />
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelectAsset" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="d" HeaderText="ATag">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="c" HeaderText="Description">
<HeaderStyle />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="a" HeaderText="AType">
<HeaderStyle />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="q" HeaderText="ID">
<HeaderStyle />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
</asp:GridView>
gv_InfoAssetsByParams.DataSource = ArrayAssets.ToList()
gv_InfoAssetsByParams.DataBind()
but when ArrayAssets is empty I want to make the gridview empty. SO I do this,
If ArrayAssets.Length > 0 Then
gv_Info.DataSource = ArrayAssets.ToList()
gv_Info.DataBind()
Else
gv_Info.DataSource = ArrayAssets.ToList()
gv_Info.DataBind()
End If
But it still appears even if i assign Nothing to the data source. How do I make it empty with headers?
ASP has a property called ShowHeader when empty. (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.showheaderwhenempty.aspx)
Set ShowHeaderWhenEmpty="false", that should work.
After looking at your code, you are repeating the same thing in your IF statement.
I tried to replicate your problem on my end, and indeed if the datasource is empty the grid is not visible. I think you should look at the properties of your gridview and go from there. As well, I recommend using a repeater for more control of the visibility and layout when displaying data.

Wrap text in grid

I have a grid with a bound field. I have set Item_style-wrap=true.
But still it is NOT wrapping the data for me. What could be the issue?
Grid code:
<asp:BoundField DataField="Reason"
HeaderText="Reason For Request"
ItemStyle-Wrap="true"
ItemStyle-Width="10%"
/>
Try this:
<asp:BoundField>
<HeaderStyle Wrap="true" />
<ItemStyle Wrap="true" />
</asp:BoundField>

How can I reduce the overall height of an asp.net gridview control?

I'm trying to reduce the overall height of this gridview so there aren't so many "empty rows" at the bottom. I tried setting the Height attribute. I also tried setting the PagingSize to a smaller number and setting the AllowPaging Attribute=true
The datasource will return a variable number of rows, but the gridview needs to be fixed height. Right now it's taking up a lot more vertical real estate than any max number of HearingInfo would even reach. In other words, there is enough space at the bottom for at least 10 more rows, but there would never ever be 10 more rows.
I haven't worked with ASP.net web controls in a while...
<asp:GridView ID="gvHearingInfo" runat="server" AutoGenerateColumns="False" EnableModelValidation="True"
ShowHeader="False" Width="100%" GridLines="Vertical"
Height="50px">
<Columns>
<asp:BoundField DataField="Authority" HeaderText="Authority"
SortExpression="Authority">
<ItemStyle Width="144px" Wrap="False" HorizontalAlign="Left"
BorderColor="Black" />
</asp:BoundField>
<asp:BoundField DataField="PublicHearing" HeaderText="PublicHearing"
SortExpression="PublicHearing" >
<ItemStyle HorizontalAlign="Left" Width="431px" BorderColor="Black" />
</asp:BoundField>
<asp:BoundField DataField="HearingDate" HeaderText="HearingDate"
SortExpression="HearingDate" >
<ItemStyle HorizontalAlign="Center" Width="93px" BorderColor="Black" />
</asp:BoundField>
<asp:BoundField DataField="HearingTime" HeaderText="HearingTime"
SortExpression="HearingTime" >
<ItemStyle HorizontalAlign="Center" Width="92px" BorderColor="Black" />
</asp:BoundField>
<asp:BoundField DataField="DistrictTelephone" HeaderText="DistrictTelephone"
SortExpression="DistrictTelephone" >
<ItemStyle HorizontalAlign="Center" Width="94px" BorderColor="Black" />
</asp:BoundField>
</Columns>
</asp:GridView>
Results.DataSource = dataView;
Results.DataBind();
foreach(GridViewRow row in this.Results.Rows)
{
if (row.Equals(""))
{
Results.Rows[0].Visible = false;
}
}
or you will have to use the GridView.RowDataBound Event but I think the code should do the work...

hyperlink should come in multiline code not working

i use this code .. but when the hyperlinks are longer then field size the gridview expands verticaly. i want the link below as like a paragraph . give me a solution
<asp:gridview id="titlesGrid" runat="server"
datasourceid="titles"
width=90% cellpadding=5 font-size="8pt"
autogeneratecolumns=false
headerstyle-backcolor="maroon"
headerstyle-forecolor="khaki"
headerstyle-font-bold
rowstyle-verticalalign="top">
<columns>
<asp:hyperlinkfield headertext="Title"
datatextfield="title"
datanavigateurlformatstring="title_details.aspx?titleid={0}"
datanavigateurlfields="title_id" />
<asp:boundfield headertext="Title ID"
datafield="title_id" />
<asp:boundfield headertext="Category"
datafield="type" />
<asp:boundfield headertext="Pub ID"
datafield="pub_id" />
<asp:boundfield headertext="Price"
datafield="price"
htmlencode=false
dataformatstring="{0:n2}"
itemstyle-horizontalalign="right" />
</columns>
</asp:gridview>
Have you tried using <ItemStyle Wrap="True" /> for <asp:hyperlinkfield /> ?

How can I pass along the Page Number in a HyperLinkField in a GridView?

I have gridview that I am using paging on. I want to pass along the current page in a asp:Hyperlink so I can send them back to the current page when they are done viewing the details of a record. Is this possible? If it is how can I do this?
<asp:GridView ID="grdObitList" runat="server" allowpaging="true"
PageSize="10" AutoGenerateColumns="false" CssClass="grdClass"
BorderStyle="None" GridLines="None" CellSpacing="2" >
<PagerStyle HorizontalAlign="Center" />
<PagerSettings Position="Bottom" FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" />
<Columns>
<asp:HyperLinkField HeaderText="Name" DataTextField="obit_fullname" DataNavigateUrlFields="obit_id" DataNavigateUrlFormatString="obitDisplay.aspx?oid={0}" />
<asp:BoundField ReadOnly="true" HeaderText="Date" DataField="obit_dod" DataFormatString="{0:d/M/yyyy}" />
<asp:BoundField ReadOnly="true" HeaderText="Resident Of" DataField="obit_resident" />
<asp:BoundField ReadOnly="true" HeaderText="Funeral Home" DataField="obit_funeralhome" />
</Columns>
One way to do it is converting it to a template column, that way you can use normal databind syntaxt to get to it (<%#)

Resources