Limit GridView Columns - asp.net

I have a gridview which is displaying products information (code below)
now I would like to limit description field so that only the first few is shown if there is a long description and also resize the image to fit in the image column (and making the image, description column a bit bigger than the others
Thanks
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductId" OnRowDeleting="gvProducts_RowDeleting" OnSelectedIndexChanged="gvProducts_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Productname" HeaderText="Name" />
<asp:BoundField DataField="ProductDescription" HeaderText="Description" />
<asp:BoundField DataField="ProductSupplier" HeaderText="Supplier" />
<asp:BoundField DataField="ProductCategory" HeaderText="Category" />
<asp:BoundField DataField="ProductSubCategory" HeaderText="SubCategory" />
<asp:BoundField DataField="ProductVAT" HeaderText="VAT" />
<asp:BoundField DataField="ProductStock" HeaderText="Stock" />
<asp:ImageField DataImageUrlField="ProductImage" HeaderText="Image"/>
<asp:CheckBoxField DataField="ProductActive" HeaderText="Active" />
<asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
</Columns>
<SelectedRowStyle CssClass="selectedrow" />
</asp:GridView>

Using css we can limit the description text to be displayed. Like this....
<asp:GridView ID="gvProducts" runat="server" CssClass="GridCSS" AutoGenerateColumns="False" DataKeyNames="ProductId" OnRowDeleting="gvProducts_RowDeleting" OnSelectedIndexChanged="gvProducts_SelectedIndexChanged">
<Columns>
-----------------------
-----------
---------
<asp:BoundField DataField="ProductDescription" HeaderText="Description" ItemStyle-CssClass="limittext" />
------------------
---------
-------------
</Columns>
</asp:GridView>
CSS style:
.GridCSS {
table-layout:fixed;
width:100%;
}
.GridCSS .limittext{
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

You can use mask for fw 4.5 or some external pluging lika Jquery Mask.
or You can make them Template Item and put some code like <%# Eval("ProductDescription").SubString(0,50) %>

If you're working with MVC, you can alter your GridView's data in your controller before binding it to GW. If you're not using MVC, the only thing I can offer is: in your RowDataBound event, check if description exceeds your defined max. chars and then truncate it.
Not a proper answer I know, but it may give you a few ideas. Can't provide code right now as I'm at work, but I'll check later if there is no answer to your post.
For further reading:
http://www.dotnetspider.com/resources/43687-How-show-formatted-html-content-grid-view.aspx
http://forums.asp.net/t/1391922.aspx/1

Try this
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<%# Eval("ProductDescription").ToString().Length > 50 ? Eval("ProductDescription").ToString().Substring(0,50)+"..." : Eval("ProductDescription").ToString() %>
</ItemTemplate>
</asp:TemplateField>
If text is more than 50 characters, display first 50, otherwise, display the whole text.
Another option is return a shortDescription from the database or from the Product object
public string ShortDescription
{
get
{
return ProductDescription.Length > 50 ? ProductDescription.Substring(0,50) : ProductDescription;
}
}

<table style="table-layout: fixed">
<tr>
<td>
<telerik:RadGrid ID="radGVEmployee" runat="server" AllowSorting="True" GridLines="None"
AutoGenerateColumns="False" CellSpacing="0" OnNeedDataSource="radGVEmployee_NeedDataSource"
Visible="True" OnSortCommand="radGVEmployee_SortCommand" AllowPaging="true" OnPageIndexChanged="radGVEmployee_PageIndexChanged"
Width="900px" OnItemDataBound="radGVEmployee_ItemDataBound" OnItemCommand="radGVEmployee_ItemCommand"
PageSize="5">
<FilterMenu EnableTheming="True">
<CollapseAnimation Duration="200" Type="OutQuint" />
</FilterMenu>
<GroupingSettings CaseSensitive="False" />
<MasterTableView DataKeyNames="UsrId" AllowFilteringByColumn="true" TableLayout="Fixed"
Visible="true" AllowMultiColumnSorting="false">
<NoRecordsTemplate>
<div style="width: 700px;" align="center">
There is No Data to display</div>
</NoRecordsTemplate>
<CommandItemSettings ExportToPdfText="Export to PDF" />
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
</RowIndicatorColumn>
<ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
</ExpandCollapseColumn>
<Columns>
<telerik:GridTemplateColumn SortExpression="UserName" DataField="UserName" HeaderText="UserName"
HeaderStyle-Width="60px" FilterControlWidth="75%" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblItmUserName" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Wrap="false"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" />
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn SortExpression="FullName" DataField="FullName" HeaderText="FullName"
HeaderStyle-Width="60px" FilterControlWidth="75%" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblItmFullName" runat="server" Text='<%# Eval("FullName") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Wrap="false"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" />
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn SortExpression="MobileNo" DataField="MobileNo" HeaderText="MobileNo"
HeaderStyle-Width="40px" FilterControlWidth="75%" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblItmMobileNo" runat="server" Text='<%# Eval("MobileNo") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Wrap="false"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" />
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn SortExpression="CompanyName" DataField="CompanyName"
HeaderText="CompanyName" HeaderStyle-Width="50px" FilterControlWidth="75%" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblItmCompanyName" runat="server" Text='<%# Eval("CompanyName") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Wrap="false"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" />
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn SortExpression="BranchName" DataField="BranchName" HeaderText="BranchName"
HeaderStyle-Width="50px" FilterControlWidth="75%" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblItmBranchName" runat="server" Text='<%# Eval("BranchName") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Wrap="false"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" />
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn SortExpression="Designation" DataField="Designation"
HeaderText="Designation" HeaderStyle-Width="50px" FilterControlWidth="75%" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblItmDesignation" runat="server" Text='<%# Eval("Designation") %>'></asp:Label>
<asp:Label ID="lblItmLockStatus" runat="server" Text='<%# Eval("LockStatus") %>'
Visible="false"></asp:Label>
<asp:Label ID="lblItmUserStatus" runat="server" Text='<%# Eval("UserStatus") %>'
Visible="false"></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Wrap="false"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" />
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn SortExpression="UserImage" DataField="UserImage" HeaderText="UserImage"
HeaderStyle-Width="50px" FilterControlWidth="75%" HeaderStyle-HorizontalAlign="Center"
AllowFiltering="false">
<ItemTemplate>
<asp:Image ID="lblUserImage" runat="server" ImageUrl='<%#Eval("ImageName")!=DBNull.Value?
System.Configuration.ConfigurationManager.AppSettings["AzureImagePath"]+Eval("ImageName"):System.Configuration.ConfigurationManager.AppSettings["UserImagePath"]+"profile-empty-head.gif"%>'
Height="100px" Width="90px" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Wrap="false"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" />
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn AllowFiltering="false" DataField="Actions" HeaderText="Actions"
HeaderStyle-Width="50px" HeaderStyle-HorizontalAlign="Center">
<%--OnClientClick='radconfirm("Are you sure you want to Change this LockStatus?", ClientGridCallback,300,150); return false;' --%>
<ItemTemplate>
<asp:ImageButton runat="server" ID="imgLockStatus" ImageUrl="~/App_Themes/DefaultClient/images/unlock.png"
CommandName="ChangeLockStatus" CommandArgument='<%# Eval("UsrId") %>' ToolTip="UserLockStatus"
AlternateText="LockStatus" OnClientClick="return blockConfirm('Are you sure you want to Change Lock Status?', event, 340, 150,'','Change Lock Status')" />
<asp:ImageButton runat="server" ID="imgUserStatus" ImageUrl="~/App_Themes/DefaultClient/images/select.png"
CommandName="ChangeUserStatus" CommandArgument='<%# Eval("UsrId") %>' ToolTip="UserActivationStatus"
AlternateText="UserActivationStatus" OnClientClick="return blockConfirm('Are you sure you want to Change Activation Status?', event, 340, 150,'','Change Activation Status')" />
<asp:ImageButton runat="server" ID="imgResetPassword" ImageUrl="~/App_Themes/DefaultClient/images/logoff.png"
CommandName="ResetPassword" CommandArgument='<%# Eval("UsrId") %>' ToolTip="ResetPassword"
Width="22px" Height="22px" AlternateText="ResetPassword" OnClientClick="return blockConfirm('Are you sure you want to Reset Password?', event, 340, 150,'','Reset Password')" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Wrap="false"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" />
</telerik:GridTemplateColumn>
<%-- <telerik:GridButtonColumn ConfirmText="Are you sure you want to Change LockStatus ?<br /><br /><br />"
ConfirmDialogType="RadWindow" HeaderStyle-Width="15px" ConfirmDialogHeight="150px"
HeaderText="LockStatus" HeaderStyle-HorizontalAlign="Center" CommandName="ChangeLockStatus"
ButtonType="ImageButton" ImageUrl="~/App_Themes/DefaultClient/images/unlock.png"
ItemStyle-HorizontalAlign="Center" ConfirmTitle="Delete Currency" UniqueName="ChangeLockStatus">
</telerik:GridButtonColumn>
<telerik:GridButtonColumn ConfirmText="Are you sure you want to Change ActivationStatus ?<br /><br /><br />"
ConfirmDialogType="RadWindow" HeaderStyle-Width="15px" ConfirmDialogHeight="150px"
HeaderText="ActivationStatus" HeaderStyle-HorizontalAlign="Center" CommandName="ChangeUserStatus"
ButtonType="ImageButton" ImageUrl="~/App_Themes/DefaultClient/images/unlock.png"
ItemStyle-HorizontalAlign="Center" ConfirmTitle="Delete Currency" UniqueName="ChangeUserStatus">
</telerik:GridButtonColumn>--%>
</Columns>
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column">
</EditColumn>
</EditFormSettings>
</MasterTableView>
<GroupingSettings CaseSensitive="False" />
<ClientSettings>
<ClientEvents OnGridCreated="GridCreated" />
</ClientSettings>
<PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
</telerik:RadGrid>
</td>
</tr>
</table>
Try this ..........

Related

Header Column of Gridview not aligning

The last header column of my gridview isnt aligning for some reason, im unsure why, any help?
<asp:GridView ID="gvStudents" runat="server" AutoGenerateColumns="False"
DataSourceID="SQLGetStudents" Width="100%" CssClass="table table-striped table-hover">
<alternatingrowstyle backcolor="#d0e6f4" forecolor="#545454"/>
<Columns>
<asp:BoundField DataField="PERSON_CODE" HeaderText="Student ID"
SortExpression="PERSON_CODE" >
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="STUDENTNAME" HeaderText="Student Name"
SortExpression="STUDENTNAME" >
<HeaderStyle Font-Bold="True" Font-Size="Larger" />
<ItemStyle Font-Bold="True" Font-Size="Larger" />
</asp:BoundField>
<asp:TemplateField HeaderStyle-HorizontalAlign="Center">
<HeaderTemplate>
<asp:Label runat="server" >Add</asp:Label>
<br />
<asp:CheckBox ID="chkAll" runat="server" onclick = "checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkAdd" runat="server" Checked="True" onclick = "Check_Click(this)"/>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
The particular part of my gridview that doesnt seem to be working is here:
<asp:TemplateField HeaderStyle-HorizontalAlign="Center">
<HeaderTemplate>
<asp:Label runat="server" >Add</asp:Label>
<br />
<asp:CheckBox ID="chkAll" runat="server" onclick = "checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkAdd" runat="server" Checked="True" onclick = "Check_Click(this)"/>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
but i thought id add all of the gridview just in case i messed up elsewhere
The Column shows up like this:

Error when trying to retain updatepanel values

I'm having a problem with a couple of dropdown boxes in my gridview losing their values upon postback.
Everything is within an update panel so I have tried to include into my code but this just throws an exception. See the below code, I have a child gridview within a larger one:
I am receiving the following exception when I go to load the gridview: A control with ID 'DropDownNote' could not be found for the trigger in UpdatePanel 'UpdateGV'.
I followed what was in this post: Control in UpdatePanel loses value
But now I'm getting the above exception, what am I missing here? Will solving this problem retain the values of the dropdowns after the postback?
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
</asp:ScriptManager>
<asp:GridView ID="GVAccounts" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333"
GridLines="Horizontal"
style="position: relative; margin-top: 10px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="branch" HeaderText="Branch"
SortExpression="branch" />
<asp:BoundField DataField="no" HeaderText="Account"
SortExpression="account" />
<asp:TemplateField HeaderText="Name" SortExpression="name">
<EditItemTemplate>
<asp:TextBox ID="TextName" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelName" runat="server" Text='<%# Eval("name") + " " + Eval("surname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="type" HeaderText="Type" SortExpression="type" />
<asp:BoundField DataField="sub" HeaderText="Sub" SortExpression="sub" />
<asp:BoundField DataField="TotalAmount" HeaderText="Hold"
SortExpression="TotalAmount" />
<asp:BoundField DataField="loc" HeaderText="LOC" DataFormatString="{0:C}" SortExpression="loc" />
<asp:BoundField DataField="locstatus" HeaderText="LOC status"
SortExpression="locstatus" />
<asp:BoundField DataField="HoldCalc" HeaderText="OD/EX Amt" SortExpression="HoldCalc" />
<asp:BoundField DataField="odtimes" HeaderText="#OD" SortExpression="odtimes" />
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id="gridChild" style="display: inline; position: relative; left: 15px; overflow: auto">
<asp:UpdatePanel ID="UpdateGV" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false"
BorderStyle="Double" BorderColor="#5D7B9D" Width="80%">
<HeaderStyle BackColor="#5D7B9D" Font-Bold="true" ForeColor="White" />
<RowStyle BackColor="#E1E1E1" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true"
oncheckedchanged="chkSelect_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="effective" HeaderText="Effective" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="desc_" HeaderText="Desc" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="code" HeaderText="TC" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="amount" HeaderText="Amount" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="tracer" HeaderText="Cheq #" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="empltype" HeaderText="Empl" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="balance" HeaderText="OD/EXT Amt" HeaderStyle-HorizontalAlign="Left" />
<asp:TemplateField HeaderText="Note">
<ItemTemplate>
<asp:DropDownList ID="DropDownNote" runat="server"
onselectedindexchanged="DropDownNote_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem>MWC</asp:ListItem>
<asp:ListItem>CBM</asp:ListItem>
<asp:ListItem>Return</asp:ListItem>
<asp:ListItem>TSF</asp:ListItem>
<asp:ListItem>OK NO S/C</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Note1">
<ItemTemplate>
<asp:DropDownList ID="ddNote2" runat="server">
<asp:ListItem Selected="True">NSF</asp:ListItem>
<asp:ListItem>Funds Not Clear</asp:ListItem>
<asp:ListItem>Post Dated</asp:ListItem>
<asp:ListItem>Stale Dated</asp:ListItem>
<asp:ListItem>Stop Payment</asp:ListItem>
<asp:ListItem>Encoding Incorrect</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Branch">
<ItemTemplate>
<asp:DropDownList ID="ddBranch" runat="server" DataSourceID="BranchDataSource"
DataTextField="branch" DataValueField="branch">
</asp:DropDownList>
<asp:SqlDataSource ID="BranchDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ThirdPartyDataConnectionString %>"
SelectCommand="SELECT [branch] FROM [branch]"></asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Account">
<ItemTemplate>
<asp:TextBox ID="TextNo" runat="server" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:DropDownList ID="ddType" runat="server" DataSourceID="typeSource"
DataTextField="Type" DataValueField="Type">
</asp:DropDownList>
<asp:SqlDataSource ID="typeSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ThirdPartyDataConnectionString %>"
SelectCommand="SELECT [Type] FROM [DMDType]"></asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sub">
<ItemTemplate><asp:TextBox ID="TextSub" Width="25px" runat="server"></asp:TextBox></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownNote" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
I decided to go a different route with this, I added MaintainScrollPositionOnPostback="true" to the top line of the aspx, since the complaint from the client was that the page would go back to the top after every postback. I had originally used the UpdatePanel to remedy this but decided this would be a better option.
Now my only problem is getting the page to retain the values of the dropdown boxes after a postback which I will have to figure out how that can be done.

sum asp unbound gridview column

Please I would like to get the sum or total of my total column values in the txtFootTotal text box. I can already multiply quantity and price and the value is shown in the total column so now i need to sum the total values and show the same in the textbox at the footer. Please note that my gridview textboxes are not calling back data from the database as they are unbound.
Here is my gridview
<asp:GridView ID="grvbill" runat="server"
ShowFooter="True" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333"
GridLines="None" >
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="SNo" />
<asp:TemplateField HeaderText="quantity">
<ItemTemplate>
<asp:TextBox ID="qty" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="unit price">
<ItemTemplate>
<asp:TextBox ID="price" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="all total">
<ItemTemplate>
<asp:TextBox ID="total" runat="server"
Height="55px" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:TextBox ID="txtFootTotal" runat="server" Width="100px" Wrap="true" style="position:relative; right:500px;"></asp:TextBox>
<asp:Button ID="ButtonAdd" runat="server"
Text="Add New Row" OnClick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="" Font-Bold="True" ForeColor="" />
<RowStyle BackColor="" />
<EditRowStyle BackColor="" />
<SelectedRowStyle BackColor="" Font-Bold="True" ForeColor="" />
<PagerStyle BackColor="" ForeColor="" HorizontalAlign="Center" />
<HeaderStyle BackColor="" Font-Bold="true" ForeColor="" />
<AlternatingRowStyle BackColor="" />
</asp:GridView>

HtmlEncode not working in asp.net

I having a problem to make HtmlEncode work properly inside a GridView in ASP.Net. Basically I'm using TemplateField to toggle ItemTemplate and EditItemTemplate to have data entry on the GridView itself. I'm looking for some resource in how I can encode Eval and Bind method in ASP.Net but all of them not work in my end.
here's my code below:
<div id="dvShowContent" runat="server" style="text-align: center">
<asp:GridView ID="dgvSortKey" runat="server" AllowSorting="True" OnRowDataBound="gv_drb"
AutoGenerateColumns="False" AllowPaging="True" BackColor="White" BorderColor="#336666"
BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal"
Height="73px" AutoGenerateEditButton="True" OnRowEditing="dgvSortKey_RowEditing"
OnRowUpdating="dgvSortKey_RowUpdating" OnRowCancelingEdit="dgvSortKey_RowCancelingEdit"
OnSelectedIndexChanging="dgvSortKey_SelectedIndexChanging" OnPageIndexChanged="dgvSortKey_PageIndexChanged"
OnPageIndexChanging="dgvSortKey_PageIndexChanging" OnRowCommand="dgvSortKey_RowCommand"
OnRowDeleted="dgvSortKey_RowDeleted" OnRowUpdated="dgvSortKey_RowUpdated" Width="561px"
PageSize="15" DataKeyNames="KeyCode,KeyDescription">
<FooterStyle BackColor="White" ForeColor="#333333" />
<RowStyle BackColor="White" ForeColor="#333333" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="LightCyan" />
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="lnkdelete" runat="server" OnClick="lnkdelete_Click">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Instruction Key Code">
<ItemTemplate>
<asp:Label ID="lblValKeyCode" runat="server" Text='<%# Server.HtmlEncode((string)Eval("KeyCode")) %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtValKeyCode" runat="server" Text='<%#Bind("KeyCode") %>' MaxLength="10"></asp:TextBox>
<%--<asp:TextBox ID="txtValKeyCode" runat="server" Text='<%#System.Web.HttpUtility.HtmlEncode(Eval("KeyCode").ToString()) %>' MaxLength="10"></asp:TextBox>--%>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="GvBorderGreen" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblValKeyDescription" runat="server" Text='<%# Server.HtmlEncode((string)Eval("KeyDescription")) %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtValKeyDescription" runat="server" Text='<%#Bind("KeyDescription") %>'
MaxLength="10"></asp:TextBox>
<%--<asp:TextBox ID="txtValKeyDescription" runat="server" Text='<%#System.Web.HttpUtility.HtmlEncode(Eval("KeyDescription").ToString()) %>'
Width="300" MaxLength="20"></asp:TextBox>--%>
</EditItemTemplate>
<ItemStyle CssClass="GvBorderGreen" />
</asp:TemplateField>
<%--<asp:BoundField DataField="KeyCode" HeaderText="Instruction Key Code" HtmlEncode="true" />
<asp:BoundField DataField="KeyDescription" HeaderText="Description" HtmlEncode="true" />--%>
</Columns>
</asp:GridView>
</div>
Please help me to solve this problem.
As Waqar suggests the answer is in the link he provided - to make this more clear you need to change this line:
<asp:TextBox ID="txtValKeyCode" runat="server" Text='<%#Bind("KeyCode") %>' MaxLength="10"></asp:TextBox>
To this:
<asp:TextBox ID="txtValKeyCode" runat="server" Text='<%#System.Web.HttpUtility.HtmlEncode((string)Eval("KeyCode")) %>' MaxLength="10"></asp:TextBox>

Gridview does not display

The gridview on my page is set up with templat fields in each column so that I can enter information and save it to my database. There is no data that ties directly into the gridview just the ability to add multiple rows at a time to save the data. I had it displayed with empty textboxes and it was working fine and all of a it stopped and now won't show up. Any clues as to what is happening. Code follows...
<asp:GridView ID="gvOLIAdj" runat="server" AutoGenerateColumns="False" CssClass="td8" CellPadding="4" ForeColor="#333333" PageSize="5" ViewStateMode="Enabled">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Approval Date ">
<EditItemTemplate>
<asp:Label ID="Label5" runat="server"></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txtAdjAppr" runat="server" CausesValidation="True"></asp:TextBox>
<asp:MaskedEditExtender ID="txtAdjApprt_MaskedEditExtender" runat="server" ClearMaskOnLostFocus="False" ClipboardEnabled="False" Mask="99/99/99" TargetControlID="txtAdjAppr" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="125px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Amount">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server"></asp:Label> </EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txtAdjAmt" runat="server" CausesValidation="True"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" Width="225px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Comments">
<EditItemTemplate>
<asp:Label ID="Label3" runat="server"></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txtCmmts" runat="server" Width="599" CausesValidation="True"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:Label ID="Label4" runat="server"></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblInitials" runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="35px" />
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#003399" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Potential causes:
You are not setting to edit mode, gvOLIAdj.EditIndex = 1;
CssClass="td8" is set to display:none/visibility:hidden
gvOLIAdj.Visible=False is
being set
You upgraded your framework, see below
GridView sets EditIndex property when in previous .NET versions it didn't
Gridviews must be bound to a datasource of somesort, the view you see in the designer is only an estimation of what the gridview will look like.

Resources