Select top from for datatable and dataview

serkan sendur 0 Tallied Votes 322 Views Share

SelectTopFrom method gets the top rows from dataview or datatable

public static DataTable SelectTopFrom(DataTable dt, int rowCount)
    {
        DataTable dtn = dt.Clone();
        for (int i = 0; i < rowCount; i++)
        {
            dtn.ImportRow(dt.Rows[i]);
        }
        return dtn;
    }
   
    public static DataView SelectTopFrom(DataView dv, int rowCount)
    {
        DataTable dt = CreateTable(dv);
        DataTable dtn = dt.Clone();
        for (int i = 0; i < rowCount; i++)
        {
            dtn.ImportRow(dt.Rows[i]);
        }
        DataView dvn = new DataView(dtn);
        return dvn;
    }
serkan sendur 821 Postaholic Banned

I forgot to write "CreateTable" method above, you can look at it in the snipped named "Convert DataView To DataTable".
I think it is better to seperate these two snippets

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.