Apr
4
2010
xiao
Suppose you fill your table with data from a database, you wouldn’t want it to do so while in Visual Studio’s Designer. Control.DesignMode ought to be the perfect property to check for in your form initialisations but unfortunately, it’s so buggy that it hardly does what you need. It doesn’t work in the constructors and have problems with user controls.
Then people tend to use
If (Process.GetCurrentProcess().ProcessName == "devenv")
which works fine if you call it once but inside a key user control’s initialiser, it may be called hundreds of time and cause a massive memory leak. Currently, the ProcessName string never gets freed and fills up memory very fast. Instead, save the process name in your own global variable when the program starts and check against
If (MyProcessName == "devenv")
instead.
no comments | tags: .NET, C, design mode, Designer, memory leak, run-time, vb, visual basic | posted in Tips
Mar
22
2010
xiao
I don’t know actually since most of the relevant stuff becomes read-only after filling with data. But my approach is solve this as early on in the process as possible to the entirety of the data and save processing time on a custom loop after.
In other words, your DataGridView is typically bound to a DataTable. That is typically filled with stuff from the database. Make sure your AutoGenerateColumns property is set to true in the DataGridView and select your data to match the type you want to display in the DataGridView. For instance, to get checkboxes,
SELECT ..., CAST(column1 AS bit), ... FROM ...
and change the data type right out of the DataAdapter and DataGridView will figure out the rest automatically
no comments | tags: .NET, C, Microsoft, SQL, vb, visual basic | posted in Tips
Jun
21
2009
xiao
There are too many top Google search results giving the impression that it is impossible to compare 2 DataRows for its value contents and the programmer needs to iterate through everything himself.
Such is no longer true in .NET 3.5 but the blogosphere doesn’t seem to have caught on yet.
It is possible, given 2 DataTables or any other enumerable object types, to compare its contents using LINQ in just one line of code.
I will give an example in VB where I’d want to compare all column properties (description, data type, field length, etc) of all tables in 2 supposedly identical SQL databases. Continue reading
no comments | tags: .NET, 3.5, column, compare, content, data type, datarow, datatable, difference, how to, LINQ, Microsoft, property, reference, SQL, table, value, vb, visual basic | posted in Tips
May
8
2009
xiao
I need to do this all the time and don’t have the best memory in the world. Today, I decided that I looked this up one too many times so here’s my solution to this multithreading problem:
The Problem:
You try to modify UI components created in one thread in another thread. In VB, you can only make UI changes on the same thread that created it so you get a nice “Cross thread operation not valid” exception.
Here’s the wrong code:
thread = New System.Threading.Thread(AddressOf DoStuff)
thread.Start()
Private Sub DoStuff()
'error occurs here'
Me.Text = "Stuff"
End Sub
Continue reading
1 comment | tags: .NET, Access, addressof, cross, delegate, invoke, Microsoft, multithreading, thread, vb, visual basic | posted in Tips