C#: multiline text in DataGridView control

You should set DefaultCellStyle.WrapMode property of column to DataGridViewTriState.True. After that text in cells will be displayed correctly. Example (DataGridView with one column): dataGridView1.Columns[0].DefaultCellStyle.WrapMode = DataGridViewTriState.True; dataGridView1.Rows.Add(“test” + Environment.NewLine + “test”); (Environment.NewLine = \r\n in Windows)

Test events with nunit

Checking if events were fired can be done by subscribing to that event and setting a boolean value: var wasCalled = false; foo.NyEvent += (o,e) => wasCalled = true; … Assert.IsTrue(wasCalled); Due to request – without lambdas: var wasCalled = false; foo.NyEvent += delegate(o,e){ wasCalled = true;} … Assert.IsTrue(wasCalled);

static readonly field initializer vs static constructor initialization

There is one subtle difference between these two, which can be seen in the IL code – putting an explicit static constructor tells the C# compiler not to mark the type as beforefieldinit. The beforefieldinit affects when the type initializer is run and knowing about this is useful when writing lazy singletons in C#, for … Read more

How to use LogonUser properly to impersonate domain user from workgroup client

Very few posts suggest using LOGON_TYPE_NEW_CREDENTIALS instead of LOGON_TYPE_NETWORK or LOGON_TYPE_INTERACTIVE. I had an impersonation issue with one machine connected to a domain and one not, and this fixed it. The last code snippet in this post suggests that impersonating across a forest does work, but it doesn’t specifically say anything about trust being set … Read more

How to get list of one column values from DataTable?

You can use Linq to DataTable: var ids = dt.AsEnumerable().Select(r => r.Field<int>(“id”)).ToList(); UPDATE: Without Linq List<int> ids = new List<int>(dt.Rows.Count); foreach(DataRow row in dt.Rows) ids.Add((int)row[“id”]); Note for efficiency it’s better to use row[index] instead of row[columnName]. First one just gets column by index from columns array. Latter gets column index from internal dictionary which maps … Read more

Forwarding events in C#

Absolutely: class B { private A m_a = new A(); public event EventType EventB { add { m_a.EventA += value; } remove { m_a.EventA -= value; } } } In other words, the EventB subscription/unsubscription code just passes the subscription/unsubscription requests on to EventA. Note that this doesn’t allow you to raise the event just … Read more