Live Chat Icon For mobile
Live Chat Icon

How to display ‘No data exists’ within the datagrid rather than just showing Column Headers with no rows

Platform: ASP.NET| Category: DataGrid

<asp:DataGrid id='DataGrid1' OnItemDataBound ='ItemDB' runat='server'></asp:DataGrid>

VB.NET


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	’ Put user code to initialize the page here
        	If Not Page.IsPostBack Then
            		’Populate the dataSet
            		’Bind the dataGrid with the dataView
            		DataGrid1.DataSource = BindTheDataClass.Binddata().Tables(0).DefaultView
            		DataGrid1.DataBind()
        	End If
End Sub ’Page_Load

Protected Sub ItemDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
	Dim dv As DataView = CType(DataGrid1.DataSource, DataView)
        	Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
        	If dv.Table.Rows.Count = 0 Then
            		’By default the Datagrid Header is shown in case there is no Data Available
            		’So in case of No Data found 
            		’Check the ListItemType.Header
            		If e.Item.ItemType = ListItemType.Header Then
	                	Dim i As Integer = e.Item.Cells.Count

		                ’Assign 'No Search result Found' in one of the cells of DataGrid
		                e.Item.Cells(0).Text = 'No Search Results Found'

		                ’Remove Rest of the empty cells from Datagrid
		                Dim j As Integer
		                For j = i - 1 To 1 Step -1
			                    e.Item.Cells.RemoveAt(j)
		                Next 
          	  	End If
        	End If
End Sub ’ItemDB

C#


private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page	here
	if (!Page.IsPostBack )
	{
		//Fill DataSet
		//Bind the DataGrid with the DataView
		DataGrid1.DataSource =ds.Tables[0].DefaultView ;
		DataGrid1.DataBind ();
	}
}

protected void ItemDB(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
	DataView dv =(DataView)DataGrid1.DataSource ;
	DataRowView drv = (DataRowView)e.Item.DataItem ;
	if (dv.Table.Rows.Count == 0 )
	{
		//By default the Datagrid Header is shown in case there is no Data Available
		//So in case of No Data found 
		//Check the ListItemType.Header
		if ((e.Item.ItemType == ListItemType.Header))
		{
			int i= e.Item.Cells.Count;

			//Assign 'No Search result Found' in one of the cells of DataGrid
			e.Item.Cells [0].Text = 'No Search Results Found';

			//Remove Rest of the empty cells from Datagrid
			for (int j=i-1;j>0;j--)
			{
				e.Item.Cells.RemoveAt(j);
			}
		}
	}
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.