Datagrid and switch

  • Thread starter Thread starter Arjen
  • Start date Start date
A

Arjen

Hello,

My datagrid shows some values out of a database.
For example 1,1,2,3,2,3,4,1.

Now these numbers are not realy nice... so I want to convert the numbers.
If it is a 1 it must show cars.
If it is a 2 is must show bikes.

How can I convert this in C#?

I have now this. (But doesn't works)
Can somebody help me?
Thanks!

-----------

<ItemTemplate>
<span class="Normal">
<%
switch(DataBinder.Eval(Container.DataItem,"FieldType")) {

case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
%>
</span>
</ItemTemplate>
 
Arjen said:
Hello,

My datagrid shows some values out of a database.
For example 1,1,2,3,2,3,4,1.

Now these numbers are not realy nice... so I want to convert the
numbers. If it is a 1 it must show cars.
If it is a 2 is must show bikes.

How can I convert this in C#?

I have now this. (But doesn't works)
Can somebody help me?
Thanks!

-----------

<ItemTemplate>
<span class="Normal">
<%
switch(DataBinder.Eval(Container.DataItem,"FieldType")) {

case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
%>
</span>
</ItemTemplate>

Put your code in a function, like this:

String myFunction(int32 value) {
switch(value) {
case 0: return "cars";
case 1: ....
}

Then call that function from your ItemTemplate:
<ItemTemplate>
<span class="Normal">
<%#
myFunction(DataBinder.Eval(Container.DataItem,"FieldType"))
%>

Don't forget the # sign, it indicates a databinding expression.
 
The easiest way is to call on a function

<span class="Normal"><%#
ConvertFieldType((int)(DataBinder.Eval(Container.DataItem, "FieldType"))
%></span>

and then just define a function
public string ConvertFieldType(int fieldTypeId){
switch(fieldTypeId){
case 1:
return "cars";
....
}
}

The function can either be in your code behind or in a <Script
runat="server"> block
 
Okay, thanks... it works!

Other question...
I have a simple checkbox inside a datagrid.
Just like this:

<input type="checkbox" name="cbx">

Now I get from the database a true or false.
If it is true I want to set the checkbox selected else not.
Is there a way to do this shortly? Or do I have to make a new function?

Thanks again!
 
Arjen said:
Okay, thanks... it works!

Other question...
I have a simple checkbox inside a datagrid.
Just like this:

<input type="checkbox" name="cbx">

Now I get from the database a true or false.
If it is true I want to set the checkbox selected else not.
Is there a way to do this shortly? Or do I have to make a new
function?

No, this one can be done without a function.

<input type="checkbox" name="cbx"
<%# DataBinder.Eval(Container.DataItem,
"BooleanFieldName")?"checked":"" %> >
 
Back
Top