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.
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"
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.
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).
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 = "1" Then
PersonInfo.items(i).cells(2).text = "X"
else
PersonInfo.items(i).cells(2).text = ""
End If
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.