Using Gridview and getting scrollable cells? - css

I am using Gridview and I want to have the cells scrollable. I already have a div that scrolls the whole table, but I need to individual cells to be kept to a certain max width and a scrollbar be created for cells that have data that would exceed that.
I have tried adding <rowstyle Wrap="false"/> this made the data stop wrapping, which I wanted, but now the rows are very very wide. Which I don't want.
I found several suggestions that say to add a div around the Gridview which I did and added a css rule like overflow-x:auto;width:200px; this did not help the cells are still very very wide.
I have the Gridview setup like this:
<asp:GridView ID="PMDataGrid"
datasourceid="PMDataSource"
ondatabound="PMDataGrid_DataBound"
autogeneratecolumns="false"
emptydatatext="No Data Available"
allowpaging="true"
pagesize="25"
runat="server"
showheader="true"
showfooter="true"
DataKeyNames="PROJECT"
allowsorting="true"
style="overflow-x:auto"
headerstyle-cssclass="FixedHeader">
<HeaderStyle BackColor="Black" ForeColor="White" />
<FooterStyle BackColor="Black" ForeColor="White" />
<RowStyle BackColor="White" ForeColor="Black" height="50px" Wrap="false" Width="250px" />
<AlternatingRowStyle BackColor="#e5e5e5" ForeColor="Black" height="50px" />
<SelectedRowStyle BackColor="#999999" ForeColor="#fff" />
I would like to be able to have a max-width of 250px and have a scrollbar when the data is too long, but I am unable to find a solution. I am fairly new to web apps in asp.net.
I have added this:
<asp:TemplateField HeaderText="COE_%_RCVD_BY_LINE" SortExpression="COE_%_RCVD_BY_LINE">
<ItemTemplate>
<asp:Label ID="Lable70" runat="server" Text='<%# Bind("COE_%_RCVD_BY_LINE") %>'></asp:Label>
</ItemTemplate
</asp:TemplateField>
Which produced this error:
Parser Error Message: A call to Bind was not well formatted. Please refer to documentation for the correct parameters to Bind.
I think it's the % that is causing the error. Is there a way to keep the % and have it work?
I forgot to add the surrounding <div></div>. It now looks like this:
'>
How and I have CSS setup like this:
.Gridview {
overflow-x:auto;
width:250px;
}
I'm pretty sure I have this setup correctly, but still the columns are very very wide:
and the rules in the CSS don't seem to be applying:

I have it figured out.
I had the CSS file with the style, this did not work. When I added the style to the div inline then it works, like this:
<asp:TemplateField HeaderText="DESCRIPTION" SortExpression="DESCRIPTION">
<ItemTemplate>
<div class="Gridview" style="overflow-x:auto; max-width:250px;">
<asp:Label ID="Lable1" runat="server" Text='<%# Bind("DESCRIPTION") %>'></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
This now works to add the scrollbar to any field that goes over the 250px max that I have set.
This can mostly be found in the similar question: Is it possible to scroll in a cell of a gridview?
But I did learn that the style has to be inline for it to work. Having the style in the CSS file did not work.

Related

GridView TemplateItem Width

I have a GridView which contains a TemplateItem column which contains a textbox. The grid itself works fine, the rows are create, I can edit the text box, etc. But the width is static. I can manually set the width but ideally I'd like it to just automatically stretch to whatever size the column is.
I've tried Googling an answer to this, and everything I've seen has been around using CSS or having a Width property assign a specific width.
This is my GridView code:
<asp:GridView runat="server" ID="grdSelectedProducts" BorderWidth="1px" CellPadding="3" CellSpacing="2" AutoGenerateColumns="False" OnRowDataBound="grdSelectedProducts_OnRowDataBound" ShowHeaderWhenEmpty="True">
<Columns>
<asp:BoundField DataField="Product" HeaderText="Product" ReadOnly="False" />
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtDescription" Text=""></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Try setting width:auto, Though you may not see a style property for a <asp:TextBox> still it will render it
<asp:TextBox runat="server" ID="txtDescription" style="width:auto !important"
Text="">

Asp.net GridView shows the content of the directory when using Chrome

