I try using Rad Grid with web Api as data source
<telerik:RadGrid runat="server" ID="grdUsers" AllowPaging="true" AllowSorting="true"
AllowFilteringByColumn="true" PageSize="5">
<MasterTableView AutoGenerateColumns="False" DataKeyNames="Id" ClientDataKeyNames="Id,PasswordHash">
<PagerStyle Mode="NumericPages" AlwaysVisible="true" />
<Columns>
<telerik:GridImageColumn DataType="System.String" DataImageUrlFields="Image" AlternateText="User image"
UniqueName="Image"
ImageAlign="Middle" ImageHeight="50px" ImageWidth="50px" AllowFiltering="false" HeaderText="">
</telerik:GridImageColumn>
<telerik:GridBoundColumn DataField="UserName" HeaderText="User Name" UniqueName="UserName"
DataType="System.String">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="FullName" HeaderText="Name" UniqueName="FullName"
DataType="System.String">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Email" HeaderText="Email" UniqueName="Email"
DataType="System.String">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="RegistrationDate" HeaderText="Registration Date" UniqueName="RegistrationDate"
DataFormatString="{0:dd/MM/yyyy}">
</telerik:GridBoundColumn>
<telerik:GridButtonColumn UniqueName="btnEdit" ButtonType="PushButton" Text="Edit" CommandName="Edit"></telerik:GridButtonColumn>
<telerik:GridButtonColumn UniqueName="btnDelete" ButtonCssClass="del" ButtonType="PushButton" Text="Delete" CommandName="Delete"></telerik:GridButtonColumn>
</Columns>
</MasterTableView>
<ClientSettings>
<Selecting AllowRowSelect="True" />
<ClientEvents OnCommand="RadGridCommand" />
<DataBinding Location="/SecuHostapi/Security/User/GetAll" ResponseType="JSON">
<DataService TableName="SecuHostUser" Type="OData" />
</DataBinding>
</ClientSettings>
</telerik:RadGrid>
Wep Api code
[HttpGet]
public List<SecuHostUser> GetAll(ODataQueryOptions<SecuHostUser> options)
{
UsersRep userRep = new UsersRep();
return userRep.GetAll();
}
although, I have put PageSize="5", the final result show all the data from the data base
This problem can be solved by returning a PageResult instead of a list.
[HttpGet]
public PageResult GetAll(ODataQueryOptions<SecuHostUser> options)
{
UsersRep userRep = new UsersRep();
//return userRep.GetAll();
var users = userRep.GetAll().AsQueryable();
var results = options.ApplyTo(users);
return new PageResult<SecuHostUser>(results as IEnumerable<SecuHostUser>,
Request.GetNextPageLink(), Request.GetInlineCount());
}
Related
I have the following in my .aspx file. When the user clicks on Edit, I like it to show a form where they can Update or Cancel. Currently when I click on Edit, it does nothing. Note that am handling the datasource from the code behind. As such, I do not have a DataSource within the .aspx file for the RadGrid:
Here is my code:
<telerik:RadGrid ID="rdClosedLoop" runat="server" GridLines="None" OnDeleteCommand="DeleteClosedLoop"
Skin="Metro" ActiveItemStyle-HorizontalAlign="Center" AutoGenerateEditColumn="true" >
<MasterTableView EditMode="EditForms" AutoGenerateColumns="False" DataKeyNames="ID" >
<RowIndicatorColumn>
<HeaderStyle Width="20px" />
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px" />
</ExpandCollapseColumn>
<Columns>
<telerik:GridBoundColumn DataField="ID" HeaderText="ID" SortExpression="ID" EditFormColumnIndex="0"
UniqueName="ID" HeaderStyle-HorizontalAlign="Center">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="ActiveInhibitor" HeaderText="ActiveInhibitor" SortExpression="ActiveInhibitor" EditFormColumnIndex="0"
UniqueName="ActiveInhibitor" HeaderStyle-HorizontalAlign="Center">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Conductivity" HeaderText="Conductivity" SortExpression="Conductivity" EditFormColumnIndex="0"
UniqueName="Conductivity" HeaderStyle-HorizontalAlign="Center">
</telerik:GridBoundColumn>
<telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn">
</telerik:GridButtonColumn>
</Columns>
<EditFormSettings ColumnNumber="1" CaptionFormatString="Edit for ID {0}"
CaptionDataField="ID">
<FormTableItemStyle Wrap="False"></FormTableItemStyle>
<FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle>
<FormMainTableStyle GridLines="None" CellSpacing="0" CellPadding="3" Width="100%">
</FormMainTableStyle>
<FormTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="2" CssClass="module"
Height="110px" Width="100%"></FormTableStyle>
<FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>
<FormStyle Width="100%" BackColor="#eef2ea"></FormStyle>
<EditColumn UpdateText="Update record" UniqueName="EditCommandColumn1" CancelText="Cancel edit">
</EditColumn>
<FormTableButtonRowStyle HorizontalAlign="Left" CssClass="EditFormButtonRow"></FormTableButtonRowStyle>
</EditFormSettings>
</MasterTableView>
<HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default" EnableImageSprites="True">
</HeaderContextMenu>
</telerik:RadGrid>
Code behind:
protected void btnClosedLoop_Click(object sender, EventArgs e)
{
// Note - verId is based on some conditions
var resultCLList = (from ms in db.ver_ClosedLoop
where ms.VerificationId == verId
select ms).ToList();
rdClosedLoop.DataSource = resultCLList;
rdClosedLoop.DataBind();
}
Please try with the below code snippet.
ASPX
<telerik:RadGrid ID="rdClosedLoop" runat="server" GridLines="None"
Skin="Metro" ActiveItemStyle-HorizontalAlign="Center" AutoGenerateEditColumn="true"
OnItemCommand="rdClosedLoop_ItemCommand">
<MasterTableView EditMode="EditForms" AutoGenerateColumns="False" DataKeyNames="ID">
<RowIndicatorColumn>
<HeaderStyle Width="20px" />
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px" />
</ExpandCollapseColumn>
<Columns>
<telerik:GridBoundColumn DataField="ID" HeaderText="ID" SortExpression="ID" EditFormColumnIndex="0"
UniqueName="ID" HeaderStyle-HorizontalAlign="Center">
</telerik:GridBoundColumn>
<telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn">
</telerik:GridButtonColumn>
</Columns>
<EditFormSettings ColumnNumber="1" CaptionFormatString="Edit for ID {0}" CaptionDataField="ID">
<FormTableItemStyle Wrap="False"></FormTableItemStyle>
<FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle>
<FormMainTableStyle GridLines="None" CellSpacing="0" CellPadding="3" Width="100%">
</FormMainTableStyle>
<FormTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="2" CssClass="module"
Height="110px" Width="100%"></FormTableStyle>
<FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>
<FormStyle Width="100%" BackColor="#eef2ea"></FormStyle>
<EditColumn UpdateText="Update record" UniqueName="EditCommandColumn1" CancelText="Cancel edit">
</EditColumn>
<FormTableButtonRowStyle HorizontalAlign="Left" CssClass="EditFormButtonRow"></FormTableButtonRowStyle>
</EditFormSettings>
</MasterTableView>
<HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default" EnableImageSprites="True">
</HeaderContextMenu>
</telerik:RadGrid>
ASPX.CS
protected void Button1_Click(object sender, EventArgs e)
{
rdClosedLoop.DataSource = getDummyData(0);
rdClosedLoop.DataBind();
}
protected DataTable getDummyData(int verId)
{
// Note - verId is based on some conditions
DataTable dt = new DataTable();
dt.Columns.Add("ID",typeof(int));
dt.Rows.Add(1);
dt.Rows.Add(2);
dt.Rows.Add(3);
return dt;
}
protected void rdClosedLoop_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.EditCommandName)
{
rdClosedLoop.DataSource = getDummyData(0);
rdClosedLoop.DataBind();
}
}
Let me know if any concern.
I'm trying to get RadGrid to conditionally hide or disable a field when it is in edit mode based on the value of another field.
I have been able to get this to work when the grid displays the list of items, but once the grid enters edit mode, the columns display ...
I am using OnItemDataBound to successfully conditionally display during the initial load, but setting the items when the user clicks a row to get it into batch mode is not working.
Note: PValue and CValue and in GridTemplateColumns, as is CardStatus.
public void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
foreach (GridDataItem item in RadGrid1.Items)
{
string BoundColumnValue = item["CardStatus"].Text; // accessing GridBoundColumn value using ColumnUniqueName
string BoundColumnValue2 = item["CValue"].Text;
TextBox txtbx = (TextBox)item.FindControl("CardStatus");
Label numlb = (Label)item.FindControl("CardValue");
if (txtbx.Text.Equals("True"))
{
txtbx.ForeColor = Color.Red;
numlb.Enabled = false;
numlb.BackColor = Color.Yellow;
numlb.ForeColor = Color.Red;
//Just testing to see if it would evaluate
}
else
{
txtbx.ForeColor = Color.Beige;
}
//string TemplateColumnValue = lb.Text;// accessing Label Text.
}
foreach (GridEditableItem item in RadGrid1.EditItems)
{
string BoundColumnValue = item["CardStatus"].Text; // accessing GridBoundColumn value using ColumnUniqueName
string BoundColumnValue2 = item["CValue"].Text;
TextBox txtbx = (TextBox)item.FindControl("CardStatus");
if (txtbx.Text.Equals("True"))
{
txtbx.ForeColor = Color.Red;
//numTxt.BackColor = Color.Yellow;
//numTxt.ForeColor = Color.Red;
}
else
{
txtbx.ForeColor = Color.Beige;
}
}
}
I just need to be able to selectively prevent data entry in a column
The ASPX source is below:
<telerik:RadGrid ID="RadGrid1" runat="server" AllowAutomaticUpdates="true" Height="930px" DataSourceID="SqlDataSource4" CellSpacing="0" GridLines="None" Width="640px" OnItemDataBound="RadGrid1_ItemDataBound" OnItemCreated="RadGrid1_ItemCreated" OnBatchEditCommand="RadGrid1_BatchEditCommand1" Skin="WebBlue">
<ClientSettings>
<Scrolling AllowScroll="True" UseStaticHeaders="True" />
</ClientSettings>
<MasterTableView AutoGenerateColumns="False" EditMode="Batch" Width="620px" CommandItemDisplay="TopAndBottom" DataSourceID="SqlDataSource4" DataKeyNames="CountKey">
<CommandItemSettings ExportToPdfText="Export to PDF" ShowSaveChangesButton="true" ShowRefreshButton="false" ShowAddNewRecordButton="false"></CommandItemSettings>
<BatchEditingSettings OpenEditingEvent="Click" EditType="Row" />
<RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
</EditFormSettings>
<PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>
<Columns>
<telerik:GridBoundColumn DataField="Pcolumn" ItemStyle-Width="75px" ReadOnly="true" Visible="false" DataType="System.Int32" FilterControlAltText="Filter Pcolumn column" HeaderText="Pcolumn" SortExpression="Pcolumn" UniqueName="Pcolumn">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Row" ReadOnly="true" Visible="false" DataType="System.Int32" FilterControlAltText="Filter Row column" HeaderText="Row" SortExpression="Row" UniqueName="Row">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn ColumnEditorID="PValue" HeaderText="Pattern" DataField="PValue" UniqueName="PValue" ItemStyle-Width="75px" HeaderStyle-Width="75px">
<EditItemTemplate>
<telerik:RadNumericTextBox ID="PValue" Width="50px" runat="server" MaxLength="1" MaxValue="9" NumberFormat-DecimalDigits="0" Text='<%# Bind("PValue") %>'></telerik:RadNumericTextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="PValue" ErrorMessage="<br/>Required (0-9) Only)!" SetFocusOnError="true"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="PValue" Width="50px" runat="server" Text='<%# Bind("PValue") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn ColumnEditorID="CValue" DataField="CValue" HeaderText="Card" UniqueName="CValue" ItemStyle-Width="75px" HeaderStyle-Width="75px">
<EditItemTemplate>
<telerik:RadNumericTextBox ID="CValue" Width="50px" AllowOutOfRangeAutoCorrect="false" runat="server" MaxLength="1" MaxValue="1" NumberFormat-DecimalDigits="0" Text='<%# Bind("CValue") %>'></telerik:RadNumericTextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="CValue" ErrorMessage="<br />Required (0-1 Only)!" SetFocusOnError="true"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="CValue" Width="50px" runat="server" Text='<%# Bind("CValue") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="DateEdited" ReadOnly="true" Visible="false" DataType="System.DateTime" FilterControlAltText="Filter DateEdited column" HeaderText="DateEdited" SortExpression="DateEdited" UniqueName="DateEdited">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn UniqueName="CardStatus" DataField="CardStatus" ItemStyle-Width="50px" HeaderStyle-Width="50px">
<ItemTemplate>
<asp:TextBox ID="CardStatus" Width="10px" runat="server" Text='<%# Bind("CardStatus") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="CardStatus" Width="10px" runat="server" Text='<%# Bind("CardStatus") %>'></asp:TextBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
<PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>
<FilterMenu EnableImageSprites="False"></FilterMenu>
</telerik:RadGrid>
Any help / workarounds would be appreciated ... again, "just" need to prevent editing in the CValue column when the CardStatus value is true (bit field) ... using batch mode (using another solution isn't an option now).
Thanks
Larry
Well, this doesn't answer the Telerik question, but ended up doing this with the MS GridView, using the tutorial here: http://msdn.microsoft.com/en-us/library/aa992036(v=vs.90).aspx.
Also was able to customize, and detect hidden grid fields, and hide cells (and disable validators) based on the hidden field's condition.
Added the following code in the RowDataBound event of the gridview:
DataRowView drv = (DataRowView)e.Row.DataItem;
if (drv["Status"].Equals(true))
{
// Find the order of the cell
e.Row.Cells[5].CssClass = "hiddencol"; // using the display: none style
// Find the Validator control in the template column
RequiredFieldValidator reqF = (RequiredFieldValidator)e.Row.Cells[5].FindControl("RequiredFieldValidator1");
reqF.Enabled = false;
}
else
{
}
I've a Telerik Radgrid displaying a list of customers. In the grid, I have a command button that's supposed to open a popup editor to add new customers. For some reason, the popup does not open. It shows a small flicker and nothing happens. Strangely enough, if I go into the database, manually add a customer record and refresh the grid, I'm able to open the editor.
Here is the grid definition in my ascx:
<telerik:RadGrid ID="CustomerList" runat="server"
GroupingEnabled="False" ShowStatusBar="True" OnPreRender="CustomerList_PreRender"
DataSourceID="CustomerDataSource"
OnItemCommand="CustomerList_ItemCommand" OnItemDataBound="CustomerList_ItemDataBound"
OnInsertCommand="CustomerList_InsertCommand" OnItemInserted="CustomerList_ItemInserted"
OnItemEvent="CustomerList_ItemEvent" >
<MasterTableView DataKeyNames="CustomerId" DataSourceID="CustomerDataSource"
AllowPaging="true" AutoGenerateColumns="False"
GridLines="None" EditMode="PopUp" ShowFooter="True"
InsertItemDisplay="Bottom" CommandItemDisplay="TopAndBottom" >
<CommandItemSettings AddNewRecordText="Add Customer" />
<Columns>
<telerik:GridBoundColumn DataField="CustomerId" DataType="System.Int32" FilterControlAltText="Filter CustomerId column" HeaderText="CustomerId" ReadOnly="True" SortExpression="CustomerId" UniqueName="CustomerId">
<ColumnValidationSettings>
<ModelErrorMessage Text="" />
</ColumnValidationSettings>
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="CustomerName" FilterControlAltText="Filter CustomerName column" HeaderText="CustomerName" SortExpression="CustomerName" UniqueName="CustomerName">
<ColumnValidationSettings>
<ModelErrorMessage Text="" />
</ColumnValidationSettings>
</telerik:GridBoundColumn>
<telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
<ItemStyle Width="20px" Font-Underline="true" />
</telerik:GridEditCommandColumn>
</Columns>
<NoRecordsTemplate>
No customers have been created yet.
</NoRecordsTemplate>
<EditFormSettings EditFormType="WebUserControl" CaptionDataField="CustomerName"
UserControlName="~/UserControls/CustomerEditor.ascx"
>
<EditColumn UniqueName="EditCommandColumn" HeaderButtonType="TextButton">
</EditColumn>
<PopUpSettings ScrollBars="None" Width="581"/>
</EditFormSettings>
<PagerStyle Mode="NextPrevAndNumeric" Position="Bottom" />
</MasterTableView>
</telerik:RadGrid>
Any help is most welcome!
I have a grid which has the CarrierID specified as the "DataKeyNames" which is also an invisible property in the grid.
I have a hyperlink column that when clicked should pass the value of the CarrierNetID (the keyvalue for that dataitem) into my codebehind so I can then pass it to another page to bring up a record for editing based on that key value.
I'm having trouble getting the attached error:
My markup is as follows: You can see that the lnkPLMN is the hyperlink. Everything is coming out on the grid just fine, I just can't seem to pass the value.
<telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" ShowStatusBar="true"
runat="server" AllowPaging="True" AllowSorting="True" AllowFilteringByColumn="True"
AutoGenerateColumns="false" ClientSettings-Resizing-AllowColumnResize="true"
DataKeyNames="CarrierNetID" OnItemDataBound="RadGrid_ItemDataBound">
<MasterTableView PageSize="10" Width="100%">
<Columns>
<telerik:GridBoundColumn DataField="CarrierNetID" Visible="false" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Name" HeaderText="Carrier Name" UniqueName="Name" SortExpression="Name" ReadOnly="true" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Region" HeaderText="Region" UniqueName="Region" AllowFiltering="true" SortExpression="Region" ReadOnly="true" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Country" HeaderText="Country" UniqueName="Country" SortExpression="Country" ReadOnly="true" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left"></telerik:GridBoundColumn>
<telerik:GridTemplateColumn HeaderText="PLMN" UniqueName="PLMN">
<ItemTemplate>
<asp:HyperLink ID="lnkPLMN" runat="server" Target="_blank" ForeColor="Blue">
</asp:HyperLink>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="TechnologyTypeCode" HeaderText="Technology" UniqueName="TechnologyTypeCode" SortExpression="TechnologyTypeCode" ReadOnly="true" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="SMSMORate" HeaderText="SSMSO Rate" UniqueName="SMSMORate" AllowFiltering="false" SortExpression="SMSMORate" ReadOnly="true" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:G}"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="SMSMTRate" HeaderText="SMSMT Rate" UniqueName="SMSMTRate" AllowFiltering="false" SortExpression="SMSMTRate" ReadOnly="true" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:G}"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="DataRate" HeaderText="Data Rate" UniqueName="DataRate" AllowFiltering="false" SortExpression="DataRate" ReadOnly="true" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:G}"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="MOCRate" HeaderText="MOC Rate" UniqueName="MOCRate" AllowFiltering="false" SortExpression="MOCRate" ReadOnly="true" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:G}"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="MTCRate" HeaderText="MTC Rate" UniqueName="MTCRate" AllowFiltering="false" SortExpression="MTCRate" ReadOnly="true" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:G}"></telerik:GridBoundColumn>
<telerik:GridCheckBoxColumn DataField="FlatRate" HeaderText="Flat Rate" UniqueName="FlatRate" SortExpression="FlatRate" ReadOnly="true" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"></telerik:GridCheckBoxColumn>
<telerik:GridCheckBoxColumn DataField="Discount" HeaderText="Discount" UniqueName="Discount" SortExpression="Discount" ReadOnly="true" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"></telerik:GridCheckBoxColumn>
<telerik:GridCheckBoxColumn DataField="ActiveFlag" HeaderText="Active" UniqueName="ActiveFlag" SortExpression="ActiveFlag" ReadOnly="true" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"></telerik:GridCheckBoxColumn>
</Columns>
</MasterTableView>
<ClientSettings EnableRowHoverStyle="true">
</ClientSettings>
<PagerStyle Mode="NumericPages"></PagerStyle>
</telerik:RadGrid>
Here is my codebehind which gets the attached error on the following line:
HyperLink link = (HyperLink)item["PLMN"].Controls[0];
protected void RadGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
HyperLink link = (HyperLink)item["PLMN"].Controls[0];
string value = item.GetDataKeyValue("CarrierNetID").ToString();
link.NavigateUrl = "~/CarrierLaunchStatusForm.aspx?addRecord=0&ID=" + value;
}
}
Can someone please help?
After placing the hyperlink in the markup (instead of the code behind), I'm getting the following compilation error:
Error 2 The GridHyperLinkColumn control with a two-way databinding to field PLMN must have an ID. C:\Projects\NPP\NPP\NPP\NPP\CarrierLaunchStatus.ascx 53
Error 3 Literal content ('</telerik:GridHyperLinkColumn>') is not allowed within a 'Telerik.Web.UI.GridColumnCollection'. C:\Projects\NPP\NPP\NPP\NPP\CarrierLaunchStatus.ascx 56
The Hyperlink column looks as follows:
<telerik:GridHyperLinkColumn UniqueName="PLMN" DataTextFormatString='<%# Bind("PLMN") %>'
DataTextField="PLMN" DataNavigateUrlFields="CarrierNetID" DataNavigateUrlFormatString="~/CarrierLaunchStatusForm.aspx?addRecord=0&ID={0}"
Target="_blank">
</telerik:GridHyperLinkColumn>
What am I missing here?
Unless you really need to modify data on the ItemDataBound event, You can just use the GridHyperLinkColumn:
<telerik:GridHyperLinkColumn DataNavigateUrlFields="CarrierNetID"
DataNavigateUrlFormatString="~/CarrierLaunchStatusForm.aspx?addRecord=0&ID={0}"
Target="_blank" Text="Your Link Text"></telerik:GridHyperLinkColumn>
I'm new to Entity Framework and I got stuck in an issue while trying to bind a navigation property (foreign key reference) to a dropdownlist. I have Telerik RadGrid control which gets the data using a EntityDataSource control. Here is the model description:
Applications: AppId, AppName, ServerId
Servers: ServerId, ServerName
The Applicaitons.ServerId is a foreign key reference to Servers.ServerId. The RadGrid lists the applications and allows the user to insert/update/delete an application. I want to show the server names as a dropdownlist in edit mode which I'm not able to. . Here is my aspx code:
<telerik:RadGrid ID="gridApplications" runat="server" Skin="Sunset"
AllowAutomaticInserts="True" AllowAutomaticDeletes="True"
AllowPaging="True" AllowAutomaticUpdates="True"
AutoGenerateColumns="False" OnItemCreated="gridApplications_ItemCreated"
DataSourceID="applicationsEntityDataSource" Width="50%"
OnItemInserted="gridApplications_ItemInserted"
OnItemUpdated="gridApplications_ItemUpdated"
OnItemDeleted="gridApplications_ItemDeleted" GridLines="None">
<MasterTableView CommandItemDisplay="Top" AutoGenerateColumns="False" DataKeyNames="AppId" DataSourceID="applicationsEntityDataSource">
<RowIndicatorColumn>
<HeaderStyle Width="20px" />
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px" />
</ExpandCollapseColumn>
<Columns>
<telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn" HeaderText="Edit" ItemStyle-Width="10%">
</telerik:GridEditCommandColumn>
<telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn" ConfirmText="Are you sure you want to delete this application?" ConfirmTitle="Confirm Delete" ConfirmDialogType="Classic" ItemStyle-Width="10%" HeaderText="Delete">
</telerik:GridButtonColumn>
<telerik:GridBoundColumn DataField="AppId" UniqueName="AppId" Visible="false" HeaderText="Application Id" ReadOnly="true">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="AppName" UniqueName="AppName" HeaderText="Application Name" MaxLength="30" ItemStyle-Width="40%">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn DataField="ServerId" UniqueName="ServerId" HeaderText="Server Hosted" EditFormColumnIndex="1">
<EditItemTemplate>
<asp:DropDownList ID="ddlServerHosted" runat="server" DataTextField="Servers.ServerName" DataValueField="ServerId" Width="40%">
</asp:DropDownList>
</EditItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
<EditFormSettings ColumnNumber="2" CaptionDataField="AppId" InsertCaption="Insert New Application" EditFormType="AutoGenerated">
<EditColumn InsertText="Insert record" EditText="Edit application id #:" EditFormColumnIndex="0" UpdateText="Application updated" UniqueName="InsertCommandColumn1" CancelText="Cancel insert" ButtonType="ImageButton"></EditColumn>
<FormTableItemStyle Wrap="false" />
<FormTableStyle GridLines="Horizontal" CellPadding="2" CellSpacing="0" Height="110px" Width="110px" />
<FormTableAlternatingItemStyle Wrap="false" />
<FormStyle Width="100%" BackColor="#EEF2EA" />
<FormTableButtonRowStyle HorizontalAlign="Right" />
</EditFormSettings>
</MasterTableView>
</telerik:RadGrid>
<asp:EntityDataSource ID="applicationsEntityDataSource" runat="server"
ConnectionString="name=AnalyticsEntities" EnableDelete="True"
EntityTypeFilter="Applications" EnableInsert="True" EnableUpdate="True" EntitySetName="Applications"
DefaultContainerName="AnalyticsEntities" Include="Servers">
</asp:EntityDataSource>
I tried another approach where I replaced the GridTemplateColumn with the following code
<telerik:RadComboBox ID="RadComboBox1" DataSourceID="serversEntityDataSource" DataTextField="ServerName" DataValueField="ServerId" AppendDataBoundItems="true" runat="server" >
<Items>
<telerik:RadComboBoxItem />
</Items>
and using a separate EntityDataSource control as follows:
<asp:EntityDataSource ID="serversEntityDataSource" runat="server"
ConnectionString="name=AnalyticsEntities" EnableDelete="True"
EntityTypeFilter="Servers" EnableInsert="True" EnableUpdate="True" EntitySetName="Servers"
DefaultContainerName="AnalyticsEntities">
</asp:EntityDataSource>
but, I get the following error.
Application cannot be inserted. Reason: Entities in 'AnalyticsEntities.Applications' participate in the 'FK_Servers_Applications' relationship. 0 related 'Servers' were found. 1 'Servers' is expected.
My question is, how do you bind the navigation property and load the values in the DropDownList/RadComboBox control?
I believe you bind your dropdownlist to that second datasource instead of the grid's datasource (as you did in the second example). Then you set the SelectedValue property on the dropdown/combobox using the <%# BIND("ServerId") %> syntax within the template.