简体   繁体   中英

Adding empty number of rows in gridview based on grid rows count

Hi i am looking for having 10 records in the grid-view by default

so if the grid-view has 3 rows then there are 7 empty rows to add,

if grid-view has 4 rows then there are 6 empty rows, etc..

also if grid-view has 10 rows there's no empty rows to add

QueryClass q = new QueryClass();
grid.DataSource = q.getdata();
grid.DataBind();

Method

public DataTable getdata()
{
string query = string.Format("SELECT TOP(10) * FROM Store");
return Search(query);
}

You could create an extension method for this:

public static class HelperMethod
{
    public static List<QueryClassObj> Extend(this List<QueryClassObj> items)
    {
        while (items.Count < 10)
        {
            items.Add(new QueryClassObj());
        }
        return items;
    }
}

When you get the data and bind to your grid simply:

QueryClass q = new QueryClass();
grid.DataSource = q.getdata().Extend();
grid.DataBind();

Based on your edit you can try this

public DataTable getdata()
{
string query = string.Format("SELECT TOP(10) * FROM Store");
DataTable results= Search(query);
while(results.Rows.Count<10)
{
 results.Rows.Add(results.NewRow());
}
}

Sorry I have not been able to test my last edit as I'm away from PC

You can Add the missing items to your datasource:

QueryClass q = new QueryClass();

var dSource = q.getdata();

foreach (var missing in Enumerable.Range(1, 10 - dSource.Rows.Count))
{
    dSource.NewRow();
}
grid.DataSource = dSource;

grid.DataBind();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM