Friday, October 19, 2012

How to Get the Selected Cells and Rows in the Windows Forms DataGridView Control


Here basically I am showing how to get selected cells/rows in DataGridView Control and use of IEnumerator while accessing them.



                DataGridViewSelectedRowCollection m = dataGrid_ZipList.SelectedRows;
                DataGridViewRow row;
 
                IEnumerator Enumerator = m.GetEnumerator();
                Enumerator.Reset();
                while (Enumerator.MoveNext())
                {
                    row = (DataGridViewRow)Enumerator.Current;
                    lstPostalCodes.Add(row.Cells["POSTAL_CODE"].Value.ToString());                   
                }

Tuesday, October 16, 2012

SQL Query to find out the frequency of each element in a column

Suppose we have a table "Person" as:

IdLastNameFirstNameAgeCity
1SmithJohn33Newyork
2SmithMike34Boston
3PaulRyan39Chicago
4WilliamBrian45Baltimore
5RaoSunita41Dayton
6ZingSue33Owings Mills
7SmithRobert45Newyork
8PaulZean33Chicago
9SrivastavaHemant33Baltimore
10RaoVenkat45Dayton


and we need to find out the frequency of the each age.


   SELECT Age, COUNT(Age)AS Frequency    
   FROM Persons   
   GROUP BY Age   
   ORDER BY    
   COUNT(Age) DESC
 

We get following result:
Age
Frequency
33
4
45
3
34
1
39
1


Now suppose we want to get the ‘Age’ which occurs more than equal to 2. For this query, we need to add HAVING clause after GROUP BY clause.



SELECT Age, COUNT(Age)AS Frequency
FROM Persons
GROUP BY Age 
HAVING  COUNT(Age) >= 2

That gives result as:
Age
Frequency
33
4
45
3


Selecting Columns in a Data Table in C#

“How to select multiple columns in a Data Table” … This is one of the questions asked by a reader in my article published at C# Corner site:
http://www.c-sharpcorner.com/UploadFile/0f68f2/querying-a-data-table-using-select-method-and-lambda-express/

So, I am showing here how we can do this. Here are three ways by which we could select multiple columns in a DataTable object.

(1) Using LINQ

This is a quite flexible way to do this. In this approach, we could do a lot using LINQ e.g. setting filter criteria over the rows, making joins of the table etc.     

   1:  var query = (
   2:                              from r in dt.AsEnumerable()
   3:                              select new
   4:                              {
   5:                                  SSN = r["SSN"].ToString(),
   6:                                  NAME = r["NAME"].ToString(),
   7:                              }
   8:                          ).ToList();
   9:   
  10:              foreach (var item in query)
  11:              {
  12:                  Console.WriteLine(item.SSN + "\t" + item.NAME);
  13:              }
 
(2) Using DataView class:

This is also a way to get a the view on Data table.

   1:              DataView view = new DataView(dt);
   2:              DataTable distinctValues = view.ToTable("dt", true, "SSN", "NAME");
   3:   
   4:              foreach (DataRow o in distinctValues.Select())
   5:              {
   6:                  Console.WriteLine("\t" + o["SSN"] + "\t" + o["NAME"]);
   7:              }

(3) Using DataTable.Rows collection

If we use this approach, we don’t have much scope. We could only enumerate the rows, read the rows, edit the rows like that…

   1:              for (int i = 0; i < dt.Rows.Count; i++)
   2:              {
   3:                  Console.WriteLine(dt.Rows[i]["SSN"]+ "\t"+ dt.Rows[i]["NAME"]);
   4:              }