Posts Tagged 'c#'

C# Listbox flickering, do not use height property

I’ve created a new ASP.NET 2.0 page, inherited a menu bar from a master page and put a listbox that is binded to a dataset. Whenever I move my mouse cursor over the menu bar, the listbox would flicker. Googling the term “C# listbox flicker” yeild solutions such as Double Buffering, custom control, .BeginUpdate and .EndUpdate, just to name a few.

And then it struck me. I tested the same page in Firefox, and the list only displayed 18.5 rows. Yes, half a row, IE6 displayed 19 full rows. That tells me it has something to do with the height property. Sure enough, remove the Height property and instead put in a specific number of rows in the Rows property solved the problem.

Reference: Vikram Lakhotia

Problems with DataGridView control in C#

So I was having problems with DataGridView control.  I created a DataGridView and have added a collection of columns that corresponds to the columns in my custom object.

The I binded my custom object to the DataSource programmatically in my code, but for the life of me, I couldn’t work out why it is not displaying in the grid.  After spending something like an hour stepping through my code, I finally figured it out.  I need to enter the column name in the DataPropertyName.

You can do that in Designer View.  Right-click on the DataGridView control and select Edit Column.  Select the column you want, and set the DataPropertyName to the column name that is in your custom object.

A little example, you have a People object that has one property – Name, and you want to bind People to DataGridView1.

1) Create DataGridView1 control on the form
2) Right click on DataGridView1 and “Add…” column Name (or whatever you want).
3) Here is the most important bit.  In your Name column, there is a property called DataPropertyName.  Type “Name” in it.  This has to match the property name of your object.
4) Create a custom object People.
5) In your code, bind your DataSource to the People object.  Probably look something like this: DataGridView1.DataSource = People;

There you have it.

BTW, I have turned off AutoGenerateColumns.  That could be another reason why my values weren’t showing up.



Follow

Get every new post delivered to your Inbox.