I have a gridview with a hyperlink:
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="False"
CssClass="table table-hover table-striped" GridLines="None" >
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="EmployeName">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%# Bind("EmployeName") %>' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" SortExpression="EmployeID" Visible="False">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("EmployeID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
However, it should only appear as a hyperlink if the employeeID is that of the logged in employee.
I can do all that, but what I do not know is how to make the hyperlink look like a Label. It's easy to have it not link anywhere, but I do not know how to make it look like a label.
Thanks
I believe if you set Enabled="false" it does. If it does not, then the only way to do that is put both a HyperLink and Label in the cell, and show the link when appropriate, and the label when appropriate, hiding the other one (which can be easily done in RowDataBound event).
Related
I have an example from this gridview:
<asp:GridView ID="GridView3" runat="server">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
<ItemStyle></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("nome")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And I would like to add a button for ordering the data in each column.
Somebody can help me find the best way to do this?
Sorting is built into the GridView control. There is a control Property called AllowSorting, set this to True and you can sort columns by clicking on the header row titles.
There is no need to add extra buttons, even if you want to do some type of special sorting, you'd still Handle the Sorting event and implement your own methods
I have a problem with adding a new row with several controls like textBoxes into Header of GridView. When I add them into Header in GridView_RowCreated method with ...
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow r = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
TableCell tc = new TableCell();
.. they always are put into first row, not second. How can I change it? I want my created row put in second Header Row. I tried to do it by myself, Firstly, I modified ShowHeader into false and add both first and second rows programmatically, but it is wrong way, aspecially when grid has no data to show but it is necessary to show header. I found this discussion similar discussion, but intellisence doesn't know about override PrepareControlHierarchy. I tried to search it with Object browser, and found that this is method of GridView, but I couldn't plug it and test. Maybe somebody knows easier solution for this prob.
I need some assist.
UPDATED!!!
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="username" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None" onrowcreated="GridView1_RowCreated">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="username" SortExpression="username">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("username") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Surname" SortExpression="Surname">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Surname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
UPDATED!!!!
Yes, profs you are right, in my case it is the easiest way to put controls in HeaderTemplate. I forgot about it. But I have a little question, how can I add names of columns without adding additional Labels? Here is my code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="username" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None" onrowcreated="GridView1_RowCreated">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField **HeaderText="username"** SortExpression="username">
<HeaderTemplate>
**<asp:Label ID="Label4" runat="server" Text="username"></asp:Label>**<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("username") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<HeaderTemplate>
<asp:Label ID="Label4" runat="server" Text="Name"></asp:Label><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Surname" SortExpression="Surname">
<HeaderTemplate>
<asp:Label ID="Label4" runat="server" Text="Surname"></asp:Label><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Surname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
I marked bold strings. If I leave without Label (just with HeaderText="username") it isn't show me any name in header column. Only if I add Labels it shows my names. What is wrong with it?
Dynamically you would have to add a second header row after every has been databound
Example in VB but you should be able to convert it easily enough.
Private Sub GridView1_DataBound(sender As Object, e As EventArgs) Handles GridView1.DataBound
// Add checks for row count.
// create a new row
Dim gvr As New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal)
Dim gvc As TableCell
// Create a new empty cell
gvc = New TableCell()
//add a new TextBox to the cell
gvc.Controls.Add(New TextBox())
// Add the cell to the row
gvr.Cells.Add(gvc)
// repeat above as necessary
// Add row to Gridview at index 1 (0 is bound header)
// GridView1.Controls(0) is the Gridview table
GridView1.Controls(0).Controls.AddAt(1, gvr)
End Sub
The simplest solution is to not attempt to do it in the code behind. Instead utilize the HeaderTemplate for your TemplateFields.
Here is one as an example:
<asp:TemplateField HeaderText="username" SortExpression="username">
<HeaderTemplate>
username
<br />
<asp:TextBox ID="username" runat="server" />
</HeaderTemplate>
<ItemTemplate...
</asp:TemplateField>
I have a gridview having 5 columns, and last column is visible to some members only.
I want that when the last columngvMessageList.Columns[4] is inivisible, its width in percentage should be given to first columngvMessageList.Columns[0].
Please, let me know, how is it possible.
My GridView is as follow:
<asp:GridView ID="gvMessageList" runat="server" Width="100%" AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true"
DataKeyNames="MESSAGE_ID" CellPadding="4" PageSize="51" EmptyDataText="No Records Found." OnSorting="gvMessageList_Sorting"
OnPageIndexChanging="gvMessageList_PageIndexChanging" OnRowDataBound="gvMessageList_RowDataBound" GridLines="None"
CssClass="table table-bordered table-condensed table-hover table-striped">
<Columns>
<asp:TemplateField HeaderText="Subject" HeaderStyle-Width="30%" SortExpression="MESSAGE_SUBJECT" HeaderStyle-BackColor="#D9EDF7"
HeaderStyle-ForeColor="#0088CC">
<ItemTemplate>
<asp:HyperLink ID="hlnkMessageSubject" runat="server" Text='<%# ((System.Data.DataRowView)Container.DataItem)["MESSAGE_SUBJECT"] %>'
NavigateUrl='<%# ((System.Data.DataRowView)Container.DataItem)["MESSAGE_URL"] %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="From" HeaderStyle-Width="14%" SortExpression="MESSAGE_FROM" HeaderStyle-BackColor="#D9EDF7"
HeaderStyle-ForeColor="#0088CC">
<ItemTemplate>
<asp:HyperLink ID="hlinkUser" runat="server" Text='<%#((System.Data.DataRowView)Container.DataItem)["MESSAGE_FROM"] %>'
NavigateUrl='<%#((System.Data.DataRowView)Container.DataItem)["FROM_URL"] %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CREATION_DATE" HeaderText="On" HeaderStyle-Width="15%" HeaderStyle-BackColor="#D9EDF7" HeaderStyle-ForeColor="#0088CC"
SortExpression="CREATION_DATE" />
<asp:TemplateField HeaderText="To" HeaderStyle-Width="21%" HeaderStyle-BackColor="#D9EDF7" HeaderStyle-ForeColor="#0088CC">
<ItemTemplate>
<asp:Label ID="lblTo" runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Broadcast" HeaderStyle-Width="20%" Visible="false" HeaderStyle-BackColor="#D9EDF7" HeaderStyle-ForeColor="#0088CC">
<ItemTemplate>
<asp:Label ID="lblBroadcast" runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In C# Codebehind:
I want increased percentage of first column here:
gvMessageList.Columns[4].HeaderStyle.Dispose();
gvMessageList.Columns[4].Visible = false;
Is it possible by using gvMessageList.Columns[0].HeaderStyle.Width = ?? or something like it.
Give the width property a new unit type. the first parameter is the width you want, and the second is what measurement to use. In the sample below, I'm telling the column to be 50% of the table's width.
gvMessageList.Columns[4].HeaderStyle.Width = New Unit(50, UnitType.Percentage);
You can change the width as follows
GrdDynamic.Columns[4].ItemStyle.Width=Unit.Pixel(500);
GrdDynamic.Columns[4].ItemStyle.Width=New Unit(50, UnitType.Percentage);
I have a gridview of messages that is populated by an object class. There is a list in the object so that for each message, there is a list of recipients. Problem is, right now the gridview is only displaying "Person[]" for each message. I want it to display as a count of the recipients in the list.
Any help would be appreciated.
HTML
<asp:GridView
ID="grdMsgSent"
runat="server"
CssClass="cellSpacing"
OnRowDataBound="grdMsgSent_RowDataBound"
AllowSorting="True"
EmptyDataText="You have not sent any messages."
AllowPaging="True"
PageSize="6"
OnPageIndexChanging="grdMsgSent_PageIndexChanging"
OnSorting="grdMsgSent_Sorting"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Recipients" HeaderText="Recipients" />
</Columns>
</asp:GridView>
C#
grdMsgSent.DataSource = listSentMsg.List;
grdMsgSent.DataBind();
For example, if you have a data column name called UserID, you can populate in grid view using Eval (DataBinding Expression). So your code will be like this in aspx page,
<asp:TemplateField HeaderText="UserID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblUserID" runat="server" Text='<%# Eval("UserID") %>' />
</ItemTemplate>
</asp:TemplateField>
In C# Code Behind,
DataTable dataTable = new DataTable();
DataColumn dataColumn;
dataColumn = new DataColumn("UserID");
You can use a templatefield:
For array[], do this:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Recipients.Length") %>' />
</ItemTemplate>
</asp:TemplateField>
For List, do this:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Recipients.Count") %>' />
</ItemTemplate>
</asp:TemplateField>
I need to develop such a program in which the GridView's rows should be decided at run time.
i.e. I have a table in database called dealer capacity.
Dealer_ID Capacity
D0001 5
Now when the Dealer D00001 is selected from combo box the number of rows in grid view should be 5. I want to use the template field also.
My code for GridView is:
<asp:GridView ID="grdlicence" runat="server" DataKeyNames="Version_id" GridLines="None" BorderStyle="Solid" AutoGenerateColumns="false" AllowSorting="true"
CssClass="mGrid table"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" >
<Columns>
<asp:BoundField DataField="Version_name" ItemStyle-CssClass="uppercase" ItemStyle-Width="150px" HeaderText="Version" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="Version_id" Visible="false" HeaderText="Version" HeaderStyle-HorizontalAlign="Left" />
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<center><asp:TextBox ID="txtprice" CssClass="alignments TextStyle" MaxLength="5" runat="server" ></asp:TextBox></center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Licence Id">
<ItemTemplate>
<center><asp:TextBox ID="txtlicenceid" CssClass="alignments uppercase" runat="server" ></asp:TextBox></center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Purchase Date">
<ItemTemplate>
<center><asp:TextBox ID="txtpurchasedate" onfocus="showCalendarControl(this);" CssClass="alignments TextStyle" runat="server"></asp:TextBox></center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Expiry Date">
<ItemTemplate>
<center><asp:TextBox ID="txtexpirydate" onfocus="showCalendarControl(this);" CssClass="alignments TextStyle" runat="server"></asp:TextBox></center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Upload File">
<ItemTemplate>
<center><asp:FileUpload ID="fileUpload" runat="server" /></center>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You need to define PageSize for your GridView and remember to set AllowPaging to true for the GridView
GridView.PageSize Property
Gets or sets the number of records to display on a page in a GridView
control.
The default is 10.
You may see this article: GridView Paging Sample in ASP.NET
You can use the linq Take() and pass the number as parameter.
Updated according to the comment, use following code.
grdlicence.DataSourse= ds.Take(5);
grdlicence.DataBind();