In my current project, I have a RadGrid with multilevel grouping which contains editable checkboxes.
However, it is not possible to collapse the group of items in the RadGrid.
The code is given below and the webpage is available on http://www.gouw.ws/EditGrouping.aspx
Can you please tell me how to make the RadGrid collapsible?
ASPX:
=====
<
telerik:RadGrid
ID
=
"RadGrid1"
AutoGenerateColumns
=
"false"
AllowMultiRowEdit
=
"true"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
OnPreRender
=
"RadGrid1_PreRender"
runat
=
"server"
>
<
ClientSettings
EnableAlternatingItems
=
"false"
>
<
Scrolling
UseStaticHeaders
=
"true"
/>
</
ClientSettings
>
<
MasterTableView
EditMode
=
"InPlace"
TableLayout
=
"Fixed"
>
<
GroupByExpressions
>
<
telerik:GridGroupByExpression
>
<
GroupByFields
>
<
telerik:GridGroupByField
FieldName
=
"Category"
SortOrder
=
"Ascending"
/>
</
GroupByFields
>
<
SelectFields
>
<
telerik:GridGroupByField
FieldName
=
"Category"
HeaderText
=
"Category"
/>
</
SelectFields
>
</
telerik:GridGroupByExpression
>
<
telerik:GridGroupByExpression
>
<
GroupByFields
>
<
telerik:GridGroupByField
FieldName
=
"Type"
SortOrder
=
"Ascending"
/>
</
GroupByFields
>
<
SelectFields
>
<
telerik:GridGroupByField
FieldName
=
"Type"
HeaderText
=
"Type"
/>
</
SelectFields
>
</
telerik:GridGroupByExpression
>
</
GroupByExpressions
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"Name"
DataType
=
"System.String"
HeaderText
=
"Name"
HeaderStyle-HorizontalAlign
=
"Left"
ItemStyle-HorizontalAlign
=
"Left"
ReadOnly
=
"true"
UniqueName
=
"Name"
/>
<
telerik:GridCheckBoxColumn
DataField
=
"Email"
DataType
=
"System.Boolean"
HeaderText
=
"Email"
HeaderStyle-HorizontalAlign
=
"Center"
ItemStyle-HorizontalAlign
=
"Center"
ReadOnly
=
"false"
UniqueName
=
"Email"
/>
<
telerik:GridCheckBoxColumn
DataField
=
"SMS"
DataType
=
"System.Boolean"
HeaderText
=
"SMS"
HeaderStyle-HorizontalAlign
=
"Center"
ItemStyle-HorizontalAlign
=
"Center"
ReadOnly
=
"false"
UniqueName
=
"SMS"
/>
<
telerik:GridCheckBoxColumn
DataField
=
"NotifyByEmail"
DataType
=
"System.Boolean"
HeaderText
=
"Notify By Email"
HeaderStyle-HorizontalAlign
=
"Center"
ItemStyle-HorizontalAlign
=
"Center"
ReadOnly
=
"false"
UniqueName
=
"NotifyByEmail"
/>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
ASPX.CS:
========
public
partial
class
EditGrouping : System.Web.UI.Page
{
private
List<Data> _data =
null
;
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack)
{
_data = Data.Load();
Session[
"DATA"
] = _data;
}
else
{
_data = (List<Data>)Session[
"DATA"
];
}
}
protected
void
RadGrid1_NeedDataSource(
object
source, GridNeedDataSourceEventArgs e)
{
this
.RadGrid1.DataSource = _data; ;
}
protected
void
RadGrid1_PreRender(
object
sender, EventArgs e)
{
foreach
(GridDataItem item
in
this
.RadGrid1.Items)
{
item.Edit =
true
;
}
this
.RadGrid1.Rebind();
}
Regards,
Herman
17 Answers, 1 is accepted
Thanks in advance,
Herman
The problem is because of rebinding the grid in PreRender event. You can put grid items in edit mode without additionally rebinding the grid by using EditIndexes collection before grid is bound. Try the following code in PageLoad event.
C#:
protected
void
Page_Load(
object
sender, EventArgs e)
{
for
(
int
i = 0; i < RadGrid1.PageSize; i++)
{
RadGrid1.EditIndexes.Add(i);
}
}
Also please take a look at the following documentation
Put all items in edit mode without additional rebind
Thanks,
Princy.
Thanks a lot for your prompt and quick reply.
I have tried your solution during the weekend.
I found out that only the first 10 lines of the gird are editable.
Here is the screen shot ... Edit Grouping Screen Shoot
Below are the source code.
Kind regards,
Herman
ASPX:
=====
<
form
id
=
"form1"
runat
=
"server"
>
<
div
>
<
telerik:RadScriptManager
ID
=
"RadScriptManager1"
runat
=
"server"
/>
<
telerik:RadAjaxManager
ID
=
"RadAjaxManager1"
runat
=
"server"
>
<
AjaxSettings
>
<
telerik:AjaxSetting
AjaxControlID
=
"RadGrid1"
>
<
UpdatedControls
>
<
telerik:AjaxUpdatedControl
ControlID
=
"RadGrid1"
/>
</
UpdatedControls
>
</
telerik:AjaxSetting
>
</
AjaxSettings
>
</
telerik:RadAjaxManager
>
<
telerik:RadGrid
ID
=
"RadGrid1"
AutoGenerateColumns
=
"false"
AllowMultiRowEdit
=
"true"
OnItemCreated
=
"RadGrid1_ItemCreated"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
runat
=
"server"
>
<
ClientSettings
EnableAlternatingItems
=
"false"
>
<
Scrolling
UseStaticHeaders
=
"true"
/>
</
ClientSettings
>
<
MasterTableView
DataKeyNames
=
"Category,Type,Name"
EditMode
=
"InPlace"
TableLayout
=
"Fixed"
>
<
GroupByExpressions
>
<
telerik:GridGroupByExpression
>
<
GroupByFields
>
<
telerik:GridGroupByField
FieldName
=
"Category"
SortOrder
=
"Ascending"
/>
</
GroupByFields
>
<
SelectFields
>
<
telerik:GridGroupByField
FieldName
=
"Category"
HeaderText
=
"Category"
/>
</
SelectFields
>
</
telerik:GridGroupByExpression
>
<
telerik:GridGroupByExpression
>
<
GroupByFields
>
<
telerik:GridGroupByField
FieldName
=
"Type"
SortOrder
=
"Ascending"
/>
</
GroupByFields
>
<
SelectFields
>
<
telerik:GridGroupByField
FieldName
=
"Type"
HeaderText
=
"Type"
/>
</
SelectFields
>
</
telerik:GridGroupByExpression
>
</
GroupByExpressions
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"Name"
DataType
=
"System.String"
HeaderText
=
"Name"
HeaderStyle-HorizontalAlign
=
"Left"
ItemStyle-HorizontalAlign
=
"Left"
ReadOnly
=
"true"
UniqueName
=
"Name"
/>
<
telerik:GridCheckBoxColumn
DataField
=
"Email"
DataType
=
"System.Boolean"
HeaderText
=
"Email"
HeaderStyle-HorizontalAlign
=
"Center"
ItemStyle-HorizontalAlign
=
"Center"
ReadOnly
=
"false"
UniqueName
=
"Email"
/>
<
telerik:GridCheckBoxColumn
DataField
=
"SMS"
DataType
=
"System.Boolean"
HeaderText
=
"SMS"
HeaderStyle-HorizontalAlign
=
"Center"
ItemStyle-HorizontalAlign
=
"Center"
ReadOnly
=
"false"
UniqueName
=
"SMS"
/>
<
telerik:GridCheckBoxColumn
DataField
=
"NotifyByEmail"
DataType
=
"System.Boolean"
HeaderText
=
"Notify By Email"
HeaderStyle-HorizontalAlign
=
"Center"
ItemStyle-HorizontalAlign
=
"Center"
ReadOnly
=
"false"
UniqueName
=
"NotifyByEmail"
/>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
</
div
>
C#:
===
public
partial
class
EditGrouping : System.Web.UI.Page
{
private
List<Data> _data =
null
;
protected
void
Page_Load(
object
sender, EventArgs e)
{
for
(
int
i = 0; i < RadGrid1.PageSize; i++)
{
this
.RadGrid1.EditIndexes.Add(i);
}
if
(!IsPostBack)
{
if
(_data ==
null
) _data = Data.Load();
Session[
"DATA"
] = _data;
}
else
{
_data = (List<Data>)Session[
"DATA"
];
}
}
protected
void
RadGrid1_NeedDataSource(
object
source, GridNeedDataSourceEventArgs e)
{
this
.RadGrid1.DataSource = _data; ;
}
protected
void
RadGrid1_ItemCreated(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem item = (GridEditableItem)e.Item;
CheckBox checkBox = (CheckBox)item[
"Email"
].Controls[0];
checkBox.AutoPostBack =
true
;
checkBox.CheckedChanged +=
new
EventHandler(Email_CheckedChanged);
checkBox = (CheckBox)item[
"SMS"
].Controls[0];
checkBox.AutoPostBack =
true
;
checkBox.CheckedChanged +=
new
EventHandler(SMS_CheckedChanged);
checkBox = (CheckBox)item[
"NotifyByEmail"
].Controls[0];
checkBox.AutoPostBack =
true
;
checkBox.CheckedChanged +=
new
EventHandler(NotifyByEmail_CheckedChanged);
}
}
protected
void
Email_CheckedChanged(
object
sender, EventArgs e)
{
CheckBox checkBox = (CheckBox)sender;
GridEditableItem item = (GridEditableItem)checkBox.NamingContainer;
Hashtable values =
new
Hashtable();
item.ExtractValues(values);
Data dataItem = GetDataItemByName(item.GetDataKeyValue(
"Category"
).ToString(), item.GetDataKeyValue(
"Type"
).ToString(), item.GetDataKeyValue(
"Name"
).ToString());
dataItem.Email = (
bool
)values[
"Email"
];
this
.RadGrid1.Rebind();
}
protected
void
SMS_CheckedChanged(
object
sender, EventArgs e)
{
CheckBox checkBox = (CheckBox)sender;
GridEditableItem item = (GridEditableItem)checkBox.NamingContainer;
Hashtable values =
new
Hashtable();
item.ExtractValues(values);
Data dataItem = GetDataItemByName(item.GetDataKeyValue(
"Category"
).ToString(), item.GetDataKeyValue(
"Type"
).ToString(), item.GetDataKeyValue(
"Name"
).ToString());
dataItem.SMS = (
bool
)values[
"SMS"
];
this
.RadGrid1.Rebind();
}
protected
void
NotifyByEmail_CheckedChanged(
object
sender, EventArgs e)
{
CheckBox checkBox = (CheckBox)sender;
GridEditableItem item = (GridEditableItem)checkBox.NamingContainer;
Hashtable values =
new
Hashtable();
item.ExtractValues(values);
Data dataItem = GetDataItemByName(item.GetDataKeyValue(
"Category"
).ToString(), item.GetDataKeyValue(
"Type"
).ToString(), item.GetDataKeyValue(
"Name"
).ToString());
dataItem.NotifyByEmail = (
bool
)values[
"NotifyByEmail"
];
this
.RadGrid1.Rebind();
}
protected
Data GetDataItemByName(
string
category,
string
type,
string
name)
{
List<Data> data = (List<Data>)Session[
"DATA"
];
foreach
(Data item
in
data)
{
if
((item.Category == category) && (item.Type == type) && (item.Name == name))
{
return
item;
}
}
return
null
;
}
}
Since you have not enabled Paging, instead of using 'RadGrid1.PageSize' iterate through using "_data.Count" (after populating the List) as shown here.
C#:
for
(
int
i = 0; i <_data.Count; i++)
{
RadGrid1.EditIndexes.Add(i);
}
Hope this helps,
Princy.
Kind regards,
Herman
I need to add an editable checkbox field on each Type grouping header as shown on Edit Grouping Screen Shot
The value will be initialised from the data object and the new value will be saved into the data object when the user press the Save button.
I need to do this on server side.
Can you please show me how to do this?
Regards,
Herman
The web page in my project is very similar to http://www.gouw.ws/EditGrouping.aspx
When the user clicks on the Save button, the data object will be sent to the back-end (and saved).
The user requested to add an editable check box in the Type Grouping Header as shown on http://www.gouw.ws/EditGrouping.jpg
When the user clicks on the check box, the value will be updated into the data object.
Is it possible to add an editable check box in the Type Grouping Header?
If it is, can you please give a sample on how to do this?
The code of the above web page are as follows:
ASPX
====
<
form
id
=
"form1"
runat
=
"server"
>
<
div
>
<
telerik:RadScriptManager
ID
=
"RadScriptManager1"
runat
=
"server"
/>
<
telerik:RadAjaxManager
ID
=
"RadAjaxManager1"
runat
=
"server"
>
<
AjaxSettings
>
<
telerik:AjaxSetting
AjaxControlID
=
"RadGrid1"
>
<
UpdatedControls
>
<
telerik:AjaxUpdatedControl
ControlID
=
"RadGrid1"
/>
</
UpdatedControls
>
</
telerik:AjaxSetting
>
</
AjaxSettings
>
</
telerik:RadAjaxManager
>
<
asp:Button
ID
=
"Button1"
style
=
"margin-right:12px"
Text
=
"Submit"
Width
=
"100"
OnClick
=
"Button1_Click"
runat
=
"server"
/>
<
asp:Button
ID
=
"Button2"
style
=
"margin-right:12px"
Text
=
"Expand"
Width
=
"100"
OnClick
=
"Button2_Click"
runat
=
"server"
/>
<
asp:Button
ID
=
"Button3"
Text
=
"Collapse"
Width
=
"100"
OnClick
=
"Button3_Click"
runat
=
"server"
/>
<
telerik:RadGrid
ID
=
"RadGrid1"
AutoGenerateColumns
=
"false"
AllowMultiRowEdit
=
"true"
OnItemCreated
=
"RadGrid1_ItemCreated"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
runat
=
"server"
>
<
ClientSettings
EnableAlternatingItems
=
"false"
>
<
Scrolling
UseStaticHeaders
=
"true"
/>
</
ClientSettings
>
<
MasterTableView
DataKeyNames
=
"Category,Type,Name"
EditMode
=
"InPlace"
GroupsDefaultExpanded
=
"false"
TableLayout
=
"Fixed"
>
<
GroupByExpressions
>
<
telerik:GridGroupByExpression
>
<
GroupByFields
>
<
telerik:GridGroupByField
FieldName
=
"Category"
SortOrder
=
"Ascending"
/>
</
GroupByFields
>
<
SelectFields
>
<
telerik:GridGroupByField
FieldName
=
"Category"
HeaderText
=
"Category"
/>
</
SelectFields
>
</
telerik:GridGroupByExpression
>
<
telerik:GridGroupByExpression
>
<
GroupByFields
>
<
telerik:GridGroupByField
FieldName
=
"Type"
SortOrder
=
"Ascending"
/>
</
GroupByFields
>
<
SelectFields
>
<
telerik:GridGroupByField
FieldName
=
"Type"
HeaderText
=
"Type"
/>
</
SelectFields
>
</
telerik:GridGroupByExpression
>
</
GroupByExpressions
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"Name"
DataType
=
"System.String"
HeaderText
=
"Name"
HeaderStyle-HorizontalAlign
=
"Left"
ItemStyle-HorizontalAlign
=
"Left"
ReadOnly
=
"true"
UniqueName
=
"Name"
/>
<
telerik:GridCheckBoxColumn
DataField
=
"Email"
DataType
=
"System.Boolean"
HeaderText
=
"Email"
HeaderStyle-HorizontalAlign
=
"Center"
ItemStyle-HorizontalAlign
=
"Center"
ReadOnly
=
"false"
UniqueName
=
"Email"
/>
<
telerik:GridCheckBoxColumn
DataField
=
"SMS"
DataType
=
"System.Boolean"
HeaderText
=
"SMS"
HeaderStyle-HorizontalAlign
=
"Center"
ItemStyle-HorizontalAlign
=
"Center"
ReadOnly
=
"false"
UniqueName
=
"SMS"
/>
<
telerik:GridCheckBoxColumn
DataField
=
"NotifyByEmail"
DataType
=
"System.Boolean"
HeaderText
=
"Notify By Email"
HeaderStyle-HorizontalAlign
=
"Center"
ItemStyle-HorizontalAlign
=
"Center"
ReadOnly
=
"false"
UniqueName
=
"NotifyByEmail"
/>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
</
div
>
</
form
>
C#
==
public
partial
class
EditGrouping : System.Web.UI.Page
{
private
Cache _cache =
null
;
private
List<Data> _data =
null
;
public
EditGrouping()
{
_cache =
new
Cache();
}
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack)
{
_cache.Load(
this
,
ref
_data);
if
(_data ==
null
) _data = Data.Load();
Session[
"DATA"
] = _data;
}
else
{
_data = (List<Data>)Session[
"DATA"
];
}
for
(
int
i = 0; i < _data.Count; i++)
{
this
.RadGrid1.EditIndexes.Add(i);
}
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
_cache.Save(
this
, _data);
}
protected
void
Button2_Click(
object
sender, EventArgs e)
{
foreach
(GridItem item
in
this
.RadGrid1.MasterTableView.Controls[0].Controls)
{
if
(item
is
GridGroupHeaderItem)
{
item.Expanded =
true
;
}
}
}
protected
void
Button3_Click(
object
sender, EventArgs e)
{
foreach
(GridItem item
in
this
.RadGrid1.MasterTableView.Controls[0].Controls)
{
if
(item
is
GridGroupHeaderItem)
{
item.Expanded =
false
;
}
}
}
protected
void
RadGrid1_NeedDataSource(
object
source, GridNeedDataSourceEventArgs e)
{
this
.RadGrid1.DataSource = _data;
}
protected
void
RadGrid1_ItemCreated(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem item = (GridEditableItem)e.Item;
CheckBox checkBox = (CheckBox)item[
"Email"
].Controls[0];
checkBox.AutoPostBack =
true
;
checkBox.CheckedChanged +=
new
EventHandler(Email_CheckedChanged);
checkBox = (CheckBox)item[
"SMS"
].Controls[0];
checkBox.AutoPostBack =
true
;
checkBox.CheckedChanged +=
new
EventHandler(SMS_CheckedChanged);
checkBox = (CheckBox)item[
"NotifyByEmail"
].Controls[0];
checkBox.AutoPostBack =
true
;
checkBox.CheckedChanged +=
new
EventHandler(NotifyByEmail_CheckedChanged);
}
}
protected
void
Email_CheckedChanged(
object
sender, EventArgs e)
{
CheckBox checkBox = (CheckBox)sender;
GridEditableItem item = (GridEditableItem)checkBox.NamingContainer;
Hashtable values =
new
Hashtable();
item.ExtractValues(values);
Data dataItem = GetDataItemByName(item.GetDataKeyValue(
"Category"
).ToString(), item.GetDataKeyValue(
"Type"
).ToString(), item.GetDataKeyValue(
"Name"
).ToString());
dataItem.Email = (
bool
)values[
"Email"
];
}
protected
void
SMS_CheckedChanged(
object
sender, EventArgs e)
{
CheckBox checkBox = (CheckBox)sender;
GridEditableItem item = (GridEditableItem)checkBox.NamingContainer;
Hashtable values =
new
Hashtable();
item.ExtractValues(values);
Data dataItem = GetDataItemByName(item.GetDataKeyValue(
"Category"
).ToString(), item.GetDataKeyValue(
"Type"
).ToString(), item.GetDataKeyValue(
"Name"
).ToString());
dataItem.SMS = (
bool
)values[
"SMS"
];
}
protected
void
NotifyByEmail_CheckedChanged(
object
sender, EventArgs e)
{
CheckBox checkBox = (CheckBox)sender;
GridEditableItem item = (GridEditableItem)checkBox.NamingContainer;
Hashtable values =
new
Hashtable();
item.ExtractValues(values);
Data dataItem = GetDataItemByName(item.GetDataKeyValue(
"Category"
).ToString(), item.GetDataKeyValue(
"Type"
).ToString(), item.GetDataKeyValue(
"Name"
).ToString());
dataItem.NotifyByEmail = (
bool
)values[
"NotifyByEmail"
];
}
protected
Data GetDataItemByName(
string
category,
string
type,
string
name)
{
List<Data> data = (List<Data>)Session[
"DATA"
];
foreach
(Data item
in
data)
{
if
((item.Category == category) && (item.Type == type) && (item.Name == name))
{
return
item;
}
}
return
null
;
}
}
Thanks,
Herman
In order to achieve the desired functionality, you can use the logic from that CodeLibrary article. As the sample project there uses an old version of RadControls, I am attaching an updated sample to this post for you to view.
Anyway, keep in mind that a limitation in this implementation is the fact that group header items text is lost during the process of adding the checkbox. I hope that this will not be a problem in your scenario.
All the best,
Tsvetina
the Telerik team
I have done more investigations and it looks like the suitable solution for my project will be hierarchy model instead of grouping model of RadGrid. I am now reading more samples and demos about this model and will try it out.
Best regards,
Herman Gouw
I have converted this RadGrid from Grouping model into Hierarchy model.
The new web page is available on http://www.gouw.ws/HierarchyWithDS.aspx
However, I am having difficulty in trying to set every row or the RadGrid with editable checkboxes to edit mode similar to the Grouping model on http://www.gouw.ws/EditGrouping.aspx
For details description of the problem, please check the following thread http://www.telerik.com/community/forums/aspnet-ajax/grid/how-to-put-the-grid-items-of-a-hierarchy-model-radgrid-in-edit-mode.aspx
Can you please help me?
Regards,
Herman
I am not sure that I understand you correctly, as there is some problem with loading the links that you posted and I cannot see how the grouped RadGrid looks in the sample page. Also, is the problem which you are encountering with putting the rows into edit mode or is it with implementing the checkboxes themselves.?
Could you please post a screenshot of how you want your grid to look with all your requirements listed, so we can offer you a more straight-to-the-point solution.
Thank you in advance.
All the best,
Tsvetina
the Telerik team
Can you paste the code you use that causes the problematic results? There were a few suggestions made here and I am not sure which one exactly you have in mind. If you are talking about putting all items in edit mode, you can also try the other suggestions in the previously linked help article:
http://www.telerik.com/help/aspnet-ajax/grid-put-all-items-in-edit-mode-no-additional-rebind.html
Kind regards,
Tsvetina
the Telerik team
Here is my code:
public
partial class Pages_Students_Attendance : PagePersisterBasePage
{
#region
Properties
public DataSet AttendanceData
{
get
{
if (SessionMng.AttendanceData == null)
{
if(SessionMng.CurrentUser.CurrentProgram.IReportingPeriodTypeId==1)
SessionMng.AttendanceData = StudentActivity.StudentActivitySelectByMonth(SessionMng.CurrentUser.CurrHierarchy.Program, txtYear.Text, ddlMonth.SelectedValue, cbxWorker.SelectedValue);
else
if (SessionMng.CurrentUser.CurrentProgram.IReportingPeriodTypeId == 2)
SessionMng.AttendanceData = StudentActivity.StudentActivitySelectBySemester(SessionMng.CurrentUser.CurrHierarchy.Program, cbxWorker.SelectedValue);
else
if (SessionMng.CurrentUser.CurrentProgram.IReportingPeriodTypeId == 3)
SessionMng.AttendanceData = StudentActivity.StudentActivitySelectByPeriod(SessionMng.CurrentUser.CurrHierarchy.Program, cbxWorker.SelectedValue);
}
return SessionMng.AttendanceData;
}
}
#endregion
#region
Methods
protected void Page_Load(object sender, EventArgs e)
{
headerUC1.My_Page_Init();
//
headerUC1.AddToAjax(
"GridAttendance", true, true);
headerUC1.AddToAjax(
"ddlMonth", "GridAttendance", true, true);
headerUC1.AddToAjax(
"txtYear", "GridAttendance", true, true);
if (GridAttendance.Visible == true)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "GridAttendance", "setScrollHeight('GridAttendance',110);", true);
}
if (!IsPostBack)
{
NerLeelef.
Program myProgram = ((User)Session[NerLeelef.User.MY]).CurrentProgram;
SessionMng.AttendanceData = null;
SessionMng.IsChecked = null;
DataTable dtMonth = (Application[CodeTablesMng.KEY] as CodeTablesMng).GetCodeTable("T_sys_Month", true); ;
ddlMonth.DataSource = dtMonth;
ddlMonth.DataTextField = dtMonth.Columns[
"nvValue"].ColumnName;
ddlMonth.DataValueField = dtMonth.Columns[
"iGlobalSysCode"].ColumnName;
ddlMonth.DataBind();
if (myProgram.IReportingPeriodTypeId == 1)
{
ddlMonth.SelectedValue =
DateTime.Now.Month.ToString();
txtYear.Text =
DateTime.Now.Year.ToString();
}
else
{
DateTime DtToDate = (DateTime)myProgram.DtToDate;
DateTime DtFromDate = (DateTime)myProgram.DtFromDate;
ddlMonth.SelectedValue = DtToDate.Month.ToString();
txtYear.Text = DtToDate.Year.ToString();
ddlMonth.Visible =
false;
txtYear.Visible =
false;
lblMonth.Visible =
false;
lblYear.Visible =
false;
}
DataSet dsWorker = Staff.GetWorker(SessionMng.CurrentUser.CurrHierarchy.Site);
FillComboBox(cbxWorker, AddEmptyRow(dsWorker,
"iPersonId", "nvPersonName"), "iPersonId", "nvPersonName");
cbxWorker.SelectedValue =
"-1";
}
for (int i = 0; i < GridAttendance.PageSize; i++)
{
GridAttendance.EditIndexes.Add(i);
}
}
protected override void InitializeCulture()
{
if (SessionMng.Language != null)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(SessionMng.Language);
Thread.CurrentThread.CurrentCulture = new CultureInfo(SessionMng.Language);
}
}
private DataSet AddEmptyRow(DataSet ds, string Id, string nvName)
{
DataRow row = ds.Tables[0].NewRow();
row[Id] =
"-1";
row[nvName] =
"";
ds.Tables[0].Rows.InsertAt(row, 0);
return ds;
}
private void FillComboBox(Infra.UI.RadComboBox cbx, Object dataSource, string DataValueField, string DataTextField)
{
cbx.DataSource = dataSource;
cbx.DataValueField = DataValueField;
cbx.DataTextField = DataTextField;
cbx.DataBind();
}
protected void GridAttendance_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
GridAttendance.DataSource =
this.AttendanceData;
}
protected void GridAttendance_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
saveAll();
}
protected void GridAttendance_PreRender(object source, EventArgs e)
{
if (SessionMng.IsChecked != null && SessionMng.IsChecked == "false")
SessionMng.IsChecked = "true";
else
{
if (!StudentActivity.IsReportByProgram(SessionMng.CurrentUser.CurrHierarchy.Program, txtYear.Text, ddlMonth.SelectedValue))
{
foreach (GridDataItem item in GridAttendance.MasterTableView.Items)
item.Edit =
true;
//GridAttendance.Rebind();
}
else
{
foreach (GridDataItem item in GridAttendance.MasterTableView.Items)
item.Edit =
false;
// GridAttendance.Rebind();
}
}
}
protected void GridAttendance_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == commandName.CancleAll.ToString())
Response.Redirect(
"Students.aspx");
else if (e.CommandName != "Update" && e.CommandName != "Cancel" && !StudentActivity.IsReportByProgram(SessionMng.CurrentUser.CurrHierarchy.Program, txtYear.Text, ddlMonth.SelectedValue))
// -
saveAll();
if (e.CommandName == Infra.UI.RadGridEn.CMD_ExportExcel)
{
GridAttendance.ExportSettings.ExportOnlyData =
true;
GridAttendance.ExportSettings.IgnorePaging =
true;
GridAttendance.ExportSettings.OpenInNewWindow =
false;
GridAttendance.MasterTableView.ExportToExcel();
}
}
protected void GridAttendance_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
Infra.UI.
CheckBox.OnTemplateClientChange(e.Item, "CheckBox1", "onValueChange");
if (e.Item is GridCommandItem)
{
GridCommandItem commandItem = e.Item as GridCommandItem;
if (StudentActivity.IsReportByProgram(SessionMng.CurrentUser.CurrHierarchy.Program, txtYear.Text, ddlMonth.SelectedValue))
{
(commandItem.FindControl(
"Save") as LinkButton).Visible = false;
(commandItem.FindControl(
"Image2") as Image).Visible = false;
}
else
{
(commandItem.FindControl(
"Save") as LinkButton).Visible = true;
(commandItem.FindControl(
"Image2") as Image).Visible = true;
}
}
}
private void saveAll()
{
foreach (GridDataItem item in GridAttendance.MasterTableView.Items)
saveRow(item);
SessionMng.AttendanceData = null;
}
private void saveRow(GridDataItem item)
{
GridEditManager editMan = (item as GridEditableItem).EditManager;
string IStudentId = string.Empty;
string iScheduleRealTimeId = string.Empty;
Hashtable oldValues = item.SavedOldValues as Hashtable;
CheckBox cbx = (CheckBox)item.FindControl("CheckBox1");
if (oldValues.ContainsKey("Attendance") && cbx.Checked != (bool)oldValues["Attendance"]) //
{
foreach (GridColumn column in item.OwnerTableView.RenderColumns)
{
if ((column is IGridEditableColumn))
{
IGridEditableColumn editableCol = (column as IGridEditableColumn);
IGridColumnEditor editor = editMan.GetColumnEditor(editableCol);
//
if (editor is GridTextColumnEditor && column.UniqueName == "IStudentId")
IStudentId = (editor
as GridTextColumnEditor).Text;
else
if (editor is GridTextColumnEditor && column.UniqueName == "iScheduleRealTimeId")
iScheduleRealTimeId = (editor
as GridTextColumnEditor).Text;
}
}
if (cbx.Checked) //
StudentActivity.UpdateStudentActivityRow(IStudentId, iScheduleRealTimeId, SessionMng.CurrentUser.Id);
else //
StudentActivity.DelStudentActivityRow(IStudentId, iScheduleRealTimeId);
}
}
protected void btnShowAttendance_Click(object sender, EventArgs e)
{
if (SessionMng.CurrentUser.CurrentProgram.IReportingPeriodTypeId == 1)
Validate(
"month");
if (Page.IsValid)
{
GridAttendance.Visible =
true;
SessionMng.AttendanceData = null;
GridAttendance.DataSource =
this.AttendanceData;
GridAttendance.DataBind();
ScriptManager.RegisterStartupScript(this, this.GetType(), "GridAttendance", "setScrollHeight('GridAttendance',110);", true);
}
}
protected void btnReport_Click(object sender, EventArgs e)
{
if (SessionMng.CurrentUser.CurrentProgram.IReportingPeriodTypeId == 1)
Validate(
"month");
if (Page.IsValid)
{
if (StudentActivity.IsReportByProgram(SessionMng.CurrentUser.CurrHierarchy.Program, txtYear.Text, ddlMonth.SelectedValue))
{
string myScript = GetLocalResourceObject("alertReport").ToString();// "alert('Activities have already been reported !');";
ScriptManager.RegisterStartupScript(this, this.GetType(), "saveConfirm1", myScript, true);
}
else
{
StudentActivity.ReportToSite(((User)Session[NerLeelef.User.MY]).CurrentProgram, txtYear.Text, ddlMonth.SelectedValue);
string myScript = GetLocalResourceObject("alertSuccessfully").ToString();// "alert('Activities have been reported successfully !');";
ScriptManager.RegisterStartupScript(this, this.GetType(), "saveConfirm2", myScript, true);
}
}
GridAttendance.Rebind();
}
protected void ToggleSelectedState(object sender, EventArgs e)
{
CheckBox headerCheckBox = (sender as CheckBox);
foreach (GridDataItem dataItem in GridAttendance.MasterTableView.Items)
(dataItem.FindControl(
"CheckBox1") as CheckBox).Checked = headerCheckBox.Checked;
SessionMng.IsChecked = "false";
}
#endregion
}
thanks.
You could then try one of the other two approaches.
If you use EditForms edit mode:
protected
void
RadGrid1_ItemCreated(
object
sender, Telerik.Web.UI.GridItemEventArgs e)
{
if
(!Page.IsPostBack && e.Item
is
GridEditableItem)
{
e.Item.Edit =
true
;
}
}
If you use InPlace, you can try the following approach, however keep an eye on performance, as it requires a second rebind of the grid control:
protected
void
RadGrid1_PreRender(
object
sender, System.EventArgs e)
{
if
(!IsPostBack)
{
foreach
(GridItem item
in
RadGrid1.MasterTableView.Items)
{
if
(item
is
GridEditableItem)
{
GridEditableItem editableItem = item
as
GridDataItem;
editableItem.Edit =
true
;
}
}
RadGrid1.Rebind();
}
}
Kind regards,
Tsvetina
the Telerik team
Now I see another problem - when I drag the column to grroup , it's O.K,
but after collapse or group the datasource is null and I cannot use rebind .
What can I do?
Thanks.
Doesn't the NeedDataSource event fire on (un)grouping your grid? If it does not, please review your code for any calls to DataBind() or DataSource settings unfollowed by Rebind() call.
Also, if your grid is not too great in terms of records, you could try using GroupLoadMode="Client" and see if the problem remains.
If you want us to help you straight away, you could open up a formal support ticket and send a small runnable sample demonstrating the problem that you have encountered. We will debug it and let you know how it can be fixed.
Greetings,
Tsvetina
the Telerik team