Datagrid Disappears When Bound

Results 1 to 2 of 2

Thread: Datagrid Disappears When Bound

  1. #1
    Join Date
    Dec 1969

    Default Datagrid Disappears When Bound

    I am binding to a datagrid with a datareader. I am using the code below to try and bind to the datagrid if the reader returns results from a query. This is all happening in the same page. I have it binding fine, but once I throw in the if/then to check if there is data in the data reader (so I can let the user no nothing was found), the datagrid does not bind even though the query should return results. The headers are visible so I know it has returned values because the reader is reading and making the datagrid visible, but for some reason it is not binding to the data because the datagrid is empty. Any Help? Code: <BR><BR>Dim read As SqlDataReader <BR>read = myCommand.ExecuteReader<BR><BR>If Not read.Read() Then<BR><BR>noresults.InnerHtml = "Your search returned no results."<BR>read.Close()<BR>MyConnection.Close()< BR>Datagrid.Visible = "false"<BR> Else <BR>Datagrid.DataSource = read<BR>Datagrid.DataBind()<BR>Datagrid.visible = "true"<BR>read.Close()<BR> End If

  2. #2
    Join Date
    Dec 1969

    Default RE: Datagrid Disappears When Bound

    I think it may have something to do with the order of your reads as the datareader is one direction only (I think) so when you read it pulls a record out of it then when you bind your not getting that record. Try something like this instead:<BR><BR>Datagrid.DataSource = myCommand.ExecuteReader<BR>Datagrid.DataBind()<BR> <BR>if DataGrid.Items.Count = 0 then<BR>&nbsp;&nbsp;noresults.InnerHtml = "Your search returned no results."<BR>&nbsp;&nbsp;DataGrid.Visible = false<BR>end if

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts