Fill GridViewColumn with c# code
I have a GridView with a column that needs to be filled with an开发者_开发知识库 algorithm. This algorithm uses a value from a SQL database. How can I use a value from a sql database in an algorithm and than put the result in the GridView column. I have a connection with the database in ASP, but did not manage to do this yet in the code-behind (C#).
And what type of field should I use (boudfield, templatefield etc.)Thanks in advance :)
If this particular algorithm isn't something that you can implement using SQL and incorporate into the data source, then my first inclination would be a TemplateField with a Hyperlink, LinkButton, Label, Literal...(whatever you want to hold the actual value).
Possible example:
<asp:GridView ID="gvExample" runat="server" DataSourceID="dsExample">
<Columns>
<asp:TemplateField HeaderText="Header Text">
<ItemTemplate>
<asp:Literal ID="litAlgorithmResult" runat="server"
Text='<%# DoMyFunction(Eval("DataSourceField1").ToString(), Eval("DataSourceField2").ToString(), Eval("DataSourceField3").ToString()) %>'>
</asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In your codebehind, add something like this (assuming VB.NET for some reason):
Protected Function DoMyFunction(ByVal Value1 as String, ByVal Value2 as String, ByVal Value3 as String) as String
DoMyFunction = Value1 & Value2 & Value3
End Function
EDIT: Added several columns being passed to that function, and added an example function.
EDIT2: Added full example GridView.
精彩评论