简体   繁体   English

从C#背后的代码绑定gridview中的动态列模板字段

[英]bind dynamic columns template field in gridview from code behind C#

I have stored proc using pivot(dynamic query) that will return dynamic columns. 我已经使用数据透视表(动态查询)存储了proc,它将返回动态列。 Like 喜欢

ItemId   A         B              c
------   --        --            --
1        A_Value1   B_Value1     C_Value1
16       A_Value2   B_Value2     C_Value2

Here A,B,C are dynamic columns . 这里A,B,C是动态列。 Sometime A only occur or A,B,C,D,E, etc because its pivot table. 有时A仅会发生,或者A,B,C,D,E等会发生,因为它有数据透视表。
Then, I will bind that result in gridview. 然后,将结果绑定到gridview中。

My requirement is, I want to bind that result gridview's template field with link button. 我的要求是,我想用链接按钮将该结果gridview的模板字段绑定。 Because I want to do rowcommand event for each column. 因为我想为每一列做rowcommand事件。
My command argument should be like this for column C 对于C列,我的命令参数应该像这样

CommandArgument = ColumnName+","+ItemID_Value;

ie
CommandArgument = "A,16"; How to do this . 这个怎么做 。 pls help. 请帮助。

You have already answer :) 您已经回答了:)

<asp:TemplateField HeaderText="A">
   <ItemTemplate>
       <asp:LinkButton runat="server" ID="lnkModify" Text="Edit" CommandName="MyCommand">
       </asp:LinkButton>
   </ItemTemplate>
</asp:TemplateField>

And OnRowDataBound (or directly in aspx) OnRowDataBound (或直接在aspx中)

MyClass item = e.Row.DataItem as MyClass;
LinkButton lnk = (LinkButton)e.Row.FindControl("lnkModify");
lnk.CommandArgument = "A," + item.Id; 

EDIT: sorry i read now that they are dinamic columns. 编辑:对不起,我现在读到它们是动态列。 Two solutions: 两种解决方案:

  1. First of all you need to have always all columns. 首先,您需要始终拥有所有列。 Then you can write code as above but you have dynamic HeaderText and CommandArgument can read HeaderText (or index of relative columns). 然后,您可以如上所述编写代码,但是您具有动态的HeaderTextCommandArgument可以读取HeaderText(或相对列的索引)。 Then you can use Visible on LinkButton if a column is not present. 然后,如果不存在列,则可以在LinkButton上使用Visible

  2. if you don't know columns number, you can build your data structure and the use AutoGenerateColumns="true" to bind data. 如果您不知道列号,则可以构建数据结构,并使用AutoGenerateColumns="true"绑定数据。

If you don't need pagination, you can use nested Repeater (one for columns ABCDE, one for rows). 如果不需要分页,则可以使用嵌套的Repeater(一个用于ABCDE列,一个用于行)。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM