Saturday, 30 May 2015

Bind Data to DataGridview using Lamba expression

If we want to bind list of objects to datagridview using coding ,we can do that by using
the following approach :-

For e.g:- If we have list of custom class that we want to bind to datagridview
List<ClassName> searchList;
 this.transactiondataGridView.DataSource = searchList.Select(x => new

{

    x.TrustAccount.Code,
 
    x.Amount,

    x.Payor,

    x.SettlementDate,

    x.PropertyAddress,

    x.Borrower

}).ToList();






                   


 




 

Wednesday, 27 May 2015

Adding file structure using treeview

  private void ListDirectory(TreeView treeView, string path)
        {
            treeView.Nodes.Clear();
            var rootDirectoryInfo = new DirectoryInfo(path);
            treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
        }

        private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
        {
            var directoryNode = new TreeNode(directoryInfo.Name);
            foreach (var directory in directoryInfo.GetDirectories())
                directoryNode.Nodes.Add(CreateDirectoryNode(directory));
            foreach (var file in directoryInfo.GetFiles())
                directoryNode.Nodes.Add(new TreeNode(file.Name));
            return directoryNode;
        }

Wednesday, 6 May 2015

Binding List to datagridview using LINQ


List<string> listFiles=new List<string>();
//FIll your list with data that you want to display in datagridview

documentsDataGridView.DataSource = listFiles.Select(x => new { ColmnName1 = Path.GetFileName(x), ColumnName2 = x }).ToList();

//This will create two columns in datagridview with the name as :-ColmnName1 /ColmnName2
 

Tuesday, 5 May 2015

Merging Two rows in sql

How to combine two or more rows corresponding to same id to remove duplicate rows


step 1 : Create Table #EscrowNo(rootId int,reference nvarchar(50))

steo2 : insert into #EscrowNo (select distinct RootId# ,

STUFF((Select ','+ReferenceNumber

from [pfm].[Contact] pfmEscrow)

where pfmEscrow.RootId#=pfmEscrow2.RootId#

FOR XML PATH('')),1,1,'') from [pfm].[Contact] pfmEscrow2

step 3 : select * from #EscrowNo

Result:
You have merged all the rows corresponding to similar id in one row

Monday, 4 May 2015

How to delete duplicate rows table in sql

Deleting duplicate rows

e.g:- Suppose that we have a table named "Employees" having auto increment id and we want to delete latest duplicate row. In the following table we would like to delete the latest duplicate salary row, which in this case is [id=3,name=f,salary=200]




DELETE FROM [Data].[dbo].[employees] 
WHERE  id IN (SELECT TOP(1)id 
              FROM   [Data].[dbo].[employees] 
              WHERE  salary IN (SELECT salary 
                                FROM   [Data].[dbo].[employees] 
                                GROUP  BY salary 
                                HAVING Count(*) > 1) 
              ORDER  BY id DESC) 

After executing this query you will get a table without any duplicate salary