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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid 1

Status
Not open for further replies.

dpanattoni

IS-IT--Management
Jan 29, 2002
76
US
I want to manually set the value in a datagrid column based on an existing datagrid column value. For example, a column will have the value 0, 1, or 2. Based on one of these values, I want to set another column to "Low", "Med", or "High".

Sounds easy. But what event do I do this in?

Thanks in advance.
 
I think I've solved my problem. It seems that I don't need to do this in an event. Instead, after the databinding, I can walk through the grid's items and set the columns the way I want, as follows:

foreach(DataGridItem dataGridItem in MyGrid.Items) {
string str1 = ((Label) dataGridItem.FindControl("ID")).Text;

dataGridItem.Cells[4].Text = "";
if (str1 == "0")
dataGridItem.Cells[4].Text = "LOW";
if (str1 == "1")
dataGridItem.Cells[4].Text = "MED";
if (str1 == "2")
dataGridItem.Cells[4].Text = "HIGH";
}
 
I would have said that the HIGH MED LOW column could have just been a conditional part of your SQL (ie a calculated field). This would mean less over heads. Might not be what your after and your code will work so all the best
 
I was unaware of the calculated field, (other than pure math). Thanks for the idea, I'll do a little research on this.

Thanks
 
I understand calculated fields when it comes to creating totals or math, but I don't see how to create a calculated field when it comes to text...

Are you saying that from my SQL statement, I could set a column = LOW, MED, or HIGH based on the selected value of another column? If so, can you give me an example? I can see how to do it if the description were coming from another table 'keyed' with the value, but in this case, there is no other table.

Thanks,
 
You could use a case statment in your query
somthing like this....
select case field when 0 then 'LOW'
when 1 then 'MED'
else 'HIGH' END
FROM MyTable
 
Thank you very much. I've never seen that before.
 
I have declared a function in vb file and I'm using it from aspx page.
Public Shared Function StatusText(ByVal Status As Int16) As String
Select Case Status
Case 0
Return "Low"
Case 1
Return "Mid"
Case 2
Return "High"
End Select
End Function

this is in the aspx page
<asp:TemplateColumn SortExpression=&quot;Status&quot; HeaderText=&quot;Status&quot;>
<ItemTemplate>
<asp:Label ID=lblStatus1 runat=server text='<%# StatusText(Container.DataItem(&quot;Status&quot;)) %>' />
</ItemTemplate>
</asp:TemplateColumn>

Where Status is a field from reader which is source to a datagrid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top