如何在数据表中自动生成telerik gridview中显示没有找到记录的消息?

时间:2015-12-28 06:16:11

标签: c# asp.net gridview datatable telerik-grid

我正在使用GridView动态创建dataset,但在我的GridView中没有记录时会出现问题,所以在这种情况下,我希望向GridView显示所有内容No Records Found的列。

这是我的代码,但GridView不可见:

 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>

预期产出:

enter image description here

2 个答案:

答案 0 :(得分:2)

您需要为gridview更改NoMasterRecordsText的{​​{1}}属性。

ASPX:

MasterTableView

OR

C#:

<MasterTableView NoMasterRecordsText="No Records Found" >

编辑:使用OnNeedDataSource将数据绑定到GridView

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

并在您的代码中

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

答案 1 :(得分:-1)

Gridview ASPX Page Put EmptyDataText

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