Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Loop through a datagrid and replace values

Posted on 2004-08-11
5
Medium Priority
?
481 Views
Last Modified: 2006-11-17
I have a datagrid.  It is being bound with a column that is either a 1 or a 0.  I need to replace the 1 with "X" and the 0 with "" in every field in a column.  Can someone help me out.  This cant be nearly as hard as Im making it.
Thanks for the help
0
Comment
Question by:Fred Goodwin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 4

Accepted Solution

by:
BradB3030 earned 1600 total points
ID: 11775205
dim i as integer
for i = 0 to datagrid1.items.count - 1
  'I'll assume the 1 or 0 is located in Column # 22, you'll have to change that accordingly
  if datagrid1.items(i).Cells(22) = "1" then
    datagrid1.items(i).Cells(22) = "X"
  else
    datagrid1.items(i).Cells(22) = ""
  end if
next i

You can also do something similar in an ItemDataBound handler subroutine

using e.item instead of datagrid1.items(i) and no for loop.
0
 
LVL 6

Expert Comment

by:pillbug22
ID: 11775256
Look at the ItemDataBound event.

This example uses assumes you're using ItemTemplates in your grid, but you could do it with bound columns as well:


Private Sub dg1_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg1.ItemDataBound

       If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim lbl As Label
            Dim myValue As Integer = CType(DataBinder.Eval(e.Item.DataItem, "dataBaseField"), Integer)
            lbl = CType(e.Item.FindControl("lblInItemTemplateToChange"), Label)
            If myValue = 1 Then
               lbl.Text = "X"
            Else
               lbl.Text = ""
            End If
        End If

    End Sub 'dgSurveyItems_ItemDataBound
0
 
LVL 8

Expert Comment

by:boulder_bum
ID: 11775258


private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
   if( e.Item.ItemType == ListItemType.Item ||
      e.Item.ItemType == ListItemType.AlternatingItem )
   {
        DataRowView drvCurrent = (DataRowView)e.Item.DataItem;

        //I'm assuming the data type is a bit
        bool oneOrZero;
        oneOrZero = (bool)drvCurrent["HasResponded"];
       
        //use casting to get a reference to whatever control you want to manipulate (label or whatever you use)
        //note: you will probably want to change the cells and/or controls index    
        //
        //Label lbl = ((Label)e.Item.Cells[0].Controls[0]);

        //manipulate your control
        if( oneOrZero )
             //lbl.Text = "X";
        else
             //lbl.Text = "O";
   }
}
0
 
LVL 7

Author Comment

by:Fred Goodwin
ID: 11777804
BradB -  I like the loop option.  however when I run it I get the error of :

Property 'Item' is 'ReadOnly'

0
 
LVL 7

Author Comment

by:Fred Goodwin
ID: 11777880
Ahh you were very close. Thanks for the help.  You just left out .text.  here is the loop I ended up using.

        Dim i As Integer
        For i = 0 To DG.Items.Count - 1
            If DG.Items(i).Cells(9).Text = 1 Then
                DG.Items(i).Cells(9).Text = "X"
            Else
                DG.Items(i).Cells(9).Text = ""
            End If
        Next i

Thanks again
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

618 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