简体   繁体   中英

Telerik RadGrid

I have the following telerik grid and need to pass the data value from the nested table into the ItemCommand so when the user clicks the grid button the code is executed I am stuck on where to go with this

                <telerik:RadGrid ID="grdRegions" runat="server" Skin="Metro" AllowCustomSorting="True"
                ShowHeader="True" PageSize="10" ShowGroupPanel="False"     OnItemCommand="grdRegions_ItemCommand"
                ShowStatusBar="True" AutoGenerateColumns="False"
                AllowSorting="True" AllowPaging="True" DataSourceID="RegionDS" Width="500px">
                <PagerStyle Mode="Slider" />
                <MasterTableView DataSourceID="RegionDS" DataKeyNames="RegionCode" AllowMultiColumnSorting="True" Width="100%">

                    <DetailTables>
                        <telerik:GridTableView runat="server" DataKeyNames="RegionCode,DealerId" DataSourceID="DealerDS" AllowFilteringByColumn="True" >
                            <ParentTableRelation>
                                <telerik:GridRelationFields DetailKeyField="RegionCode" MasterKeyField="RegionCode"  />
                            </ParentTableRelation>
                            <Columns>
                                <telerik:GridBoundColumn SortExpression="DealerId" HeaderText="Dealer" HeaderButtonType="TextButton"
                                    DataField="Dealer" UniqueName="DealerId">
                                </telerik:GridBoundColumn>

                                <telerik:GridBoundColumn SortExpression="RegionCode" HeaderText="FDAF Region Code" HeaderButtonType="TextButton"
                                    DataField="RegionCode" UniqueName="RegionCode" AllowFiltering="False">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn SortExpression="DealerId" HeaderText="DealerId" HeaderButtonType="TextButton"
                                    DataField="DealerId" UniqueName="Dealer">
                                </telerik:GridBoundColumn>
                               <telerik:GridTemplateColumn ItemStyle-Width="22px" AllowFiltering="false">
                                <ItemTemplate>
                                    <asp:ImageButton ID="EditDealer" runat="server" AlternateText="Edit Dealer" ToolTip="Edit Dealer"
                                        ImageUrl="Images/edit.png" CommandArgument='<%# DataBinder.Eval(Container,"DataItem.DealerId")%>'
                                        CommandName="EditDealer" CausesValidation="false" />
                                </ItemTemplate>
                                <ItemStyle Width="22px"></ItemStyle>
                            </telerik:GridTemplateColumn>
                            </Columns>
                        </telerik:GridTableView>
                    </DetailTables>
                    <Columns>
                        <telerik:GridBoundColumn SortExpression="RegionName" HeaderText="FDAF Region" HeaderButtonType="TextButton"
                            DataField="RegionName" UniqueName="RegionName" AllowFiltering="False">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn SortExpression="RegionCode" HeaderText="FDAF Region Code" HeaderButtonType="TextButton"
                            DataField="RegionCode" UniqueName="RegionCode">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn SortExpression="RegionDealerCount" HeaderText="Dealers In Region" HeaderButtonType="TextButton"
                            DataField="RegionDealerCount" UniqueName="RegionDealerCount" AllowFiltering="False">
                        </telerik:GridBoundColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>






                     protected void grdRegions_ItemCommand(object sender, GridCommandEventArgs e)
                        {
                            objDealerDL.DealerId = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["DealerId"].ToString());
                            GridDataItem item = (GridDataItem)e.Item;                       
                            if (e.CommandName == "EditDealer")
                            {
                                try
                                {
                                  do work
                                }
                                catch (Exception ex)
                                {

                                }
                            }
                        }

You can use the ItemDataBound method.

Within this method you simply search for the control by name and set it's command argument.

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridDataItem)  
        {  
            // this is the grids current rows item data
            GridDataItem item = (GridDataItem)e.Item;  

            // This next line finds the image button control from within the grid cell
            ImageButton  btn = (ImageButton )item["ColumnNameHere"].FindControl("EditDealer");  

            // Cast the data item back to the type of item that you passed in as a datasource
            YourDataItemType val = (YourDataItemType) item.DataItem;

           // now set your command argument
            btn.CommandArgument = val.ChildValue;
        }  
    }  

When I say Cast the data item back to the type of item that you passed in as a datasource

Your data source is RegionDS.

For example if your RegionDS is a list of regions ie

List<Region>

Your cast would be:

Region val = (Region) item.DataItem;

ie the Single object from the larger collective

In the case of data tables you would Replace Region with DataRowView.

As you don't state what you are using as a datasource it's hard to provide any further information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM