<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="DataFormatting._Default" %>
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
<
form
id
=
"form1"
runat
=
"server"
>
<
telerik:RadAjaxManager
ID
=
"RadAjaxManager1"
runat
=
"server"
>
</
telerik:RadAjaxManager
>
<
telerik:RadScriptManager
ID
=
"RadScriptManager1"
Runat
=
"server"
>
</
telerik:RadScriptManager
>
<
div
class
=
"contributionTable"
>
<
asp:PlaceHolder
ID
=
"PlaceHolder1"
runat
=
"server"
>
<
telerik:RadGrid
ID
=
"clientContribution"
runat
=
"server"
onitemdatabound
=
"clientContribution_ItemDataBound"
>
</
telerik:RadGrid
>
</
asp:PlaceHolder
>
</
div
>
</
form
>
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Diagnostics;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
namespace DataFormatting
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BuildTheGrid();
}
}
protected void clientContribution_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridGroupHeaderItem)
{
GridGroupHeaderItem item = (GridGroupHeaderItem)e.Item;
DataRowView groupDataRow = (DataRowView)e.Item.DataItem;
}
}
#region Build the grid
private DataSet BuildTheGrid()
{
try
{
clientContribution.DataSource = null;
DataSet contributionColumns = LoadGridData();
// Define the main grid
for (int loopPos = clientContribution.MasterTableView.Columns.Count; loopPos > 0; loopPos--)
{
clientContribution.MasterTableView.Columns.RemoveAt(loopPos - 1);
}
clientContribution.ID = "MyGrid";
clientContribution.DataSource = contributionColumns;
clientContribution.Width = Unit.Percentage(98);
clientContribution.AutoGenerateColumns = false;
clientContribution.ShowStatusBar = true;
clientContribution.MasterTableView.Width = Unit.Percentage(100);
// now build the hierarchical grid
GridBoundColumn boundColumn = new GridBoundColumn();
foreach (DataColumn col in contributionColumns.Tables[0].Columns)
{
boundColumn = new GridBoundColumn();
clientContribution.MasterTableView.Columns.Add(boundColumn);
boundColumn.DataField = col.ColumnName;
boundColumn.HeaderText = col.ColumnName;
boundColumn.Visible = true;
if (col.Ordinal == 1)
{
boundColumn.DataFormatString = "{0:##,###.0}";
}
if (col.Ordinal == 2)
{
boundColumn.DataFormatString = "{0:###.0%}";
}
}
clientContribution.DataBind();
}
catch (Exception exc)
{
Debug.WriteLine(exc.Message);
return null;
}
finally
{
}
return null;
}
#endregion
#region Load the Grid Data
private DataSet LoadGridData()
{
// return the data to display
DataSet contributionData = new DataSet("MyData");
DataTable gridData = contributionData.Tables.Add("ContData");
gridData.Columns.Add(new DataColumn("DummyValue"));
gridData.Columns.Add(new DataColumn("DecimalValue"));
gridData.Columns.Add(new DataColumn("PercentageValue"));
for (int i = 0; i < 10; i++)
{
DataRow dataRow = contributionData.Tables[0].NewRow();
dataRow["DummyValue"] = "Value" + i.ToString();
dataRow["DecimalValue"] = 1234.88 * i;
dataRow["PercentageValue"] = 2.33 * i;
contributionData.Tables[0].Rows.Add(dataRow);
}
return contributionData;
}
#endregion
}
}
Protected Sub RadGrid1_ItemInserted(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridInsertedEventArgs) Handles RadGrid1.ItemInserted
If e.Exception Is Nothing Then
Dim editedItem As GridEditableItem = e.Item
If "Placements".Equals(editedItem.OwnerTableView.Name) Then
Dim gridRows As GridItemCollection = RadGrid1.Items
Dim id As Int32 = 0
For Each data As GridDataItem In gridRows
id = Math.Max(id, data.GetDataKeyValue("PlanMasterId"))
Next
UpdateFarmPerformance(id)
ElseIf "Sheds".Equals(editedItem.OwnerTableView.Name) Then
'Update the Placment details from the shed details
Dim parentItem As GridDataItem = editedItem.OwnerTableView.ParentItem
Dim id As Int32 = parentItem.OwnerTableView.DataKeyValues(parentItem.ItemIndex)("PlanMasterId")
updatePlacements(id)
Dim gridRows As GridItemCollection = e.Item.OwnerTableView.Items
Dim id2 As Int32 = 0
For Each data As GridDataItem In gridRows
id2 = Math.Max(id2, data.GetDataKeyValue("PlanMasterShedId"))
Next
UpdateShedPerformance(id2)
UpdateFarmPerformance(id)
End If
End If
End Sub
<script>
somethingnasty
</script>
Hello everybody ,
I have a combobox inside of a Radgrid and want to be bound on runtime.
There is a very suit sample in Q1 2011 version of Telerik Demo under grid title(Gridà .Net3.5à Entity Framework Manual CRUD Operations) but it is very simple and there are just some common data Management about textbox.
But I tend to do that with a relational model,Suppos that we have two tables ,USERS and GENDER related with a f_key.
USER{UserID int ,Username nvarchar , GenderID int}
GENDER{GenderID int ,Longname nvarchar}
My aspx file is like this :
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
AllowPaging="true" AllowSorting="true" OnDeleteCommand="RadGrid1_DeleteCommand"
OnInsertCommand="RadGrid1_InsertCommand" AllowFilteringByColumn="true" OnUpdateCommand="RadGrid1_UpdateCommand"
OnUnload="RadGrid1_Unload">
<MasterTableView CommandItemDisplay="Top" AutoGenerateColumns="false" DataKeyNames="USERID"
InsertItemPageIndexAction="ShowItemOnCurrentPage">
<Columns>
<telerik:GridBoundColumn DataField="UserID" HeaderText="UserID" UniqueName="UserID" />
<telerik:GridBoundColumn DataField="Username" HeaderText="Username" UniqueName="Username" />
<telerik:GridTemplateColumn DataField="GENDER" HeaderText="GENDER" UniqueName="GENDER" >
<InsertItemTemplate>
<telerik:RadComboBox ID="RadComboBox1" runat="server"
Width="140px" />
</InsertItemTemplate>
<EditItemTemplate>
<telerik:RadTextBox ID="RadTComboBox1" runat="server"
ReadOnly="true" Width="140px" />
</EditItemTemplate>
</Columns>
</MasterTableView>
</telerik:RadGrid>
How I do my CRUDoperation with this realational model with EntityFramework.
I should demonstarte that I have an edmx file with two entities:tbl_Users , tbl_Gender and I tend to do this operation in Codebehind.
Some common code is such as this,if we use only textbox :
protected void RadGrid1_Unload(object sender, EventArgs e)
{
if (DbContext != null)
DbContext.Dispose();
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = DbContext.tbl_User.Select("it.UserID,it.Username,...");
}
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
GridEditableItem item = e.Item as GridEditableItem;
int _userId = (int)item.GetDataKeyValue("UserID");
tbl_User _tbl_user = DbContext.tbl_User.Where(p => p.UserID == _userId).FirstOrDefault();
item.UpdateValues(_tbl_user);
DbContext.SaveChanges();
}
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
GridEditableItem item = e.Item as GridEditableItem;
Hashtable values = new Hashtable();
item.ExtractValues(values);
tbl_User _tbl_user = new tbl_User();
item.UpdateValues(_tbl_user);
DbContext.AddTotbl_User(_tbl_user);
DbContext.SaveChanges();
}
protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
{
int _userId = (int)(e.Item as GridDataItem).GetDataKeyValue("UserID");
tbl_User _tbl_user = DbContext.tbl_User.Where(p => p.USERID == _userId).FirstOrDefault();
DbContext.DeleteObject(_tbl_user);
DbContext.SaveChanges();
}
this
.Form.DefaultFocus = RadComboBox1.ClientID;
RadAjaxManager am = RadAjaxManager.GetCurrent(
this
);
am.FocusControl(RadComboBox1.ClientID +
"_Input"
);
RadScriptManager1.SetFocus(RadComboBox1);