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

Get custom objects server side after checking a row

1 Answer 46 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Christian
Top achievements
Rank 1
Christian asked on 10 Jul 2012, 06:54 AM
Hi,
I have a grid that is bound to a data source:

<telerik:RadGrid ID="gvVerwendungen" runat="server" PageSize="15" 
                         AllowPaging="True" Width="100%"  AutoGenerateColumns="false"
                         OnNeedDataSource="gvVerwendungen_NeedDataSource" ShowStatusBar="true">
  
            <MasterTableView Width="100%" Summary="RadGrid table"  ShowFooter="true">
                <Columns>
                    <telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn">
                        <ItemTemplate>
                            <asp:CheckBox ID="Auswahl" runat="server" AutoPostBack="True" OnCheckedChanged="Auswahl_ToggleRowSelection" />
                        </ItemTemplate>
                        <HeaderTemplate>
                            <asp:CheckBox ID="headerChkbox" runat="server" AutoPostBack="True" />
                        </HeaderTemplate>
                    </telerik:GridTemplateColumn>
  
                    <telerik:GridBoundColumn FooterText="Dienststelle" UniqueName="Dienststelle" 
                                             HeaderText="Dienststelle" DataField="Dienststelle">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn FooterText="Beginn" UniqueName="Beginn" 
                                             HeaderText="Beginn" DataField="Beginn"
                                             DataType="System.DateTime" DataFormatString="{0:dd/MM/yy}">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn FooterText="Sachbezug" HeaderText="Sachbezug"
                                             DataField="Bezug">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn FooterText="Status" UniqueName="VerwendungsStatus"
                                             DataField="VerwendungsStatus">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
  
            <PagerStyle Mode="NextPrevAndNumeric" />
  
        </telerik:RadGrid>

In the code behind there is the event code:

protected void gvVerwendungen_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
               if (mitarbeiter != null && mitarbeiter.VerwendungseintraegeAktuelleUndGeplante != null)
                {
                    this.gvVerwendungen.DataSource = mitarbeiter.VerwendungseintraegeAktuelleUndGeplante;
                }
                             
        }

This works fine.
For the selection of the row I want to use a checkbox:
protected void Auswahl_ToggleRowSelection(object sender, EventArgs e)
       {
           ((sender as CheckBox).NamingContainer as GridItem).Selected = (sender as CheckBox).Checked;
           bool checkHeader = true;
             
             
           foreach (GridDataItem dataItem in gvVerwendungen.MasterTableView.Items)
           {
               // Auswahl is the name of the ckeckbox-column
               if (!(dataItem.FindControl("Auswahl") as CheckBox).Checked)
               {
                   checkHeader = false;
                     
                   break;
               }
           }
           GridHeaderItem headerItem = gvVerwendungen.MasterTableView.GetItems(GridItemType.Header)[0] as GridHeaderItem;
           (headerItem.FindControl("headerChkbox") as CheckBox).Checked = checkHeader;
           if (gvVerwendungen.SelectedItems.Count > 0)
           {
               // gvVerwendungen is the name of the grid
               GridDataItem dataItem = gvVerwendungen.SelectedItems[0] as GridDataItem;
               // Question: How can I get the bound custom object?
               // Problem: The custom object has no ID
           }
           else
           {
               tbStellenzeichen.Text = "Nothing";
           }
       }

My Question: How can I get the custom object instance of the current row?

For a hint, I would be grateful
Kind regards.
Christian

1 Answer, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 13 Jul 2012, 05:51 AM
Hi,

If you want to take the DataItem container of the GridDataItem you could use the DataItem property of the GridDataItem class:

if (gvVerwendungen.SelectedItems.Count > 0)
           {
               // gvVerwendungen is the name of the grid
               GridDataItem dataItem = gvVerwendungen.SelectedItems[0] as GridDataItem;
               var dataSourceItem = dataItem.DataItem;
               // Question: How can I get the bound custom object?
               // Problem: The custom object has no ID
           }
           else
           {
               tbStellenzeichen.Text = "Nothing";
           }

If this is not the case, please elaborate a bit more about your case.

Greetings,
Andrey
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
Christian
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Share this question
or