SIGN UP MEMBER LOGIN:    
ARTICLE

Custom Functions for Datatable

Posted by Amit Choudhary Articles | ASP.NET Controls in C# April 01, 2010
Here is some custom function that can be useful while dealing with datatables.
Reader Level:

Hi friends, more of the time when we need to shuffle the records or getting top n rows while retrieving the data from database. We often use database function in our stored procedures.

But what if once you have fetched the data in datatable and now you have requirement to perform these two task with you tabular data. The solution that comes in mind is, create a custom function and get the desired result from datatable. 

Here is the some custom function that can be useful while dealing with datatables.

1. Shuffling the DataTable

/// <summary>
/// Randomizes the order of the rows in a DataTable by pulling out a single row and moving it to the end for
/// shuffleIterations iterations.
/// </summary>
/// <param name="inputTable"></param>
/// <param name="shuffleIterations"></param>
/// <returns></returns>
public static DataTable shuffleTable(DataTable inputTable, int shuffleIterations)
{
    int index;
    System.Random rnd = new Random();
    // Remove and throw to the end random rows until we have done so n*3 times (shuffles the dataset)
    for (int i = 0; i < shuffleIterations; i++)
    {
        index = rnd.Next(0, inputTable.Rows.Count - 1);
        inputTable.Rows.Add(inputTable.Rows[index].ItemArray);
        inputTable.Rows.RemoveAt(index);
    }
    return inputTable;
}

2. Getting Top n rows from DataTable

/// <summary>
/// Return Top n rows from a datatable
/// </summary>
/// <param name="dt">Source DataTable that is to be filetered</param>
/// <param name="n">No. of records you need in output datatable</param>
/// <returns>DataTable Contains the Resuling n rows</returns>
public DataTable getTop(DataTable dt, int n)
{
    DataTable outputTable = dt.Clone();
    for (int i = 0; i < n; i++)
        outputTable.ImportRow(dt.Rows[i]);
    return outputTable;
}

Well these custom function can be very useful to you guys so I am sharing it with you.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor