用C#代码填充GridViewColumn [英] Fill GridViewColumn with c# code

查看:106
本文介绍了用C#代码填充GridViewColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView,其中的一列需要用算法填充.该算法使用SQL数据库中的值.如何在算法中使用sql数据库中的值,然后将结果放入GridView列中.我在ASP中与数据库建立了连接,但是在代码隐藏(C#)中仍无法做到这一点.
以及我应该使用哪种类型的字段(boudfield,templatefield等)

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.)

先谢谢您了:)

推荐答案

如果您不能使用SQL来实现此特定算法,而是将其合并到数据源中,那么我的第一个倾向是带有超链接的TemplateField,LinkBut​​ton,Label,Literal ...(无论您要保留的实际值).

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).

可能的例子:

<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>

在后面的代码中,添加类似以下内容(由于某些原因假设使用VB.NET):

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

添加了传递给该函数的几列,并添加了示例函数.

Added several columns being passed to that function, and added an example function.

添加了完整的示例GridView.

Added full example GridView.

这篇关于用C#代码填充GridViewColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