GridView.Columns.Add VS GridView.Columns.Insert和GirdViewRow.FindControl [英] GridView.Columns.Add vs GridView.Columns.Insert and GirdViewRow.FindControl

查看:264
本文介绍了GridView.Columns.Add VS GridView.Columns.Insert和GirdViewRow.FindControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到的东西,我觉得不可思议。因此,我想获得它的工作原理这样的解释。

I have noticed something that I find weird. Therefore I would like to get an explanation of how it works this way.

我有一个GridView,像这样的:

I have a GridView, like this:

    <asp:GridView ID="_grdFordelinger" runat="server" CssClass="grid" AutoGenerateColumns="False">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    Vælg
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="FordelingCheckBox" runat="server" />
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

正如你所看到的我只有在这一点上定义的一列,并且该列包含每一行的复选框。
然后,我从我的code通过行添加更多的列后面,后来,当用户点击一个按钮,我环路寻找到复选框得到遏制,像行:

As you can see I only have one column defined at this point, and that column contains a CheckBox for each row. I then add more columns from my code behind, and later on, when the user clicks a button, I loop through the rows to find the rows where the CheckBox has been checked, like:

        foreach (GridViewRow row in _fordelingsSelector.Rows)
        {
            foreach (TableCell cell in row.Cells)
            {
                foreach (Control control in cell.Controls)
                {
                    if(control is CheckBox)
                        Trace.Write("I FOUND A CHECKBOX!!!");
                }
            }
        }

或者

        foreach (GridViewRow row in _fordelingsSelector.Rows)
        {
            var checkBox = (CheckBox) row.FindControl("FordelingCheckBox");

            if (checkBox.Checked)
                Trace.Write("I will now delete fordeling with id: " + row.Cells[1].Text);
        }

我已经尝试了两种方式,两者的工作方式,当我使用添加到Columns.Add在GridView的其他列,如:

I have tried both ways, and both ways work when I add the additional columns to the GridView using Columns.Add, like:

        foreach (var boundField in boundFields)
        {
            _grdFordelinger.Columns.Add(boundField);
        }

不过,我想复选框列是最右边的列,所以我想它不会有所作为添加使用Columns.Insert列,如:

But I would like the CheckBox column to be the rightmost column, so I thought it would not make a difference to add the columns using Columns.Insert, like:

        for (int i = 0; i < boundFields.Count; i++)
        {
            _grdFordelinger.Columns.Insert(i, boundFields[i]);
        }

但现在我无法找到使用code以上了(现在更改从1到0的单元索引复选框列是最右边的列)的复选框。这是为什么?
为什么复选框列必须在最左边?

But now I cannot find the CheckBoxes using the code above anymore (Changing the cell index from 1 to 0 now that the CheckBox column is the rightmost column). Why is that? Why do the CheckBox column have to be on the far left?

感谢很多提前

推荐答案

我没有一个答案,但我有相同的问题。我会希望索伦将回复,如果他最终找到了答案。如果不是我希望别人会用一个解决方案或替代方法作出回应。

I don't have an answer but I have the exact same problem. I'll hoping Soren will reply if he eventually found an answer. If not I'm hoping someone else will respond with a solution or a workaround.

问候,

鲍勃阿瓦洛

这篇关于GridView.Columns.Add VS GridView.Columns.Insert和GirdViewRow.FindControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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