views:

186

answers:

1

Hi guys

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

A: 

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);
    }
Lee Elenbaas
Hi Lee. Thanks for your reply, but it doesn't answer my question. I know all items after it is pushed - and that is what I want
Søren
Then i need clarification for the problem: (1) You start with the checkbox column the only one at index 0 (2) If you add columns using the Columns.Add method the checkbox remains at index 0 and is at the leftmost (3) If you add columns using Columns.Insert(i...) the checkboxes should be on the left most and be located at index Columns.Count-1
Lee Elenbaas