We usually add a blank row in the gridview if the query to populate gridview returns zero rows.
If thats where you want an empty row since gridview wont be visible if rows.count =0, thentry this
after
Gridview1.Databind()
EmptyGridFix( Gridview1);
The definition is
protected void EmptyGridFix(GridView grdView)
{
// normally executes after a grid load method
if (grdView.Rows.Count == 0 &&
grdView.DataSource != null)
{
DataTable dt = null;
// need to clone sources otherwise it will be indirectly adding to
// the original source
if (grdView.DataSource is DataSet)
{
dt = ((DataSet)grdView.DataSource).Tables[0].Clone();
}
else if (grdView.DataSource is DataTable)
{
dt = ((DataTable)grdView.DataSource).Clone();
}
vickyjha
Member
5 Points
94 Posts
add blank row in gridview
Jan 08, 2008 05:22 AM|LINK
hello,
i want to add blank row in my gridview, so that i can insert data through gridview to database
can any body help?
thanx in advance
naveenj
Contributor
6164 Points
1130 Posts
Re: add blank row in gridview
Jan 08, 2008 06:26 AM|LINK
Hi vickyjha,
We usually add a blank row in the gridview if the query to populate gridview returns zero rows.
If thats where you want an empty row since gridview wont be visible if rows.count =0, thentry this
after
Gridview1.Databind()
EmptyGridFix( Gridview1);
The definition is
protected void EmptyGridFix(GridView grdView)
{
// normally executes after a grid load method
if (grdView.Rows.Count == 0 &&
grdView.DataSource != null)
{
DataTable dt = null;
// need to clone sources otherwise it will be indirectly adding to
// the original source
if (grdView.DataSource is DataSet)
{
dt = ((DataSet)grdView.DataSource).Tables[0].Clone();
}
else if (grdView.DataSource is DataTable)
{
dt = ((DataTable)grdView.DataSource).Clone();
}
if (dt == null)
{
return;
}
dt.Rows.Add(dt.NewRow()); // add empty row
grdView.DataSource = dt;
grdView.DataBind();
// hide row
grdView.Rows[0].Visible = false;
grdView.Rows[0].Controls.Clear();
}
// normally executes at all postbacks
if (grdView.Rows.Count == 1 &&
grdView.DataSource == null)
{
bool bIsGridEmpty = true;
// check first row that all cells empty
for (int i = 0; i < grdView.Rows[0].Cells.Count; i++)
{
if (grdView.Rows[0].Cells[i].Text != string.Empty)
{
bIsGridEmpty = false;
}
}
// hide row
if (bIsGridEmpty)
{
grdView.Rows[0].Visible = false;
grdView.Rows[0].Controls.Clear();
}
}
}
This will do.
Naveen Jose
ASP.NET Freelancer, Consultant
Please remember to click Mark as Answer on the post that helps you
vinz
All-Star
126896 Points
17922 Posts
MVP
Re: add blank row in gridview
Jan 08, 2008 08:36 AM|LINK
Check this out
http://geekswithblogs.net/casualjim/articles/51360.aspx
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
Garry_G
Member
113 Points
85 Posts
Re: add blank row in gridview
Jan 08, 2008 08:56 AM|LINK
You can always use the GridView footer to set up a 'blank row' for editing. I've used this method a few times in the past.
Are you planning to use the standard GridView editing commands or are you calling custom procedures to do this?
vickyjha
Member
5 Points
94 Posts
Re: add blank row in gridview
Jan 08, 2008 09:19 AM|LINK
Allen Chen –...
All-Star
40943 Points
4949 Posts
Re: add blank row in gridview
Jan 09, 2008 06:20 AM|LINK
Hi:
The easiest way to achieve this is to put a DetailsView into EmptyDataTemplate:
<asp:GridView DataSourceID="SqlDataSource1" ID="GridView5" runat="server">
<EmptyDataTemplate>
<asp:DetailsView DefaultMode="Insert" AutoGenerateInsertButton="true" DataSourceID="SqlDataSource1" ID="DetailsView3" runat="server" Height="50px" Width="125px">
</asp:DetailsView>
</EmptyDataTemplate>
other templates here
</asp:GridView>
Regards
Allen Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.