GridView.Columns.Add vs GridView.Columns.Insert and GirdViewRow.FindControl

北城余情 提交于 2019-12-11 01:20:30

问题


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

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>

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!!!");
                }
            }
        }

Or:

        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);
        }

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);
        }

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]);
        }

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?

Thanks a lot in advance


回答1:


when you insert an item into a list - it pushes the index of all items after it by 1

you should do something similar to:

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[row.Cells.Count-1].Text);
    }



回答2:


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.

Regards,

Bob Avallone



来源:https://stackoverflow.com/questions/3306907/gridview-columns-add-vs-gridview-columns-insert-and-girdviewrow-findcontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!