How to manipulate a Windows Forms control from another thread and get return value via Invoke() method


I was working on a Windows Forms project and I had this requirement to basically do some manipulation on a TreeListView on the parent form (MainForm in this case) for which we already had a developed a method and also to get the return value back from it. To make life even easier, we needed to do this from a BackGroundWorker thread in this case. Now, the problem with Windows Forms controls is that they can’t be manipulated apart from the main thread, so I had to look for a work around of some sort. So here’s a little modified snippet of the code for your understanding:-

if (m.treeListView.InvokeRequired)
{
ra= (ReportData?)m.treeListView.Invoke(new Func<ReportData?>(
() => m.CreateDataforReport(“Rpt type”, false)));
//m is the main form’s reference!

}

So what I’ve done is that I passed the main form’s reference to the child form in the constructor and made the TreeListView and the CreateDataforReport public. Notice that I called CreateDataforReport method inside the Func<ReportData?> and not directly. This is what enables you to get the appropriate return value, be it string, integer or any complex type like I have it here.

Advertisements

The type ‘System.IO.Packaging.Package’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘WindowsBase’


While I was working with the Open Office XML SDK 2.0, after I added the reference to the DocumentFormat.OpenXml assembly and tried to build the application I ran into this error message.

The type ‘System.IO.Packaging.Package’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’

Turns out, all you need to do to fix it is to just add another reference to WindowsBase assembly that is found in the .NET Tab.

Where/How do we place/refer to images in SharePoint 2010 WebParts


Where are the images located in SharePoint project or webpart
The location of images in SharePoint 2010 Project/web part

This one is especially useful for the new bie because when you start with building webparts for SharePoint 2010, you often come to a point where you wanna add a URL somewhere, lets say programatically or in the CSS or may be in the ASCX code and you wonder, hey wait a minute where are the images for SharePoint placed anyway? How do I refer to them? So here’s your answer.

What you do is that you Right Click the Web parts project > Add > SharePoint “Images” Mapped Folder option. Doing that will give you an images folder inside your project directory and you can refer to that like this:

“~/_layouts/Images/ProjectNamesETC/xyz.png”

Have fun!

SPGridView’s OnRowDataBound event returning nulls – Unable to FindControls


Earlier today I was just playing around with SharePoint 2010 and thought I should do a simple task with SPGridView, that is to display the data and show an appropriate image in the “Status” column instead of the text. Hmm, sounds quite simple doesn’t it! But no, it turned into a misery. Well, when I started it was quite natural to use the OnRowDataBound event on that to capture the data and do some modifications with the controls (I was using TemplateFields in the SPGridView by the way), so I did that and I tried  doing things like e.Row.FindControl(“xyz”), e.Row.Cells[2] and using RowIndex property which was returning -1 where e refers to an object of GridViewRowEventArgs and there was nothing to get over there. It was nulls all around!

So here’s a sample of the code in the visual webpart that I was writing:-

<SharePoint:SPGridView ID=”spGvWorkOrders”  runat=”server” >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID=”lbl_workOrderId” Text='<% # Bind(“Id”) %>’  runat=”server”></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField>
<ItemTemplate>
<asp:Label ID=”lbl_workOrderTitle”  runat=”server” Text='<% # Bind(“Title”) %>’ ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>

<asp:Image ID=”img_status” Width=”10px” ImageUrl='<% #StatusToImage(“\””+ Eval(“Status”)+”\””) %>’ runat=”server” />
</ItemTemplate>
</asp:TemplateField>

And here is the function:-

protected string StatusToImage(string status)
{
if ( ( rowcount++ % 2) == 0)
return “~/_layouts/Images/WorkOrders/add.png”;
else
return “~/_layouts/Images/WorkOrders/arrow_up_green.png”;
}

Notice that I had been using the Bind function earlier but when I tried using it to send the value to StatusToImage function it did not work. Eval is the better option anyway in case you’re just interesting in displaying the data.

So what I did was that instead of using the event I just put the function call inside the ascx code so that whenever a row is created that function is called with the value of Status from the Data Source and with that value sent to a function, we can return back a string to give the path to the image file. The syntax however, is quite tricky and it took me a while to figure that out correctly but it worked in the end. Hope it will save you some time though!

Unknown server tag ‘asp:ScriptManager’ After having AjaxControlToolkit.dll referenced


Yesterday I was working on a project which had the AJAX Extensions in use. It’s primarily a .NET 2.0 application and I had .NET Framework 3.5 SP 1 installed on my system. It was working normally and all of a sudden I got this error that: Unknown server tag ‘asp:ScriptManager’

It was strange because every thing was working and I had the reference to the AjaxControlToolkit.dll.

I found out that by adding the section below to the <system.web> node in the web.config solves the problem:-
<pages>

<controls>

<add tagPrefix=”asp” namespace=”System.Web.UI” assembly=”System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ />

</controls>

</pages>