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

Configuring the CommandItem bar

4 Answers 107 Views
Grid
This is a migrated thread and some comments may be shown as answers.
kernelk
Top achievements
Rank 1
kernelk asked on 30 Aug 2013, 06:40 PM
Hi,

I have a RadPageView with several RadGrid controls on each page. I am trying to accomplish a few things:

1. Add a CommandItem button but still have the default "refresh" button as well as have the buttons created from CommandItemSettings-AddNewRecordText and ShowExportToPdfButton. Defining a CommandItemTemplate removes these buttons.

2. I want to Hijack the functionality of the CommandItem button created by ShowExportToPdfButton and call my own PDF export method/handler.

3. Since there will be many Grids, how can I hijack each grid's ShowExportToPdfButton and identify which grid to export from the same export method?

Basically, I want a generic way to hijack each Grid's ShowExportToPdfButton or insert into each grid's CommandItem bar a generic exportPDF button that I define.

An add on question is how to find the width of each visible column that is to be exported so that I can set the grid.ExportSettings.Pdf.PageWidth accordingly.

Thanks for any advice.

4 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 31 Aug 2013, 11:14 AM
Hello,

<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
    AutoGenerateColumns="false" OnItemCommand="RadGrid1_ItemCommand">
    <MasterTableView CommandItemDisplay="Top" Name="MyGrid1">
        <CommandItemSettings ShowExportToPdfButton="true" />
        <Columns>
            <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                <ItemStyle Width="100px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                <ItemStyle Width="110px" />
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
<telerik:RadGrid ID="RadGrid2" runat="server" OnNeedDataSource="RadGrid2_NeedDataSource"
    AutoGenerateColumns="false" OnItemCommand="RadGrid2_ItemCommand">
    <MasterTableView CommandItemDisplay="Top" Name="MyGrid2">
        <CommandItemSettings ShowExportToPdfButton="true" />
        <Columns>
            <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                <ItemStyle Width="150px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                <ItemStyle Width="110px" />
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.ExportToPdfCommandName)
        {
            string strGridName = e.Item.OwnerTableView.Name;
  
            int totelwidth = 0;
            foreach (GridColumn gc in RadGrid1.MasterTableView.Columns)
            {
                if (gc.ItemStyle.Width.IsEmpty == false)
                {
                    totelwidth += (int)gc.ItemStyle.Width.Value;
                }
            }
  
            CommonExportSetttigs(strGridName);
            RadGrid1.ExportSettings.Pdf.PageWidth = totelwidth;
 
                e.Canceled = true; //cancel default export functionality
        }
    }
    protected void RadGrid2_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.ExportToPdfCommandName)
        {
            string strGridName = e.Item.OwnerTableView.Name;
  
            int totelwidth = 0;
            foreach (GridColumn gc in RadGrid2.MasterTableView.Columns)
            {
                if (gc.ItemStyle.Width.IsEmpty == false)
                {
                    totelwidth += (int)gc.ItemStyle.Width.Value;
                }
            }
  
            CommonExportSetttigs(strGridName);
            RadGrid2.ExportSettings.Pdf.PageWidth = totelwidth;
            e.Canceled = true; //cancel default export functionality
        }
    }
  
    protected void CommonExportSetttigs(string strGridName)
    {
        switch (strGridName)
        {
            case "MyGrid1": // From RadGrid1
                // Do your functionality here
                break;
            case "MyGrid2": // From RadGrid2
                break;
            default:
                break;
        }
    }


Thanks,
Jayesh Goyani
0
kernelk
Top achievements
Rank 1
answered on 03 Sep 2013, 04:13 PM
Hi Jayesh, thanks for the reply, you have given me some good ideas. I will look into creating a single RadGrid_ItemCommand to be used by all the grids.

Thanks
0
kernelk
Top achievements
Rank 1
answered on 05 Sep 2013, 06:02 PM
So far things are going ok, but some of the grids have an EditCommandColumn and/or GridButtonColumn and I want to hide these from the export. I am searching and I am not finding a good answer as to when and how to do this.
0
kernelk
Top achievements
Rank 1
answered on 06 Sep 2013, 12:20 AM
Updated: Ok I think I have solved the issues now thanks.

foreach (GridColumn gc in grid.MasterTableView.Columns)
{
    bool hideColumn = false;
    if (gc is GridEditCommandColumn) { hideColumn = true; }
    else if (gc is GridButtonColumn) { hideColumn = true; }
 
    if (hideColumn) { gc.Visible = false; }
 
    if (gc.Visible)
    {
        pageWidth += (int)gc.HeaderStyle.Width.Value;
    }
}

foreach (GridColumn gc in grid.MasterTableView.AutoGeneratedColumns)
{
    if (gc.Visible)
    {
        pageWidth += (int)gc.HeaderStyle.Width.Value;
    }
}
Tags
Grid
Asked by
kernelk
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
kernelk
Top achievements
Rank 1
Share this question
or