This is a migrated thread and some comments may be shown as answers.

No data to display

8 Answers 468 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Surendar S
Top achievements
Rank 1
Surendar S asked on 31 Dec 2010, 10:59 AM
Hi, 
    I am using Radgridview and define columns using the smart tag in design window. Every thing works fine. If there is no data available to display in the grid, it should give the message "No data to display". But this does not happen. How should i acheive this?. I get the message only if i dont define columns and use AutoGenerateColumns=True.

With Regards,
S. Surendar.

8 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 31 Dec 2010, 02:29 PM
Hello,

This should work for you.
Me.RadGridView1.DataSource = dataSource
If Me.RadGridView1.RowCount = 0 Then
    Me.RadGridView1.TableElement.Text = "No Data to display"
End If

hope that helps
Richard
0
Surendar S
Top achievements
Rank 1
answered on 03 Jan 2011, 10:38 AM
Hi Richard,
    This is not working for me. I have defined my columns in the GridViewDataColumn Collection Editor present in the grid's smart tag.Now if there is no data to be displayed, the grid will show the column header with no message like "No Data to display".

With Regards,
S Surendar.
0
Richard Slade
Top achievements
Rank 2
answered on 03 Jan 2011, 11:14 AM
Hi,

What version of the controls are you using? if you are not using the latest version I'd suggest upgrading as there are many enhancements in the latest version.
If you are using the latest, please post a sample where this can be replicated and I will do my best to help
Richard
0
Emanuel Varga
Top achievements
Rank 1
answered on 03 Jan 2011, 11:24 AM
Hello,

The problem in the scenario described is that because you have defined the columns for the grid, it is assumed that the user can always add new data to the grid (using the new row), that's why the grid creates (and shows the column headers, the grouping row and other elements).
In my point of view, there are a couple of ways of handling this, you could either use the SaveLayout / LoadLayout of the grid, so whenever you don't have any data you can just save the layout and clear everything in the grid, and the No data... text will appear, and on the RowsChanging event you can just load the layout, like in the following example:

using System;
using System.IO;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
    private RadButton addButton;
    private Stream layout;
 
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.Dock = DockStyle.Fill;
        radGridView1.Columns.Add(new GridViewDecimalColumn("Id"));
        radGridView1.Columns.Add(new GridViewTextBoxColumn("Name"));
 
        radGridView1.RowsChanging += new GridViewCollectionChangingEventHandler(radGridView1_RowsChanging);
 
        this.Controls.Add(addButton = new RadButton());
        addButton.Text = "Add Data";
        addButton.Dock = DockStyle.Bottom;
        addButton.Click += new EventHandler(addButton_Click);
    }
 
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
 
        layout = new MemoryStream();
        radGridView1.SaveLayout(layout);
        radGridView1.Columns.Clear();
    }
 
    void radGridView1_RowsChanging(object sender, GridViewCollectionChangingEventArgs e)
    {
        if (radGridView1.RowCount == 0 && e.Action == Telerik.WinControls.Data.NotifyCollectionChangedAction.Add)
        {
            layout.Position = 0;
            radGridView1.LoadLayout(layout);
        }
    }
 
    void addButton_Click(object sender, EventArgs e)
    {
        radGridView1.Rows.Add(new object[] { radGridView1.RowCount, "Rows: " + radGridView1.RowCount });
    }
}

Of if this does not suit your need we can always check for readonly and hide all the visible elements if there aren't any rows, and make them visible again, once a row has been added, something like this:
using System;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form2 : Form
{
    private RadGridView radGridView1;
    private RadButton addButton;
 
    public Form2()
    {
        InitializeComponent();
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.Dock = DockStyle.Fill;
        radGridView1.Columns.Add(new GridViewDecimalColumn("Id"));
        radGridView1.Columns.Add(new GridViewTextBoxColumn("Name"));
 
        radGridView1.RowsChanging += new GridViewCollectionChangingEventHandler(radGridView1_RowsChanging);
 
        this.Controls.Add(addButton = new RadButton());
        addButton.Text = "Add Data";
        addButton.Dock = DockStyle.Bottom;
        addButton.Click += new EventHandler(addButton_Click);
    }
 
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
 
        ChangeGridInternalsVisibility(false);
    }
 
    private void ChangeGridInternalsVisibility(bool visible)
    {
        if (visible)
        {
            radGridView1.TableElement.ViewElement.Visibility = Telerik.WinControls.ElementVisibility.Visible;
            radGridView1.TableElement.Text = string.Empty;
        }
        else
        {
            radGridView1.TableElement.ViewElement.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
            radGridView1.TableElement.Text = Telerik.WinControls.UI.Localization.RadGridResLocalizationProvider.CurrentProvider.GetLocalizedString(Telerik.WinControls.UI.Localization.RadGridStringId.NoDataText);
        }
 
        if (radGridView1.EnableGrouping)
        {
            radGridView1.ShowGroupPanel = visible;
        }
    }
 
    void radGridView1_RowsChanging(object sender, GridViewCollectionChangingEventArgs e)
    {
        if (radGridView1.RowCount == 0 && e.Action == Telerik.WinControls.Data.NotifyCollectionChangedAction.Add)
        {
            ChangeGridInternalsVisibility(true);
        }
    }
 
    void addButton_Click(object sender, EventArgs e)
    {
        radGridView1.Rows.Add(new object[] { radGridView1.RowCount, "Rows: " + radGridView1.RowCount });
    }
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Richard Slade
Top achievements
Rank 2
answered on 03 Jan 2011, 11:39 AM
Hi,

Even if you have the columns already defined, this should not be a problem and you can still set the No data text.
Please try this.
Me.RadGridView1.Columns.Add(New GridViewTextBoxColumn("A"))
Me.RadGridView1.Columns.Add(New GridViewTextBoxColumn("B"))
Dim people As New BindingList(Of String)
Me.RadGridView1.DataSource = people
If Me.RadGridView1.RowCount = 0 Then
    Me.RadGridView1.TableElement.Text = "No Data to display"
End If

here is a screenshot of defined columns and No Data text

Regards,
Richard
0
Emanuel Varga
Top achievements
Rank 1
answered on 03 Jan 2011, 11:42 AM
Hello Richard,

Happy New Year!

I think what Surendar is trying to say is that he wants the grid to behave like it would if nothing would have been defined for that grid (no columns, no data source) just an empty white space with a no data text, and to achieve this without loosing all of the columns you have to either hide all of the elements or just save / load the layout.

Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Richard Slade
Top achievements
Rank 2
answered on 03 Jan 2011, 11:46 AM
Hi Emanuel,

Happy New Year to you too. Hope you had a good start to the year.
If that's correct, then I agree with your posting, but otherwise, I would have thought it would be enough to show the No Data Text with the column headers as per my example.

All the best
Richard
0
Surendar S
Top achievements
Rank 1
answered on 03 Jan 2011, 12:47 PM
What Richard said is right. I made a mistake in the coding and just now identified it. Sorry for that. Thank you
 Wish you a happy new year.

With Regards,
S Surendar
Tags
GridView
Asked by
Surendar S
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Surendar S
Top achievements
Rank 1
Emanuel Varga
Top achievements
Rank 1
Share this question
or