I Love Stackoverflow
I Love Stackoverflow

Reputation: 6868

How to display no records found message in auto generated telerik gridview from datatable?

I am dynamically creating GridView with dataset but getting problem when I have no Records in my GridView so in that case I want to show my GridView with all that columns with No Records Found.

This is my code but GridView is not visible:

 var table = new DataTable();
 table .Columns.Add("Source");
 table .Columns.Add("Destination");
 table .Columns.Add("Date");

If()
{
  //All my logics where i am generating value for this "Source,Destination,Date" Column.
  foreach (var item in data)
  {
     table.Rows.Add(Source,Destination,Date);
  }
  //Binding to Gridview
 GridView1.DataSource = dt;
 GridView1.DataBind();
}
else
{
   //No Records so display gridview with "No records" along with columns.
      GridView1.DataSource = dt; //Here i will not be having any data for this 3 columns Source,Destination,Date
      GridView1.DataBind();//But My gridview is not visible
}

Aspx:

<telerik:RadGrid ID="GridView1" runat="server">
                            <MasterTableView AutoGenerateColumns="true" NoMasterRecordsText="No Records Found">
                                <Columns>
                                </Columns>
                            </MasterTableView>
                        </telerik:RadGrid>

Expected output:

enter image description here

Upvotes: 2

Views: 2551

Answers (2)

Rahul Nikate
Rahul Nikate

Reputation: 6337

You need to change NoMasterRecordsText property for MasterTableView for gridview.

ASPX:

<MasterTableView NoMasterRecordsText="No Records Found" >

OR

C#:

protected void Page_Load(object sender, EventArgs e)
{
   GridView1.MasterTableView.NoMasterRecordsText = "No Records Found";     
}

EDIT: Binding data to GridView using OnNeedDataSource

<telerik:RadGrid ID="GridView1" runat="server" OnNeedDataSource="GridView1_NeedDataSource">
    <MasterTableView AutoGenerateColumns="true" NoMasterRecordsText="No Records Found">
           <Columns>
           </Columns>
    </MasterTableView>
</telerik:RadGrid>

and in your code behind

// GridView1_NeedDataSource
protected void GridView1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    BindGridViewData();
}

//Bind Grid View 
private void BindGridViewData()
{
 var table = new DataTable();
         table .Columns.Add("Source");
         table .Columns.Add("Destination");
         table .Columns.Add("Date");

       if()
       {

         foreach (var item in data)
         {
            table.Rows.Add(Source,Destination,Date);
         }
         //Binding to Gridview
         GridView1.DataSource = dt;

      }
      else
      {

          GridView1.DataSource = dt; 

      }
}

Upvotes: 2

Genish Parvadia
Genish Parvadia

Reputation: 1455

Gridview ASPX Page Put EmptyDataText

<asp:GridView ID="GvZone" runat="server" AutoGenerateColumns="False" 
    EmptyDataText="No Records Found !"  >
 </asp:GridView>

Upvotes: -1

Related Questions