Solved

DataGrid OnItemDataBound Event

Posted on 2004-10-29
1,452 Views
Last Modified: 2008-02-07
I have a data grid control that lists a bunch of aggregate data against a list of entities.  In a case where the entity has 0 in each column i need to remove that row from the grid so it isn't shown.

I understand the datagrid control is only visible from the client side but any documentation I have about the onDataBind event details writing this code to the server side.  So this has confused me considerably.

How can I achieve this?

To test an idea I tried this in the HTML (and wrote a little function called removezerorows) but I get a compile error.
<asp:datagrid id="grdBudget" OnItemDataBound="RemoveZeroRows();"

function RemoveZeroRows()
{
      alert('Removing Zero Row');
}
0
Question by:billy21
    4 Comments
     
    LVL 6

    Author Comment

    by:billy21
    I also need to know how to remove the row and how I can access the item that is being databound.
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    You can Access those rows like folling in your ItemDataBound event..

    ----------------------------------------------------------------------------------------------------------------
    //In Following code I'm hiding rows for wich TotalSales is less then 100....

    private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
          switch( e.Item.ItemType )
          {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:

                double TotalSales = Convert.ToDouble( e.Item.Cells[7].Text );  //TotalSales
                           if ( TotalSales < 100 ) e.Item.Visible = false;

                break;
          }
    }
    ----------------------------------------------------------------------------------------------------------------

    -tushar
    0
     
    LVL 6

    Author Comment

    by:billy21
    Sorry but need it in VB and need to actually remove the rows.
    0
     
    LVL 18

    Accepted Solution

    by:
    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
     Select e.Item.ItemType
     Case ListItemType.Item, ListItemType.AlternatingItem
       Dim TotalSales As Double = Convert.ToDouble(e.Item.Cells(7).Text)
       If TotalSales < 100 Then
         e.Item.Visible = False
       End If
       ' break
     End Select
    End Sub
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: Foundations of Front-End Development

    Jump-start a lucrative career in front-end web development, with zero previous coding experience required. This course covers the basic programming concepts and languages required for creating engaging websites from scratch.

    Suggested Solutions

    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    934 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now