RadGrid - How do you change HeaderText of Bound field? - asp.net

I am using a RadGrid and I had the autogenerated set to true. This results in a column header text of the the database field cap_name. In an ASP.NET GridView you would change it like (after setting autogeneratedcolumns to false:
<Columns>
<asp:BoundField DataField="cap_name" HeaderText="Capability"
SortExpression="cap_name" />
</Columns>
I set autogenerated colums to false and tried to use BoundField but the compiler said to use telerik:GridColumn. How do I use this to get a similar result?

Use <rad:GridBoundColumn HeaderText="Capability" UniqueName="ClmCapability" DataField="cap_name" /> .
NOTE : replace <rad: with the prefix you use for your RadGrid control when you register it. You can find it as TagPrefix in :
<%# Register Assembly="RadGrid.Net2" Namespace="Telerik.WebControls" TagPrefix="rad" %>
You can't use asp:boundfield because it is for asp.net gridview, while you are using telerik radgrid
EDIT :
<rad:RadGrid ID="MyRadGrid" runat="server" EnableAJAXLoadingTemplate="true"
AutoGenerateColumns="false" AllowFilteringByColumn="True" AllowPaging="True"
AllowSorting="True" GridLines="None" Width="60%" EnableAJAX="True" OnNeedDataSource="MyRadGrid_NeedDataSource">
<ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True">
</ClientSettings>
<MasterTableView RetrieveAllDataFields="false" AdditionalDataFieldNames="Name">
<PagerStyle AlwaysVisible="false" Mode="NextPrevNumericAndAdvanced" ShowPagerText="true" />
<Columns>
<rad:GridBoundColumn HeaderText="Capability" UniqueName="ClmCapability" DataField="cap_name" />
</Columns>
</MasterTableView>
</rad:RadGrid>

Related

Dynamic population in gridview

I have a grid view, in which I have 1 column as template field, while other 8 columns are dynamically created.
This template field has a checkbox, while trying to retrieve this checkbox in code-behind file, we get it as null.
When columns are not dynamically created, the checkbox is pretty fine and has a value in code behind.
Below is my code:
<asp:GridView ID="gridResultSet" runat="server" AutoGenerateColumns="false" AllowSorting="true"
OnRowCreated="GridResultSet_RowCreated" OnLoad="GridResultSet_Load" CssClass="reportGrid"
CellPadding="4" OnSorting="GridResultSet_Sorting" OnRowDataBound="GridResultSet_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Reclass">
<ItemTemplate>
<acesec:CheckBox ID="chkReclass" CssClass="CheckBoxListStyle" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle CssClass="gridrow" />
<AlternatingRowStyle CssClass="gridrow" />
<HeaderStyle CssClass="gridheader" />
</asp:GridView>
System.Web.UI.WebControls.CheckBox chbReclass = gridResultSet.Rows[i].FindControl("chkReclass") as System.Web.UI.WebControls.CheckBox;
Do I need to check something in order to access a template field while creation of dynamic columns?
Any body faced same kind of situation?
Pointers will be highly appreciated.
Regards

Working with ASP.Net GridView sorting and paging

We set up an ASP.Net GridView and included sorting and paging. When the user clicks on the GridView column header links for sorting the data or if the user clicks on the numbers links at the bottom of the GridView for paging the data, nothing happens.
Here is a cut down version of the markup for the GridView:
<asp:UpdatePanel
ID="UpdatePanelSummary"
runat="server"
UpdateMode="Always">
<ContentTemplate>
<h1>Maintenance</h1>
<% '-- GridView (Grid) for summary. -- %>
<% '-- The user chooses a summary row from here and details are shown in a DetailsView. -- %>
<% '--------------------------------------------------------------------------------------- %>
<asp:GridView
ID="GridViewSummary"
runat="server"
AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="ID"
Width="224px"
AllowPaging="True"
PageSize="7">
<Columns>
<asp:BoundField DataField="Unit" HeaderText="Unit"
SortExpression="Unit" />
<asp:BoundField DataField="TheName" HeaderText="Name"
SortExpression="TheName" />
<asp:BoundField DataField="ID"
HeaderText="ID" SortExpression="ID" InsertVisible="False" ReadOnly="True"
Visible="False" />
<asp:CommandField ButtonType="Button" SelectText="Select Unit Details"
ShowSelectButton="True" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Since nothing is happening, we are assuming we need to write some coding in the code-behind file. Can you show us what coding is needed to wake up the sorting and paging?
Check out this post
sorting and paging with gridview asp.net
Basically you need to add server side event handlers for sorting and paging.
Here is an example - you can copy/paste most of it.
http://www.dotnetspider.com/resources/1249-Grid-View-Paging-Sorting.aspx

Hyperlink in datagrid view

I want to set hyperlink field in datagrid view. When user clicks on that link, a query string should be generated and user should be directed to another page. So how can I set hyperlink to generate query string?
<asp:GridView ID="Griddata" runat="server" AutoGenerateColumns="False" CellPadding="1"
GridLines="Horizontal" Width="1000px" ShowFooter="True" CssClass="grid" AlternatingRowStyle-CssClass="alt">
<Columns>
<asp:HyperLinkField HeaderText="ID" DataTextField="rec_id" DataNavigateUrlFields="rec_id"
DataNavigateUrlFormatString="followme.aspx?record={0} " />
<asp:BoundField HeaderText="Login" DataField="LoginName"></asp:BoundField>
</Columns>
</asp:GridView>
This is a sample GridView defined in ASP.NET
You need to specify the <asp:Hyperlinkfield> in the column definition.
In that field, you need to specify the DataTextfield (is what will be displayed on screen in that column), your URL (DataNavigateUrlFormatString) and your parameter that you want to use in that URL (DataNavigateUrlFields)
Note: I'm binding to this grid from code-behind, not through a SqlDatAdaptor but the result is the same.
You will get something like this:
you can do like...
<ItemTemplate>
<asp:HyperLink ID="Edit" runat="server" Text="Edit" NavigateUrl='<%# Eval("DataKeyName", "~/View.aspx?Id={0}") %>' />
</ItemTemplate>
<a href='page.aspx?id=<#Eval("ID")>'>click</a>

I cannot make the headers of my gridview click able for sorting

I have a gridview that I populate based on a query to a db. I'm trying to add sorting on my gridview but I cannot make my column headers clickable. I have allow sorting set to true, and I have my OnSorting event set. My columns are of a few different types. I know the code I need to have in my code behind, but I cannot click on the headers for some reason. Any help on what I'm missing would be appreciated.
<asp:GridView ID="Grid1" runat="server"
AutoGenerateColumns="False"
OnSelectedIndexChanging="Selected_Row_Changing"
DataKeyNames="ApplicationId"
AllowPaging="True"
OnPageIndexChanging="Grid1_PageIndexChanging"
AllowSorting="True"
OnSorting="Grid1_Sorting"
OnRowCreated="OnRowCreated"
OnRowCommand="Grid1_RowCommand"
OnRowDataBound="Grid1_RowDataBound">
<Columns>
<asp:templatefield ...>
<itemtemplate>
<asp:linkbutton .../>
</itemtemplate>
</asp:templatefield>
<asp:BoundField ... />
<asp:HyperLinkField ... />
<asp:ButtonField ... />
</Columns>
</asp:GridView>
You don't have set the SortExpression, have you?
For example:
<asp:boundfield datafield="CompanyName"
headertext="CompanyName"
headerstyle-wrap="false"
sortexpression="CompanyName"/>
Make sure you're not setting the Header Template but rather set the HeaderText attribute for the TemplateField

Adding a validator to the gridview textbox, created in edit-mode of a bound field

take a look at this sample code: (question bellow)
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="SqlDataSource2"
AutoGenerateColumns="False" onrowupdated="GridView1_RowUpdated"
DataKeyNames="Product_Id">
<Columns>
<asp:ImageField DataImageUrlField="Image_Name" HeaderText="Image_Name"
ReadOnly="True" >
<ItemStyle Width="50px" Height="50px" Wrap="true"/>
</asp:ImageField>
<asp:BoundField DataField="Product_Id" HeaderText="Product_Id"
InsertVisible="False" ReadOnly="True" SortExpression="Product_Id">
</asp:BoundField>
<asp:BoundField DataField="Product_Name" HeaderText="Product_Name"
SortExpression="Product_Name" />
<asp:BoundField DataField="Category_Name" HeaderText="Category_Name"
SortExpression="Category_Name" ReadOnly="true" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Size" HeaderText="Size"
SortExpression="Size" />
<asp:BoundField DataField="Price" HeaderText="Price"
SortExpression="Price" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
Assume I initialize an SqlDataSource, add a parameter and so on.
The thing is, that when a user clicks edit we get a textbox to edit the colnumn value.
I want to validate the data enter by the user before the update is performed and the new
data is propagated back to the server.How?
10x a lot!
You need to convert the BoundField into a TemplateField. Then you can add a validator to the actual TextBox control.
Option 1:
But from the UNKNOWN answer, the Microsoft recommends the same.. as he told.
ref: http://msdn.microsoft.com/en-us/library/bb426882.aspx#aspnett19_vldcntredtuics_topic2
Option 2:
But, we can do.
You need to add the validation either the javascript validation or server side validation
control, when the GridView's DataBound event is happening at runtime on the particular
TableCell of the Gridview rows.
Hence, when you click the update button that custom generated javascript or the validation
control will check for the validation on editing the values.
This process is more harder than the converting boundfield to templatefield
refer: http://www.aspdotnetcodes.com/GridView_Dynamic_Validation.aspx
Option 3:
And you can go for server side validation on the values instead of client side validation:
refer: http://msdn.microsoft.com/en-us/library/bb332383.aspx

Resources