6 Replies - 30822 Views - Last Post: 05 November 2008 - 01:39 PM Rate Topic: -----

#1 seko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 08-November 07

displaying gridview without any record in table

Posted 09 November 2007 - 05:25 AM

gridview is not displaying if there is no record in table.But I have to show first gridview and then user select a row to add a new record or update or delete.
Is This A Good Question/Topic? 0
  • +

Replies To: displaying gridview without any record in table

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: displaying gridview without any record in table

Posted 09 November 2007 - 06:30 AM

If you're wanting an empty GridView to show, even when no records are returned, so you can make sure the default functionality of adding a row are available, before binding the GridView to your DataSet, check to see if the DataSet contains any rows, if it doesn't then add an "empty" row to it, then bind it to the GridView.

You will want to "clone" the original underlying data source for your GridView so as to not be modifying the original datasource for your grid, this can be done in either VB.Net or C#, and since I dont know the language you're using Ill demonstrate both. Something like this in VB.Net

'Check to ensure we have zero rows in our GridView
'and that our GridView has been assigned a DataSource
If GridView1.Rows.Count = 0 AndAlso GridView1.DataSource <> Nothing Then
		'Create ourselves a DataTable to
		'hold our "dummy" row
	Dim dt As DataTable = Nothing

	' need to clone sources otherwise it will be indirectly adding to 
	' the original source
		'Check to see if the DataSource of our grid is a DataTable or DataSet
	If TypeOf GridView1.DataSource Is DataSet Then
		dt = (DirectCast(GridView1.DataSource, DataSet)).Tables(0).Clone()
		ElseIf TypeOf GridView1.DataSource Is DataTable Then
		dt = (DirectCast(GridView1.DataSource, DataTable)).Clone()
	End If
		'Check to ensure our DataTable object was assigned a value
	If dt = Nothing Then
		Return
	End If
		'Now we add our empty row
	dt.Rows.Add(dt.NewRow())
	'Bind our GridView
	GridView1.DataSource = dt
	GridView1.DataBind()

	'Hide the "dummy" row we inserted
	GridView1.Rows(0).Visible = False
	GridView1.Rows(0).Controls.Clear()
End If



Something like this in C#:

//Check to ensure we have zero rows in our GridView
//and that our GridView has been assigned a DataSource
if (GridView1.Rows.Count == 0 &&  GridView1.DataSource != null)
{
	//Create ourselves a DataTable to
	//hold our "dummy" row
	DataTable dt = null;
	
	// need to clone sources otherwise it will be indirectly adding to 
	// the original source
	//Check to see if the DataSource of our grid is a DataTable or DataSet
	if (GridView1.DataSource is DataSet)
	{
		dt = ((DataSet)GridView1.DataSource).Tables[0].Clone();
	}
	else if (GridView1.DataSource is DataTable)
	{
		dt = ((DataTable)GridView1.DataSource).Clone();
	}
	//Check to ensure our DataTable object was assigned a value
	if (dt == null)
	{
		return;
	}
	// now we add our empty row
	dt.Rows.Add(dt.NewRow()); 
	//Bind our GridView
	GridView1.DataSource = dt;
	GridView1.DataBind();		

	//hide our "dummy" row
	GridView1.Rows[0].Visible = false;
	GridView1.Rows[0].Controls.Clear();
}



Now you have 2 examples in 2 different languages, that should at least give you an idea of where to go next :)
Was This Post Helpful? 0
  • +
  • -

#3 seko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 08-November 07

Re: displaying gridview without any record in table

Posted 09 November 2007 - 07:49 AM

PsychoCoder thank you so much you have saved my project and my time.
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: displaying gridview without any record in table

Posted 09 November 2007 - 07:51 AM

No problem, glad I could help :)
Was This Post Helpful? 0
  • +
  • -

#5 seko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 08-November 07

Re: displaying gridview without any record in table

Posted 09 November 2007 - 09:04 AM

PsychoCoder we tried your code but it wasn't work. I have a gridview which is bounded a sqldatasource, and a detailsview which is bounded to girdview.This is only thing that ı know :blink:
meanwhile my programming language is vb.net

This post has been edited by seko: 09 November 2007 - 09:18 AM

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: displaying gridview without any record in table

Posted 09 November 2007 - 12:01 PM

@seko,

I never said that was the exact code you needed to solve your problem, but it was an idea to push you down the right road. I offered it as an option, and I showed example code on how to implement it. I hope you weren't expecting me to write and offer the exact code you needed for this?
Was This Post Helpful? 0
  • +
  • -

#7 byurow  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 05-November 08

Re: displaying gridview without any record in table

Posted 05 November 2008 - 01:39 PM

I know that it has been a year since this thread was active, but I have a question I hope someone can help me with. I am teaching myself ASP.Net with VB code behind while creating a project for work. I have been using Microsofts asp.net tutorials which I found very useful. I realize that the way microsoft is teaching may not be the best way but it has at least gotten me started.

Well, now that I have gotten pretty far into my business processes, I have come across the need to display a gridview when there is no data in the underlying table. Specifically, I will need to add a new record through the grid's footer row but the grid will not show up because I have no data in the datasource. I found this thread through a google search and it seems to be exactly what I am looking for. However, being a complete NOOB, I have no idea where to place the code (VB version) or how to call it. I tried placing it in the Page_Load sub and get the following complile error: 'Operator '=' is not defined for types 'System.Data.DataTable' and 'System.Data.DataTable'. Could someone please help me with where to place the code and then how / where to call it from my grid???? I appreciate any help.

View PostPsychoCoder, on 9 Nov, 2007 - 05:30 AM, said:

If you're wanting an empty GridView to show, even when no records are returned, so you can make sure the default functionality of adding a row are available, before binding the GridView to your DataSet, check to see if the DataSet contains any rows, if it doesn't then add an "empty" row to it, then bind it to the GridView.

You will want to "clone" the original underlying data source for your GridView so as to not be modifying the original datasource for your grid, this can be done in either VB.Net or C#, and since I dont know the language you're using Ill demonstrate both. Something like this in VB.Net

<SNIP>

Now you have 2 examples in 2 different languages, that should at least give you an idea of where to go next :)

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1