I'm facing an issue when dealing with GridViews.
I've got a GridView which displays some data. When I click on any of the rows using IE8 everything is fine and it will display the page associated to it.
When performing the same operation under Chrome, what happens is that the browser is displaying the content of the directory. (This is really awkward as I was expecting the same behaviour as IE8)
I don't know what's going on and I've searched everywhere for similar issues but without luck.
First I thought it could be something related to EnableViewState, but after changing the values, everything remains the same.
Here is part of the definition of my GridView:
<asp:datagrid id="sGrid" ShowHeader="True"
AutoGenerateColumns="False" runat="server"
CssClass="tablelist" EnableViewState="true" UseAccessibleHeader="true"
onselectedindexchanged="sGrid_selectedIndexChanged">
<HeaderStyle CssClass="myHeading"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderStyle-HorizontalAlign="Left">
<HeaderStyle CssClass="myHeading"></HeaderStyle>
<ItemStyle CssClass="mySummary"></ItemStyle>
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderStyle-HorizontalAlign="Left" HeaderText="C1">
<HeaderStyle CssClass="myHeading"></HeaderStyle>
<ItemStyle CssClass="mySummary"></ItemStyle>
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderStyle-HorizontalAlign="Left" HeaderText="C2">
<HeaderStyle CssClass="myHeading"></HeaderStyle>
<ItemStyle CssClass="mySummary"></ItemStyle>
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
The method for the row click is as follows:
protected virtual string GetRowOnClickScript(DataGridItem item) {
return String.Format("rowClick(this,'{0}','{1}','{2}');",
((myAdapter)item.DataItem).C1,
((myAdapter)item.DataItem).C2,
((myAdapter)item.DataItem).C3);
}
RowClick is a js function:
function rowClick(sender, C1, C2, C3)
{
location.href = 'myPage.aspx?c1='+ C1 +'&c2='+ C2 + '&c3=' + C3;
}
The function is used like this:
if (IsBrowserIE(Request.ServerVariables["HTTP_USER_AGENT"]))
{
e.Item.Attributes.Add("onclick", GetRowOnClickScript(e.Item));
}
else //Chrome
{
AddAttributesToCell(e.Item.Cells, "onclick", GetCellOnClickScript(e.Item));
}
What I believe is that this bit of code here will only work for IE and not for any other browser!!!.
Update:
Thanks to the help from Yuriy Galanter, we found out that attribution will work regardless of the browser (at least for Chrome).

I want to put table in asp.net grid view

this is my grid view code given below>
<asp:GridView ID="gvReport" runat="server" AutoGenerateColumns="False" GridLines="None"
ShowHeader="true" HorizontalAlign="Left" OnRowCommand="gvReport_RowCommand">
<Columns>
<asp:TemplateField HeaderText="User" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Image id="img" text=<%#Eval("Coupon_Image")%>/>
</ItemTemplate>
<HeaderStyle Font-Bold="false" HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="getCoupon" runat="server" CssClass="btn" Width="145" Height="34"
CommandName="getCoupon" CommandArgument='<%#Eval("Coupon_Id")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In this it showing like this
Image 1 Button
Image 2 Button
actually i want like this type in grid view
Image1 Image2 Image3
Button Button Button
Please help me ....
advance thanks
A gridview won't work for this. Use the Table control for that, and programmably build the rows and columns. The challenge is the gridview isn't meant to render horizontally. You may be able to make it work with a repeater or ListView too, as you can have the ListView or repeater render the columns, and the layout template be the table and header row.

Applying ItemStyle or HeaderStyle in GridView Skin

I would like to apply default column-based styles to my GridView controls using a skin or other function of ASP.NET themes (or really any other automated way).
Currently my GridView skin looks like this:
<asp:GridView runat="server" GridLines="None">
<HeaderStyle CssClass="GridViewRowHeader" />
<FooterStyle CssClass="GridViewRowHeader" />
<RowStyle CssClass="GridViewRowA" />
<AlternatingRowStyle CssClass="GridViewRowB" />
</asp:GridView>
And a typical GridView column definition looks like this:
<Columns>
<asp:BoundField DataField="ExitTimestamp" HeaderText="Exit" SortExpression="ExitTimestamp">
<ItemStyle CssClass="GridViewCell" />
<HeaderStyle CssClass="GridViewHeader" />
</asp:BoundField>
<asp:TemplateField HeaderText="Visitor Name/Agency">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("VisitorName") %>'></asp:Label><br />
<asp:Label ID="Label2" runat="server" Text='<%# Bind("VisitorAgency") %>'></asp:Label>
</ItemTemplate>
<ItemStyle CssClass="GridViewCell" />
<HeaderStyle CssClass="GridViewHeader" />
</asp:TemplateField>
</Columns>
What I would like to get around is having to add the <ItemStyle> and <HeaderStyle> elements to every column definition in my GridView controls. I have tried various ways in the skin file including adding a <Columns> container and <ItemStyle> element therein, but nothing seems to work. Is there a way to do this?
For posterity, here is how I ended up specifically solving the problem using CSS.
I assigned a CSS class to the GridView in my skin file like so:
<asp:GridView runat="server" CssClass="GridView">
The style selects td (item) and th (header) cells within the GridView and works just as well as applying a plain CSS class directly with ItemStyle and HeaderStyle in your Column definitions:
table.GridView tr td, table.GridView tr th
{
/* style attributes here */
}
The advice here may work for you http://forums.asp.net/t/1063057.aspx in short define and add a CssClass to your gridview then use plain old CSS stylesheet to target and decorate the control.

datagrid with fixed header and scrollbars

Is possible set fixed header and scrollbar in a datagrid?
How can i do?
thanks
I worked on this for awhile and gave up on making the CSS work on all browsers. A simple albeit not elegant way to accomplish this is to just have two distinct tables whose column widths match up. The first table holds the headers and the second holds the content and is scrollable.
I used jQuery to make all the column widths match up.
For a complete description, see this post: Scrollable DataGrid table with Fixed Header with jQuery
have at this codeproject entry: Fixed Header in ASP.NET DataGrid
To solve this, I placed a second datagrid above the original, I sized all the columns in both datagrids equally. In the datagrid that shows the data, I set the header to not show. In the code behind I bound the top datagrid to an empty data table and set the bound fields to show the header when empty.
<div style="padding-bottom:2px">
<asp:GridView id="RoleListHeader" runat="server" AutoGenerateColumns="false" showheaderwhenempty="True" showheader="true" height="20px" width="350px" selectedrowstyle-height="20px" rowstyle-height="20px" headerstyle-height="10px" forecolor="#ff333333" backcolor="White">
<columns>
<asp:TemplateField ItemStyle-Width="23">
<ItemTemplate>
<asp:CheckBox></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SystemDesc" HeaderText="Role Description" ReadOnly="true" ItemStyle-Width="174"></asp:BoundField>
<asp:BoundField DataField="EndDate" HeaderText="EndDate" ReadOnly="true" ItemStyle-Width="153"></asp:BoundField>
</columns>
</asp:GridView>
</div>
<div style="max-height:300px;overflow:auto">
<asp:GridView id="RoleList" runat="server" AutoGenerateColumns="false" showheaderwhenempty="True" showheader="false" height="0px" width="350px" selectedrowstyle-height="20px" rowstyle-height="20px" headerstyle-height="10px" forecolor="#ff333333" backcolor="White" rowstyle-horizontalalign="Center" rowstyle-borderstyle="Solid" rowstyle-bordercolor="#ff404040">
<columns>
<asp:TemplateField ItemStyle-Width="23">
<ItemTemplate>
<asp:CheckBox runat="server" OnCheckedChanged="GVChkBox_CheckChanged" AutoPostBack="true"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SystemDesc" HeaderText="Role Description" ReadOnly="true" ItemStyle-Width="174"></asp:BoundField>
<asp:BoundField DataField="EndDate" HeaderText="EndDate" ReadOnly="true" ItemStyle-Width="153"></asp:BoundField>
<asp:BoundField DataField="ADHSystemId" ShowHeader="false"></asp:BoundField>
</columns>
</asp:GridView>
</div>

Resources