Search code examples
c#asp.netwebformsteleriktelerik-grid

Is there a way I can force Telerik RadGrid to always show 5 rows regardless of my datasource has that much rows


I want to force Telerik RadGrid to show every 5 row, regardless of how many (lower than 5) rows I have in my data source I'm binding it to. For example, If I have 3 records in my datasource, I want it to show all those three records plus 2 empty rows. I was thinking about adding two empty rows to my datatable I'm binding to, but I wanted to know is there a better way.


Solution

  • Your idea of adding rows to the datatable that the Radgrid is bound to is by far the easiest:

    This is just an example, using the AdventureWorks 2012 db.


    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        String ConnString = @"Data Source=J-PC\SQLEXPRESS;Initial Catalog=AdventureWorks2012;Integrated Security=True";
        SqlConnection conn = new SqlConnection(ConnString);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand("select top 3 AddressID,AddressLine1,City,StateProvinceID,PostalCode from Person.Address ", conn);
    
        DataTable myDataTable = new DataTable();
    
        conn.Open();
        try
        {
            adapter.Fill(myDataTable);
        }
        finally
        {
            conn.Close();
        }
    
        if(myDataTable.Rows.Count < 5)
        {
            DataRow dr = null;
            for (int i = 0; i <= 5-myDataTable.Rows.Count ; i++)
            {
                myDataTable.Rows.Add(new object[]{});
            }
        }
    
        RadGrid1.DataSource = myDataTable;
    }
    

    enter image description here