wpfwinformsdatagridviewwindowsformshost

WPF WindowsFormsHost control is not woking showing error


I am using WindowsFormsHost to show DataGridView in my WPF application.

<wfi:WindowsFormsHost Name="winhost" Grid.Row="0" Width="930" Height="200" Margin="5,5,5,0" Visibility="Hidden">
<wf:DataGridView  x:Name="dataGridViewOutlookMailList" ScrollBars="Both" AllowUserToAddRows="False" RowHeadersVisible="False" CellContentClick="dataGridViewOutlookMailList_CellContentClick">
    <wf:DataGridView.Columns>
        <wf:DataGridViewCheckBoxColumn Name="dataGridViewCheckBoxColumn3" HeaderText="" Width="25" Visible="True" />
        <wf:DataGridViewTextBoxColumn  Name="dataGridViewTextBoxColumn13" HeaderText="Outlook Entry ID" Width="100" Visible="false" />
        <wf:DataGridViewTextBoxColumn  Name="dataGridViewTextBoxColumn14" HeaderText="FullPath With FileName" Width="100" Visible="False" />
        <wf:DataGridViewTextBoxColumn  Name="dataGridViewTextBoxColumn15" HeaderText="Resume Name" Width="100" Visible="True" />
        <wf:DataGridViewTextBoxColumn  Name="dataGridViewTextBoxColumn18" HeaderText="Sender Name" Width="100" Visible="True" />
        <wf:DataGridViewTextBoxColumn  Name="dataGridViewTextBoxColumn17" HeaderText="Received By Name" Width="100" Visible="True" />
        <wf:DataGridViewTextBoxColumn  Name="dataGridViewTextBoxColumn16" HeaderText="Received" Width="100" Visible="True" />
        <wf:DataGridViewTextBoxColumn  Name="dataGridViewTextBoxColumn19" HeaderText="Sender Address" Width="100" Visible="True"  />
        <wf:DataGridViewTextBoxColumn  Name="dataGridViewTextBoxColumn20" HeaderText="To" Width="100" Visible="True" />
        <wf:DataGridViewTextBoxColumn  Name="dataGridViewTextBoxColumn21" HeaderText="Bcc" Width="100" Visible="True" />
        <wf:DataGridViewTextBoxColumn  Name="dataGridViewTextBoxColumn22" HeaderText="Cc"  Width="100" Visible="True" />
        <wf:DataGridViewTextBoxColumn  Name="dataGridViewTextBoxColumn23" HeaderText="Subject" Width="100" Visible="True" />
        <wf:DataGridViewTextBoxColumn  Name="dataGridViewTextBoxColumn24" HeaderText="Body" Width="100" Visible="True" />
        <wf:DataGridViewCheckBoxColumn Name="dataGridViewCheckBoxColumn4" HeaderText="NotesActivity" Width="25" Visible="False" />
        <wf:DataGridViewCheckBoxColumn Name="dataGridViewCheckBoxColumn5" HeaderText="Body As Attachment" Width="100" Visible="False" />
    </wf:DataGridView.Columns> 
</wf:DataGridView>

I am getting following error while mouse over on datagridview also the UI is not loaded properly. see the screen shot for details.

enter image description here

I am also using following code

WinForms.DataGridViewCellStyle ColStyle1 = new WinForms.DataGridViewCellStyle();
System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection();
pfc.AddFontFile(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\NewZrr\OpenSans-Regular.ttf");
foreach (WinForms.DataGridViewColumn c in dataGridViewOutlookMailList.Columns)
{
    c.DefaultCellStyle.Font = new System.Drawing.Font(pfc.Families[0], 8, System.Drawing.FontStyle.Regular);
}
pfc = null; 

in my Window_Loaded method to change the font of datagridview. kindly guide me to resolve the issue.


Solution

  • The problem is that the PrivateFontCollection instance in the pfc variable goes out of scope, so when the DataGridViewCell is drawn for the first time the specified font cannot be loaded correctly.

    Move the pfc instance outside the Window_Loaded method to prevent GC from collecting it, and do not set it to null, make it a long-living object.

    Reference: Parameter is not valid when draw text in label with custom font