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.
displaying gridview without any record in table
Page 1 of 16 Replies - 24851 Views - Last Post: 05 November 2008 - 01:39 PM
Replies To: displaying gridview without any record in table
#2
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
Something like this in C#:
Now you have 2 examples in 2 different languages, that should at least give you an idea of where to go next
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
#3
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.
#4
Re: displaying gridview without any record in table
Posted 09 November 2007 - 07:51 AM
No problem, glad I could help
#5
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
meanwhile my programming language is vb.net
meanwhile my programming language is vb.net
This post has been edited by seko: 09 November 2007 - 09:18 AM
#6
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?
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?
#7
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.
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.
PsychoCoder, 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
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
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|