3

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);
}

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

thanks but i didn't understand how to add extension method i added the method i used in question
@Ayman Replace QueryClassObj with the type of object that your q.getdata() returns
system.data.datatable does not contain a definition for Extend accepting a first argument of type system.data.datatable
@Ayman See my edit. The original answer was posted before you showed the getdata()
1

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.