Hopefully some one can help... I have been trying now for the past few days to find a button located in the footer of a gridview which has an empty datasource. I have used the following code to show the header and footer on the empty grid, but when i
try to reference the button to set its visibility to false I get the error "Object reference not set to an instance of an object.".
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace xGridView
PublicClass xGridView
Inherits GridView
#Region"Properties"
<Category("Behaviour")> _
<Themeable(True)> _
<Bindable(BindableSupport.No)> _
<DefaultValue(False)> _
PublicProperty ShowWhenEmpty() AsBooleanGetIf ViewState("ShowWhenEmpty") IsNothingThenReturnFalseEndIfReturnCBool(ViewState("ShowWhenEmpty"))
EndGetSet(ByVal value AsBoolean)
ViewState("ShowWhenEmpty") = value
EndSetEndPropertyProtected _footerRow2 As GridViewRow
PublicOverloadsOverridesReadOnlyProperty FooterRow() As GridViewRow
GetDim f As GridViewRow = MyBase.FooterRow
If f IsNot NothingThenReturn f
ElseReturn _footerRow2
EndIfEndGetEndPropertyProtected _headerRow2 As GridViewRow
PublicOverloadsOverridesReadOnlyProperty HeaderRow() As GridViewRow
GetDim h As GridViewRow = MyBase.HeaderRow
If h IsNot NothingThenReturn h
ElseReturnMe._headerRow2
EndIfEndGetEndProperty#End RegionProtectedOverloadsOverridesFunction CreateChildControls(ByVal dataSource As System.Collections.IEnumerable, ByVal dataBinding AsBoolean) AsIntegerDim numRows AsInteger = MyBase.CreateChildControls(dataSource, dataBinding)
'no data rows created, create empty table if enabled If numRows = 0 AndAlso ShowWhenEmpty Then'create table Dim table AsNew Table()
table.ID = Me.ID
'convert the exisiting columns into an array and initialize Dim fields As DataControlField() = New DataControlField(Me.Columns.Count - 1) {}
Me.Columns.CopyTo(fields, 0)
'create a new header row IfMe.ShowHeader Then
_headerRow2 = MyBase.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)
Me.InitializeRow(_headerRow2, fields)
table.Rows.Add(_headerRow2)
EndIf'create the empty row Dim emptyRow AsNew GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal)
Dim cell AsNew TableCell()
cell.ColumnSpan = Me.Columns.Count
'cell.Width = Unit.Percentage(100)IfNot [String].IsNullOrEmpty(EmptyDataText) Then
cell.Controls.Add(New LiteralControl(EmptyDataText))
EndIfIfMe.EmptyDataTemplate IsNot NothingThen
EmptyDataTemplate.InstantiateIn(cell)
EndIf
emptyRow.Cells.Add(cell)
table.Rows.Add(emptyRow)
'create footer row IfMe.ShowFooter Then
_footerRow2 = MyBase.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal)
Me.InitializeRow(_footerRow2, fields)
table.Rows.Add(_footerRow2)
EndIfMe.Controls.Clear()
Me.Controls.Add(table)
EndIfReturn numRows
EndFunctionEndClassEnd Namespace
CSharpSean
Contributor
5464 Points
917 Posts
Re: Solution: Show Header/Footer of Gridview with Empty Data Source
Sep 04, 2007 05:02 PM|LINK
Problem solved with help from http://forums.asp.net/p/1012442/1373310.aspx#1373310
andre.monteir
Member
13 Points
6 Posts
Re: Solution: Show Header/Footer of Gridview with Empty Data Source
Mar 26, 2008 03:50 PM|LINK
Nice and clean CISCBrain!!! [Yes]
André Monteiro
chrisbarm
Member
2 Points
1 Post
Re: Solution: Show Header/Footer of Gridview with Empty Data Source
Jun 13, 2008 11:10 AM|LINK
Hopefully some one can help... I have been trying now for the past few days to find a button located in the footer of a gridview which has an empty datasource. I have used the following code to show the header and footer on the empty grid, but when i try to reference the button to set its visibility to false I get the error "Object reference not set to an instance of an object.".
Vilh. Nellemann
Member
2 Points
1 Post
Re: Solution: Show Header/Footer of Gridview with Empty Data Source
Jun 19, 2008 01:58 PM|LINK
Hello
I have question as to what is the content of you namespace "TExtended"?
When I run form1 Class ExtendedGridView is not a known element, how com?
I presume "MetricDataGraphUC.ascx" includes namespace TExtended.
I am looking foward to get the big picture.
Best regards
Vilh. Nellemann
faruk2011
Member
2 Points
1 Post
Re: Solution: Show Header/Footer of Gridview with Empty Data Source
Jul 23, 2008 11:28 AM|LINK
excellent solution. working properly.
Thank you very much.
Faruk Ahmed
asp .net ... c# ...
Kriswd40
Member
98 Points
341 Posts
Re: Solution: Show Header/Footer of Gridview with Empty Data Source
Sep 03, 2008 06:49 PM|LINK
Several years late, but this is a much simpler way to get the heading to display:
datagridName.DataSource = new DataTable();datagridName.DataBind();
Works with datagrid, I have not tested with gridView.
y34h
Member
2 Points
1 Post
Re: Solution: Show Header/Footer of Gridview with Empty Data Source
Nov 21, 2008 02:51 AM|LINK
after a few suffering and some forum-surfing i said to my self : "AAHHH what the hell !!, i'll do it my self XDD"
so... here's my solution
bool empty = false; protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e) { DataTable t = (DataTable)e.ReturnValue; if (t.Rows.Count == 0) { t.Rows.Add(new object[] { }); empty = true; } } protected void GridView1_DataBound(object sender, EventArgs e) { if (empty) GridView1.Rows[0].Visible = false; }azamsharp
All-Star
24244 Points
4612 Posts
MVP
Re: Solution: Show Header/Footer of Gridview with Empty Data Source
Nov 21, 2008 05:39 PM|LINK
Just bind it to an empty DataSet or DataTable and the headers and footers will be displayed
HighOnCoding
gemsgems
Member
2 Points
1 Post
Re: simplest way - eyad
Mar 23, 2009 04:59 PM|LINK
the simple way to put select command of SqlDataSource control to get default row as this :
SqlDataSource.SelectCommand = "select '' as f1,'' as f2,'' as f3,'' as f4,'' as f5,'' as f6 from dual";
where :
f1,f2,... the name of columns that uses
SqlDataSource is the control that bind your DataGrid
Pak514
Participant
833 Points
318 Posts
Re: Solution: Show Header/Footer of Gridview with Empty Data Source
Apr 21, 2009 04:30 PM|LINK