Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting Data 1

Status
Not open for further replies.

sthmpsn1

MIS
Sep 26, 2001
456
US
I am dumping data into a datagrid and want to convert anything that is a 1 to an X and anything that is a 0 to a blank. Any suggestions on how I would do this for 5 selected fields.
 
What we do is just loop through the datagrid, and check the cell in each row that we want to change.

For i = 0 to DataGrid1.Items.Count - 1
If DataGrid1.items(i).cells(indexofcell).Value = 1 Then
DataGrid1.items(i).cells(indexofcell).value = "x"
else
DataGrid1.items(i).cells(indexofcell).Value = ""
Next

Note: I think its datagrids that have an items collection only, and the datatable that has the rows collection, but not at my work computer right now. You get the idea from the code though.

Also, we actually use checkboxes and an image of a check instead of just an 'X' or blank. Makes the user interface more intuitive and look better. Since you're only dealing with a boolean anyway (1 or 0), you could look into this opion as well.

hth

D'Arcy
 
So you use a checkbox instead?? Is that what your saying? that would be really cool. How do you do that? Also when I try the code above it says that value is not a member of tablecell
 
To use the checkboxes, what you do is add 2 template columns, and a bound column for the boolean value.

Add a checkbox to the first template column, and an image control to the second.

once the datagrid has been bound to the data:
1. If you want the user to edit the data, show the column with the checkbox and hide the image column, or vice versa if the user isn't editing (this is what we do; we have a "check" graphic to show if an item is checked, but we don't want the user to edit it)

2. Then, you just check each item in the datagrid and check that cell. You simply assign the value of the checkbox.Checked property to the boolean, and also hide/show the image control depending on the value as well.

Oh, and my bad: its not value, it'll be Text
i.e.
DataGrid1.items(i).cells(indexofcell).Text = "x"

D
 
How do you call a column in the datagrid show you show the column that has the checkbox. Mine won't need any editing capabilites. Just simply show the checkbox image if the value is 1, or nothing if it is blank.
 
All you do in code is go

DataGrid1.Columns(index).Visible = True/False

and that shows it

if you mean how do you set the image control to be visible = true/false, you just go:

DataGrid1.Items(index).Cells(index).Controls(1).Visible = True/False
Or
Dim imgImage as new System.blahblah.Image
imgImage = DataGrid1.items(index).FindControl(controlname)
imgImage.visible = True/False

If you do the first way, the controls collection index must be set to 1. The first control will always be the cell itself, and if there is another control in that cell it will then be (1).

D
 
I have never worked with a template before.
Is it something like this?

<asp:templatecontrol>
imageurl = blah blah blah
</asp:templatecontrol>

Or does this need to be designed a specific way?
 
okay so here is the code I have right now. What do I need to do so that I display the templated image for one of my columns below if the value returned is a 1. I guess I don't understand that part. before I was using this code that you gave me, but I don't think that will still work?

For i = 0 to PersonInfo.Items.Count - 1
If PersonInfo.items(i).cells(2).text = &quot;1&quot; Then
PersonInfo.items(i).cells(2).text = &quot;X&quot;
else
PersonInfo.items(i).cells(2).text = &quot;&quot;
End If

Next



<asp:DataGrid id=&quot;PersonInfo&quot; Runat=&quot;server&quot;
AllowSorting=&quot;True&quot;
DataKeyField=&quot;ID&quot;
AlternatingItemStyle-BackColor=&quot;#A6F4FD&quot;
AutoGeneratecolumns=&quot;false&quot;
onSortCommand=&quot;DataGrid_Sort&quot;>
<columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Image
Width=&quot;75&quot; Height=&quot;75&quot;
ImageUrl=&quot;../graphics/check.gif&quot;
Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
<asp:boundcolumn
DataField=&quot;ID&quot;
HeaderText=&quot;ID&quot;
ReadOnly=&quot;true&quot;
SortExpression=&quot;ID&quot; />
<asp:boundcolumn
DataField=&quot;loginID&quot;
HeaderText=&quot;login ID&quot;
SortExpression=&quot;loginID&quot; />
<asp:boundcolumn
ItemStyle-HorizontalAlign=&quot;center&quot;
ItemStyle-Font-Bold=&quot;true&quot;
DataField=&quot;senttosup&quot;
HeaderText=&quot;Sent To Supervisor&quot;
SortExpression=&quot;senttosup&quot; />
<asp:boundcolumn
ItemStyle-HorizontalAlign=&quot;center&quot;
ItemStyle-Font-Bold=&quot;true&quot;
DataField=&quot;senttohr&quot;
HeaderText=&quot;Sent To HR&quot;
SortExpression=&quot;senttohr&quot; />
<asp:boundcolumn
ItemStyle-HorizontalAlign=&quot;center&quot;
ItemStyle-Font-Bold=&quot;true&quot;
DataField=&quot;hrapprove&quot;
HeaderText=&quot;HR Approved&quot;
SortExpression=&quot;hrapprove&quot; />
<asp:boundcolumn
ItemStyle-HorizontalAlign=&quot;center&quot;
ItemStyle-Font-Bold=&quot;true&quot;
DataField=&quot;backtoemp&quot;
HeaderText=&quot;Back To Employee&quot;
SortExpression=&quot;backtoemp&quot; />
<asp:boundcolumn
ItemStyle-HorizontalAlign=&quot;center&quot;
ItemStyle-Font-Bold=&quot;true&quot;
DataField=&quot;empapprove&quot;
HeaderText=&quot;Employee Approved&quot;
SortExpression=&quot;empapprove&quot; />
<asp:boundcolumn
ItemStyle-HorizontalAlign=&quot;center&quot;
ItemStyle-Font-Bold=&quot;true&quot;
DataField=&quot;supapprove&quot;
HeaderText=&quot;Supervisor Approved&quot;
SortExpression=&quot;supapprove&quot; />
</columns>
</asp:DataGrid>
 
Got it, I got the images to display using this

PersonInfo.items(i).cells(8).text = &quot;<img border=0 src=../graphics/check.gif>&quot;
 
Kewl man.

You can also display/hide the control by doing this:

Dim imgImage as new system.blahblah.Image
imgImage = PersonInfo.items(i).cells(8).controls(1)
imgImage.Visible = True/False

D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top