Hiding columns in ASP.Net Datagrid - c#

My DataGrid is something like this:
<asp:DataGrid ID="tasks" runat="server" AutoGenerateColumns="False" GridLines="None">
<HeaderStyle CssClass="task-list-header"/>
<ItemStyle CssClass="task-list-row"/>
<Columns>
<asp:BoundColumn DataField="Name"
HeaderStyle-Width="100px"
HeaderText="Name"></asp:BoundColumn>
<asp:BoundColumn DataField="Description"
HeaderStyle-Width="250px"
HeaderText="Description"></asp:BoundColumn>
<asp:BoundColumn DataField="IsComplete"
HeaderStyle-Width="125px"
HeaderText="Is Complete"></asp:BoundColumn>
<asp:TemplateColumn>
<HeaderStyle Width="75px"/>
<ItemTemplate>
<asp:HyperLink ID="Hyperlink1" runat="server"
NavigateUrl='<%# ModuleContext.EditUrl("TaskId", Eval("TaskId").ToString(), "EditTask") %>'
Text="Edit"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderStyle Width="75px"/>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CommandArgument='<%# Eval("TaskId") %>' CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
This is basically a list of Task with Last 2 columns as Edit and Delete. I am using DNN8. I want to hide the Edit and Delete links if the user is not logged in as Superuser. How can I do that? I also tried the property OnAutoBinding but no luck.
Thanks, in advance for the help.

You can you like this
e.Row.Cells[columnIndex].Visible = false;

Related

Change CommandField text dynamically

<asp:BoundField HearderText="Name" Datafield="Name"/>
<asp:CommandField HearderText="Name" SelectText="Go" ShowSelectButton="True"/>
I want that each CommandField takes the data like DataField, so instead of "Go", it would have values like, "Jhon", "Mary"...in each row
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:LinkButton ID="btnSelect" runat="server" CommandName="Select" Text='<%# Eval("Name") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
This worked for me

ASP.NET: Download file from database when clicking on file name on Grid View

I have a grid view that displays all of uploaded files of an employee (data from SQL DB).
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" AllowPaging="true" ShowFooter="false" PageSize="5"
CssClass="table" AlternatingRowStyle-BackColor="WhiteSmoke"
HeaderStyle-BackColor="#6C7A95" HeaderStyle-BorderColor="#666666" HeaderStyle-BorderStyle="Solid" HeaderStyle-BorderWidth="2" HeaderStyle-ForeColor="White"
OnPageIndexChanging="OnPaging" EmptyDataText="No Documents">
<Columns>
<asp:BoundField DataField="file_name" HeaderText="File Name" />
<asp:BoundField DataField="upload_date" HeaderText="Date (GMT -7)" />
<asp:BoundField DataField="file_status" HeaderText="Status" />
<asp:TemplateField HeaderText="Employee's Note">
<ItemTemplate>
<a data-original-title='<%# Eval("emp_note")%>' href="#" class="demo-cancel-click" rel="tooltip"><i class="icon-book"></i></a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The interface look like this:
I like to archive this: the file name will be a link button (or whatever the best way is) and when click on name it will download the file. So I modified the File Name column:
<asp:TemplateField HeaderText =" File Name">
<asp:ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server"
OnClick="DownloadFile" Text='<%#Eval("file_name") %>'
CommandArgument='<%# Eval("file_id") %>'></asp:LinkButton>
</asp:ItemTemplate>
</asp:TemplateField>
But then the interface became: (file_name disappeared)
How do I get what I need?
You need to remove asp: from ItemTemplate. Your markup should look like below:
<asp:TemplateField HeaderText=" File Name">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server"
OnClick="DownloadFile" Text='<%#Eval("file_name") %>'
CommandArgument='<%# Eval("file_id") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Hope it helps!

Paging not working in GridView when ajax control is removed

I have a grid view in my aspx page. I have enabled paging also.
Paging is working fine when an ajax control is in page, but when I removed the ajax control paging is not working anymore. ie, on clicking page number 2 grid becomes empty.
What could be the possible reason? Please suggest a solution.
code
<asp:GridView ID="SitesGrid" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="SitesDataMgr" AutoGenerateColumns="False" DataKeyNames="ID" CellPadding="3" CellSpacing="3" OnRowDataBound="viewSite_RowDataBound" class="tbl_blck gridtable clearfix" PageSize="4" EmptyDataText="No rows found">
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="clientID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" HeaderStyle-ForeColor="White" SortExpression="ClientName">
<ItemTemplate>
<asp:Label ID="clientLabel" runat="server" Text='<%# Bind("ClientName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" HeaderStyle-ForeColor="White" SortExpression="SiteStatus">
<ItemTemplate>
<asp:Label ID="siteLabel" runat="server" Text='<%# Bind("SiteStatus") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Details" HeaderStyle-ForeColor="White" >
<ItemTemplate>
<asp:LinkButton ID="viewSite" runat="server" OnClick="viewSite_click" CausesValidation="False" CommandName="Edit"
Text="View"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="SitesDataMgr" runat="server" ConvertNullToDBNull="True" OldValuesParameterFormatString="{0}" SelectMethod="GetAllSitesByUser" FilterExpression="ClientName LIKE '%{0}%'" TypeName="Creation.DataMgr">
<SelectParameters>
<asp:SessionParameter Name="createdUserID" Type="String" SessionField="strUserName" />
</SelectParameters>
<FilterParameters>
<asp:ControlParameter Name="ClientName" ControlID="txtSearch" PropertyName="Text" />
</FilterParameters>
</asp:ObjectDataSource>
It looks like you have not added the SelectCount method to your ObjectDataSource.
This is required for the ObjectDatasource to correctly page your data.
Your select method needs the int parameters maximumRows and startRowIndex This is the default parameter names required for paging. if you do have them in. Please use them to filter your data.

Conditional HyperLink in GridView?

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).

When i Check the Checkbox how do I change that specific row style? C# ASP.NET

So i got this gridview on ASP.NET, it is about a gmail client, i made a template field with a check box that is for select several message at the same time, but first when you select a message i want to change the row background color, I already have my CSS but i dont know how to trigger the action or how to know what row was selected in the code. Here is the template of my grid:
<asp:GridView ID="inboxGrid" runat="server"
AutoGenerateColumns="false" CssClass="inbox" ShowHeader="false" BorderStyle="None" GridLines="None">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
<ItemStyle Width="24" CssClass="check"/>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="from" />
<ItemTemplate>
<asp:Label ID="lblFrom" runat="server"
Text = '<%# Eval("From") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="subject" />
<ItemTemplate>
<asp:Label ID="lblSubject" runat="server" Text='<%# Eval("Subject") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="date" />
<ItemTemplate>
<asp:Label ID="lblFecha" runat="server" Text='<%# Eval("Fecha") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle CssClass="inbox"/>
</asp:GridView>
Here is a solution using JQuery
Provided you add a CssClass named chk to asp:checkbox
<asp:CheckBox ID="chkSelect" runat="server" CssClass="chk" />
Here is the script
$(document).ready(function () {
$(".chk :checkbox").live("click", function () {
$(this).closest("tr").css("background-color", this.checked ? "#0000FF" : "");
});
});
Try this:
<asp:CheckBox ID="chkSelect" runat="server" OnClick="changeCss(this.id);" />
You can use toggleClass('CssClassName') to toggle the class on checkbox clicks:
function changeCss(ctrlId){
$(ctrlId).parent().parent().toggleClass('check');
}
The hierarchy is tr > td > checkbox, we go reverse to change the css of tr/row.

Categories