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