Monday, 15 March 2010

asp.net - C# code inside aspx to fill gridview -



asp.net - C# code inside aspx to fill gridview -

i have gridview.

<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false"> <columns> <asp:boundfield datafield="status" headertext="status" itemstyle-width="100px" > <itemstyle width="100px"></itemstyle> </asp:boundfield> </columns> </asp:gridview>

in codebehind

gridview1.datasource = datatable1; gridview1.databind();

in aspx file want write c# code. illustration next if status == 0 set column value equal 'zero'; if status == 1 set column value equal 'one';

how change

<asp:boundfield datafield="status" headertext="status" itemstyle-width="100px" > <itemstyle width="100px"></itemstyle> </asp:boundfield>

so above written algorithm works? thanks!

use templatefields, example:

<asp:templatefield headertext="status"> <itemtemplate> <%# returntext(eval("status")) %> </itemtemplate> </asp:templatefield>

in c# codebehind

protected string returntext(object val) { if(val!=null) { if(val.tostring().equals("1")) {return "one"; } else if(val.tostring().equals("0")) {return "zero";} } homecoming ""; }

another alternative homecoming text sql query:

select status, case status when 1 'one' when 0 'zero' end status_text yourtable

then can bind column status_text boundfield.

c# asp.net gridview

No comments:

Post a Comment