The cell index for Report column is 9.
You have set as 10. So the error.
Please verify it.
alhamd says:
bool
Health = (row.Cells[6].Controls[0]
as
CheckBox).Checked;
There is no column for Health.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="UserID"
Class="table table-striped table-bordered table-hover"
OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelEdit" OnRowUpdating="OnRowUpdating">
<Columns>
<asp:CheckBoxField DataField="IsSelected" HeaderText="Action" />
<asp:BoundField DataField="UserName" HeaderText="User Name" ItemStyle-Width="150px" ReadOnly="true" />
<asp:BoundField DataField="Password" HeaderText="Password" ItemStyle-Width="150px" ReadOnly="true" />
<asp:CheckBoxField DataField="Animal" HeaderText="Animal" ItemStyle-Width="150px" />
<asp:CheckBoxField DataField="Breading" HeaderText="Breading" ItemStyle-Width="150px" />
<asp:CheckBoxField DataField="Milk" HeaderText="Milk" ItemStyle-Width="150px" />
<asp:CheckBoxField DataField="Stock" HeaderText="Stock" ItemStyle-Width="150px" />
<asp:CheckBoxField DataField="Accounts" HeaderText="Accounts" ItemStyle-Width="150px" />
<asp:CheckBoxField DataField="Employee" HeaderText="Employee" ItemStyle-Width="150px" />
<asp:CheckBoxField DataField="Reports" HeaderText="Reports" ItemStyle-Width="150px" />
<asp:CommandField ShowEditButton="true" HeaderText="Update" ItemStyle-Width="100px" />
</Columns>
</asp:GridView>
C#
private void BindGridView()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("IsSelected",typeof(bool)),
new DataColumn("UserID",typeof(int)),
new DataColumn("UserName",typeof(string)),
new DataColumn("Password",typeof(string)),
new DataColumn("Animal",typeof(bool)),
new DataColumn("Breading",typeof(bool)),
new DataColumn("Milk",typeof(bool)),
new DataColumn("Stock",typeof(bool)),
new DataColumn("Accounts",typeof(bool)),
new DataColumn("Employee",typeof(bool)),
new DataColumn("Reports",typeof(bool)) });
dt.Rows.Add(true, 1, "ABC", "123", true, true, true, true, true, true, true);
dt.Rows.Add(true, 2, "SKy", "123", true, true, true, true, true, true, true);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int UserId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
//Get the checked value of the CheckBoxField column.
bool isSelected = (row.Cells[0].Controls[0] as CheckBox).Checked;
bool Animal = (row.Cells[3].Controls[0] as CheckBox).Checked;
bool Breading = (row.Cells[4].Controls[0] as CheckBox).Checked;
bool Milk = (row.Cells[5].Controls[0] as CheckBox).Checked;
bool Stock = (row.Cells[6].Controls[0] as CheckBox).Checked;
bool Accounts = (row.Cells[7].Controls[0] as CheckBox).Checked;
bool Employee = (row.Cells[8].Controls[0] as CheckBox).Checked;
bool Reports = (row.Cells[9].Controls[0] as CheckBox).Checked;
GridView1.EditIndex = -1;
BindGridView();
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGridView();
}
protected void OnRowCancelEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGridView();
}