Link to home
Start Free TrialLog in
Avatar of Ed
EdFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How do I hide a button on page if datagrid empty

I want to not display a button on a page if my datagrid is empty.

I just need to know hw to tell if the gridview is empty.


eg

If gridview1(whats the code to say it empty)

then
button1.visible = false
ASKER CERTIFIED SOLUTION
Avatar of j-w-thomas
j-w-thomas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Edjones
Edjones


Thanks

does not work on

 Protected Sub GridView1_RowDataBound

what should I use?
Is it after the bind to the datagrid?

'/// first the bind ///
gridview1.dataBind()

'/// then the check for rows ///
if gridview1.Items.count = 0 then
        button1.visible = false
        gridview1.visible = false
else
        button1.visible = true
        gridview1.visible = true
end if

If not can you post the entire sub
I'm using a separe code file



    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If GridView.Items.count = 0 Then
            Button1.Visible = False
        Else
            Button1.Visible = True
        End If
 
 
    End Sub

Open in new window

I thought that you had the entire sub finished except for this part. Is that true? I understand that you are a beginner on this subject, but it defintely needs more than that to work. Also you have it for a row command, is that waht you mean?

Try the code below and add in the correct select statement for your database and the connection string.

This is set up in a very simple format...
Sub BindData(s As Object, e As EventArgs)
 
Dim strSQLStatement As String = "SELECT * FROM TABLE_NAME"
Dim objCmd As OdbcCommand '/// or this needs to be OleDbCommand depending on your database
Dim objRdr As OdbcDataReader '/// or OleDbDataReader
'/// make sure that you have your connection string declared ///
	try		
		objCmd = New OdbcCommand(strSQLStatement,objConn)
		objConn.Open()
		objRdr = objCmd.ExecuteReader()
		dgDataGrid1.DataSource = objRdr
		dgDataGrid1.DataBind()
		objRdr.Close()
		objConn.Close()
		
		if dgDataGrid1.Items.Count = 0 then
			dgDataGrid1.Visible = false
			button1.visible = false
		else
			dgDataGrid1.Visible = true
			button1.visible = true
		end if
 
		if objConn.State = ConnectionState.Open then
			objConn.Close()
		end if
				
		catch objEx As Exception
				
			'/// you can put a custom error type message here ///
 
					
		finally
				
				if objConn.State = ConnectionState.Open then 
					objConn.Close() 
				end if
 
		end try
 
End Sub

Open in new window