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

Why is background color and font change only updated every other row in radgrid?

3 Answers 163 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kimberly
Top achievements
Rank 1
Kimberly asked on 16 Nov 2012, 05:31 PM
I am trying to export the contents of my radgrid to a pdf file.  I am using the default skin.  Since I have 13 columns, the data is too long to fit on the page and is cut off.  I was trying to update the size of the text initially and noticed that it was only working on every other row.  I am assuming that this has something to do with the AlternatingItemStyle setting?  Is there something special that I need to add to get this update to work on all rows in the radgrid?  Please let me know if you need for me to provide you with any other information.

protected void listAllAcctsBtnExportToPDF_Click(object sender, EventArgs e)
    {
      ApplyStylesToPDFExport(rgListAllAccounts.MasterTableView);
       rgListAllAccounts.MasterTableView.ExportToPdf();
    }//end of listAllAcctsBtnExportToPDF_Click


private void ApplyStylesToPDFExport(GridTableView view)
    {
      // Get access to the header of the grid 
      GridItem headerItem = view.GetItems(GridItemType.Header)[0];
  
      // Apply some css style to the header 
      headerItem.Cells.Count.ToString();
  
      foreach (TableCell cell in headerItem.Cells)
       
        cell.Style["font-family"] = "Verdana";        
        cell.Style["text-align"] = "left";        
        cell.Style["vertical-align"] = "middle";        
        cell.Style["font-size"] = "10px";
      }
  
      // Get access to the data of the grid 
      GridItem[] dataItems = view.GetItems(GridItemType.Item);      
    
      // Apply some css style to the data items 
      foreach (GridItem item in dataItems)
      {
        foreach (TableCell cell in item.Cells)
        {
          cell.Style["font-family"] = "Verdana";
          cell.Style["text-align"] = "left";
          cell.Style["vertical-align"] = "middle";
          cell.Style["font-size"] = "10px";
          cell.Style["background-color"] = "red";
        }
      }
    }//end of ApplyStylesToPDFExport

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Nov 2012, 02:43 PM
Hello,

i have tried with your code and its works perfectly.

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
>
            <MasterTableView>
                <Columns>
                    <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridEditCommandColumn>
                    </telerik:GridEditCommandColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    dynamic data = new[] {
      new { ID = 1, Name ="aaa"},
      new { ID = 2, Name = "bbb"},
      new { ID = 3, Name = "ccc"},
      new { ID = 4, Name = "ddd"},
      new { ID = 5, Name ="eee"},
      new { ID = 6, Name ="aaa"},
      new { ID = 7, Name = "bbb"},
      new { ID = 8, Name = "ccc"},
      new { ID = 9, Name = "ddd"},
      new { ID = 10, Name ="eee"}
    };
    RadGrid1.DataSource = data;
}
 
 
 
 
protected void Button1_Click(object sender, EventArgs e)
{
    ApplyStylesToPDFExport(RadGrid1.MasterTableView);
    RadGrid1.MasterTableView.ExportToPdf();
    //RadGrid1.Rebind();
}
 
private void ApplyStylesToPDFExport(GridTableView view)
{
    // Get access to the header of the grid
    GridItem headerItem = view.GetItems(GridItemType.Header)[0];
 
    // Apply some css style to the header
    headerItem.Cells.Count.ToString();
 
    foreach (TableCell cell in headerItem.Cells)
    {
        cell.Style["font-family"] = "Verdana";
        cell.Style["text-align"] = "left";
        cell.Style["vertical-align"] = "middle";
        cell.Style["font-size"] = "10px";
    }
 
    // Get access to the data of the grid
    GridItem[] dataItems = view.GetItems(GridItemType.Item);
 
    // Apply some css style to the data items
    foreach (GridItem item in dataItems)
    {
        foreach (TableCell cell in item.Cells)
        {
            cell.Style["font-family"] = "Verdana";
            cell.Style["text-align"] = "left";
            cell.Style["vertical-align"] = "middle";
            cell.Style["font-size"] = "10px";
            cell.Style["background-color"] = "red";
        }
    }
}



Thanks,
Jayesh Goyani
0
Kimberly
Top achievements
Rank 1
answered on 20 Nov 2012, 08:44 PM
Jayesh,

Thank you for your reply!  I am still seeing the same behaviour.  I tried this in both Mozilla Firefox and IE.  Do you know what else I might be missing that would cause this problem?  Please let me know if you need for me to provide you with more information.

Thanks!!

Kim
0
Kostadin
Telerik team
answered on 23 Nov 2012, 12:35 PM
Hello Kimberly,

The code you provided looped only through the Items and not the AlternateItems. In order to loop through all the items you could use the following approach.
private void ApplyStylesToPDFExport(GridTableView view)
    {
        // Get access to the header of the grid
        GridItem headerItem = view.GetItems(GridItemType.Header)[0];
 
        // Apply some css style to the header
        headerItem.Cells.Count.ToString();
 
        foreach (TableCell cell in headerItem.Cells)
        {
            cell.Style["font-family"] = "Verdana";
            cell.Style["text-align"] = "left";
            cell.Style["vertical-align"] = "middle";
            cell.Style["font-size"] = "3px";
        }
 
        // Get access to the data of the grid
        GridDataItemCollection dataItems = RadGrid1.MasterTableView.Items;
 
        // Apply some css style to the data items
        foreach (GridItem item in dataItems)
        {
            foreach (TableCell cell in item.Cells)
            {
                cell.Style["font-family"] = "Verdana";
                cell.Style["text-align"] = "left";
                cell.Style["vertical-align"] = "middle";
                cell.Style["font-size"] = "3px";
                cell.Style["background-color"] = "red";
            }
        }
    }//end of ApplyStylesToPDFExport


Regards,
Kostadin
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
Kimberly
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Kimberly
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or