This is a migrated thread and some comments may be shown as answers.

How to collapse groups of items in RadGrid with editable checkboxes

17 Answers 296 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Herman Gouw
Top achievements
Rank 2
Herman Gouw asked on 01 Sep 2010, 04:51 PM
Hi,

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

Sort by
0
Herman Gouw
Top achievements
Rank 2
answered on 03 Sep 2010, 02:15 AM
Can someone (preferably Princy) please help with this problem?

Thanks in advance,
Herman
0
Princy
Top achievements
Rank 2
answered on 03 Sep 2010, 05:19 AM
Hello 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.
0
Herman Gouw
Top achievements
Rank 2
answered on 06 Sep 2010, 03:45 AM
Hi 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;
    }
}
0
Princy
Top achievements
Rank 2
answered on 07 Sep 2010, 11:34 AM
Hello Herman,

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.
0
Herman Gouw
Top achievements
Rank 2
answered on 08 Sep 2010, 09:34 AM
Thanks Princy for your help.

Kind regards,
Herman
0
Herman Gouw
Top achievements
Rank 2
answered on 13 Sep 2010, 09:05 AM
Hi Princy,

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
0
Herman Gouw
Top achievements
Rank 2
answered on 14 Sep 2010, 06:21 PM
Hi Princy,

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

0
Tsvetina
Telerik team
answered on 16 Sep 2010, 09:10 AM
Hi 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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Herman Gouw
Top achievements
Rank 2
answered on 19 Sep 2010, 07:56 AM
Thanks Tsvetina.

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
0
Herman Gouw
Top achievements
Rank 2
answered on 24 Sep 2010, 05:46 AM
Hi Tsvetina,

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
0
Tsvetina
Telerik team
answered on 29 Sep 2010, 01:38 PM
Hi 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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
nissim
Top achievements
Rank 1
answered on 05 Oct 2011, 11:02 AM
I have exactly the same problem but the solution you gave does not help when I go to a different page grid. When I go to another page I get the lines do not edit. Can you help me with this?
0
Tsvetina
Telerik team
answered on 05 Oct 2011, 03:47 PM
Hello Nissim,

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
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
nissim
Top achievements
Rank 1
answered on 26 Jan 2012, 10:15 AM

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.

0
Tsvetina
Telerik team
answered on 26 Jan 2012, 04:54 PM
Hi Nissim,

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
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
nissim
Top achievements
Rank 1
answered on 29 Jan 2012, 12:19 PM
Thank you,
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.
0
Tsvetina
Telerik team
answered on 01 Feb 2012, 10:39 AM
Hi Nissim,

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
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Herman Gouw
Top achievements
Rank 2
Answers by
Herman Gouw
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Tsvetina
Telerik team
nissim
Top achievements
Rank 1
Share this question
or