Reputation: 15
I have a grid with several data columns and a column with check boxes. There is a column called app Number which contains numbers. Now I want to select number in the checked rows and put them into an array. How can I do it?
Here is the code I have done up to now.
<asp:GridView ID="gvAppeals" runat="server" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4"
Width="665px" AutoGenerateColumns="False" OnSelectedIndexChanged="gvAppeals_SelectedIndexChanged1"
AllowPaging="True" PageSize="10" OnPageIndexChanging="gvAppeals_PageIndexChanging" OnRowDataBound="gvAppeals_RowDataBound">
<Columns>
<asp:BoundField DataField="App_no" HeaderText="APP NO">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Font-Size="Small" HorizontalAlign="Left" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="EMP_FULLNAME" HeaderText="FULL NAME">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Font-Size="Small" HorizontalAlign="Left" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="EMP_NIC_NO" HeaderText="NIC">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Font-Size="Small" HorizontalAlign="Left" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="EMP_BIRTHDAY" HeaderText="BIRTH DATE" DataFormatString="{0:yyyy-MM-dd}">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Font-Size="Small" HorizontalAlign="Left" VerticalAlign="Middle" />
</asp:BoundField>
<asp:TemplateField>
<%--<HeaderTemplate>
<asp:CheckBox ID="chkHeader" runat="server" AutoPostBack="true" />
</HeaderTemplate>--%>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="Black" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
C# code
protected void btnConfirm_Click(object sender, EventArgs e)
{
//DataSet ds = new DataSet();
int[] numbers;
numbers = new int[gvAppeals.Rows.Count];
int noOfRowsChecked = 0;
foreach (GridViewRow row in gvAppeals.Rows)
{
int rowIndex = row.RowIndex;
CheckBox chkrow = (CheckBox)row.FindControl("chkSelect");
if (chkrow.Checked == true)
{
numbers[noOfRowsChecked] = Int32.Parse(gvAppeals.DataKeys[rowIndex]["App_no"].ToString());
noOfRowsChecked++;
}
//update the dept by checking the selected appeal numbers
if (noOfRowsChecked > 0)
{
for (int i = 0; i < numbers.Length; i++)
{
int appNo = numbers[i];
dba.confirmAppeal(appNo);
}
}
else
{
WebMsgBox.Show("Please select an application to confirm");
}
}
}
Upvotes: 0
Views: 3698
Reputation: 35514
There are no DataKeys
defined in the GridView, to it will always be out of range. Add the DataKeyNames
property to the GridView.
<asp:GridView ID="gvAppeals" runat="server" DataKeyNames="App_no" >
Upvotes: 1
Reputation: 4622
Just a gentle reminder that you need to subtract one (-1) on your indexing. That's probably the reason you're getting System.ArgumentOutOfRangeException
.
Upvotes: 2