Multiple Cell Selection in DataGridView

September 9, 2009

DataGridView has a facility to select multiple cells (or a range of cells) using mouse dragging, Cltrl+Click or Shift+Click. This feature is similar to cell selection in spreadsheet applications such as Microsoft Excel.

In order to enable this feature in DataGridView, use ‘SelectionMode‘ property as follows:

DataGridViewCell.SelectionMode = DataGridViewSelectionMode.CellSelect;

To read/write data from/to selected cells simply write:

foreach (DataGridViewCell cell in dataGridView1.SelectedCells)

{

//read cell data

MessageBox.Show(cell.Value.ToString());

//change cell data

dataGridView1.Rows[cell.RowIndex].Cells[cell.ColumnIndex].Value = 1;

}


Sorting

May 16, 2009

There are several ways to sort data in C# but one of the easiest ways is using BindingSource sort capability.
Depending on what object (underlying list) is linked to the BindingSource as its DataSource and whether it has implemented IBindingList or IBindingListView interfaces or not, you can use this feature.

To find out whether a BindingSource with current DataSource supports sorting, use SupportsSorting property:

if bindingSource1.SupportsSorting
{
bindingSource1.Sort = “Code, Name”;
}

Similary, you can benefit from BindingSource.Filter .


Using SelectionChangeCommitted event of ComboBox

May 12, 2009

If you want to capture changes in ComboBox you can use ‘SelectedIndexChanged’ or ‘SelectedValueChanged’ events. However, if you want to get changes only when the user changes the selection, use ‘SelectionChangeCommitted’ event instead.

SelectionChangeCommitted occurs only when the ComboBox selection changes by user (via keyboard or mouse) and it is not raised when the selection changes programmatically.


String Replicate

February 3, 2009

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(“##”,4).ToArray());


The Beginning of the End

January 17, 2009

I studied most of the book and I was evaluated by a couple of .NET experts in the end. The interview was successful and I officially started developing database applications in C#.

I try to update here as soon as I learn new techniques, tips and tricks. For now, I start with ” LINQ to SQL”.
LINQ to SQL is a kind of sophisticated OR Mapper (Object Relational Mapper) which has several useful features that results easier and simpler development.

This is what Microsoft experts say:
“LINQ to SQL provides a runtime infrastructure for managing relational data as objects without losing the ability to query. Your application is free to manipulate the objects while LINQ to SQL stays in the background tracking your changes automatically.”

For more information please visit:  http://msdn.microsoft.com/en-us/library/bb425822.aspx

P.S.
A friend of mine who showed me the capabilities of LINQ to SQL, also said that this technology is obsolete right now and “Entity Framework” should probably be replaced with it. Entity Framework version 1 was released with .NET framework 3.5 service pack 1 and the next version will be released by .NET framework 4.


Merry Christmas!

December 26, 2008

Long time no update…

I couldn’t start reading the book until last week.
It’s an interesting book and help you to switch to C# in the shortest possible time.
For me, with software designing and programming background, many concepts are similar but of course there are a few new features such as Generics, Attributes, etc. that weren’t in Delphi.
I have been reading two chapters a day and I’m currently reading the 12th chapter.


My first post!

September 7, 2008

Hello everybody!

After 7 years of professional software development in VB and Delphi, I have finally decided to switch to C# .Net. There are several reasons for this decision which I’m not going to explain them.

I’m trying to utilize my previous Delphi experience and learn new programming technics as much as I can.

This blog may be useful for beginners at first and gradually for professionals later when I got improved. Your feedback would be a good motivation for me to continue.

By the way, I’m going to start with “Pro C# 2008 and .NET 3.5 Platform” by Andrew Troelsen.