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="">
Related
When I hide a field in one of my DataGrid columns, the column's width appears to be omitted from the grid altogether, causing the sizing of all my other columns to be readjusted. Now the sizing of the column is already being auto-adjusted [responsively] by the bootstrap styling (CssClass="table table-hover"; see below). That's fine. What's not fine is the column width being adjusted a second time when I dynamically (from code-behind) hide one of the fields (Visible = false)--reason being, I have a second DataGrid identical to this first one below whose untouched column widths must match (I'm not hiding any fields there).
<asp:datagrid id="dgS" AutoGenerateColumns="False" CssClass="table table-hover" ShowHeader="False" ShowFooter="False" runat="server" DataKeyField="SID" CellPadding="0" GridLines="None" AllowSorting="True" OnItemDataBound="dgS_ItemDataBound">
<HeaderStyle CssClass="" BackColor="steelblue" ForeColor="white" />
<Columns>
<asp:TemplateColumn HeaderText="SID" SortExpression="SID" HeaderStyle-CssClass=" " ItemStyle-CssClass=" ">
<ItemTemplate>
<asp:Label ID="lblSID" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "SID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Grade" SortExpression="GradeChangeDescription" HeaderStyle-CssClass=" " ItemStyle-CssClass=" ">
<ItemTemplate>
<asp:dropdownlist id="ddlG" Runat="server"></asp:dropdownlist>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="C" Visible="False" HeaderStyle-CssClass="" ItemStyle-CssClass="">
<ItemTemplate>
<asp:Label id="lblC" Runat="server" text='<%# String.Format("{0:MM/dd/yyyy}", DataBinder.Eval(Container.DataItem, "C"))%>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
Instead of hiding/removing the column all together and cause resize you could hide its content with opacity. eg:
.fakeHidden { opacity: 0;}
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.
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.
I have a gridview column in which I have one column that point to a pdf file on the file server. I need to have another column right beside this, displaying the pdf icon. The user should be able to click the icon and launch the file from the file server.
My code is:
<asp:GridView ID="gvInvoices" AutoGenerateColumns="false" runat="server" Font-Names="Arial" Font-Size="Small" Width="50%">
<Columns>
<asp:TemplateField HeaderText="File Type">
<ItemTemplate><img runat="server" src="Images/img_Pdf.gif" id="imgFileType" /></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate><%#Eval("InvoiceNumber")%></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
How can I add a "NavugateURL" or href="" to the img ?
Rather than using an HTML img control, try using an ASP.Net Server control.
A good choice would be the Hyperlink Control:
Instead of:
<img runat="server" src="Images/img_Pdf.gif" id="imgFileType" />
Use:
<asp:HyperLink ID="imgFileType" ImageUrl="Images/img_Pdf.gif" NavigateUrl="" runat="server">HyperLink</asp:HyperLink>
Just set your NavigateUrl property.
you need to wrap your icon around an anchor tag, and set the href of the anchor tag using DataBinding expression Eval. This assumes your Datasource field "PDFPath" is an absolute path.
<asp:GridView ID="gvInvoices" AutoGenerateColumns="false" runat="server" Font-Names="Arial" Font-Size="Small" Width="50%">
<Columns>
<asp:TemplateField HeaderText="File Type">
<ItemTemplate><a href='<%#Eval("PDFPath")%>'> <img runat="server" src="Images/img_Pdf.gif" id="imgFileType" /></a></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate><%#Eval("InvoiceNumber")%></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
inside the RowDataBind event look for your image control and set its NavigateUrl property
protected void gvInvoices_RowDataBound(object Sender , GridViewRowEventArgs e)
{
if(e.Row.RowType==DataRow)
{
HtmlControl icon = e.Row.FindControl("imgFileType") as HtmlControl;
icon.NavigateUrl = ((MyDataType)e.Row.DataRow).PDFPath;
}
}
Note it is free hand writing so you may find some syntax errors that you should fix
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>