Archive | February, 2009

String Replicate

3 Feb

The simplest way to replicate (repeat) a string in C# is using string class constructor:

string s = new string('#', 4); //result: ####

If you want to replicate a sub-string (not only a char), use this:

string.Concat(System.Collections.ArrayList.Repeat("#0",4).ToArray()); //result: #0#0#0#0

DataBound ComboBox Problem

3 Feb

When you want to use a data bound ComboBox in .NET (C#, VB, etc), you should set a few properties such as DataSource, DisplayMember and ValueMember.
It works fine unless you want to set SelectedValue for the first time. It won’t be set!

I found that BindingContext property must be created before initializing SelectedValue:

ComboBox1.BindingContext = new BindingContext();
ComboBox1.DataSource = MyDataSet.DefaultView
ComboBox1.DisplayMember = "Title";
ComboBox1.ValueMember = "Code";
ComboBox1.SelectedValue = 1;
%d bloggers like this: