I am getting issues when i want to include read only columns along with editable columns in gridview by asp:boundfield, i followed the below code path link for my requirement getting issue at oncheckedchange method code. please help with the updated solution with including additional read only columns. it is very urgent for me. thanks in advance.
getting issue at below line. Object reference not set to an instance of an object
row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = !isChecked;
https://www.aspsnippets.com/Articles/Bulk-Edit-Update-Multiple-Rows-in-ASPNet-GridView-using-CheckBoxes.aspx
protected void OnCheckedChanged(object sender, EventArgs e)
{
bool isUpdateVisible = false;
CheckBox chk = (sender as CheckBox);
if (chk.ID == "chkAll")
{
foreach (GridViewRow row in gvCustomers.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked = chk.Checked;
}
}
}
CheckBox chkAll = (gvCustomers.HeaderRow.FindControl("chkAll") as CheckBox);
chkAll.Checked = true;
foreach (GridViewRow row in gvCustomers.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
for (int i = 1; i < row.Cells.Count; i++)
{
row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = !isChecked;
if (row.Cells[i].Controls.OfType<TextBox>().ToList().Count > 0)
{
row.Cells[i].Controls.OfType<TextBox>().FirstOrDefault().Visible = isChecked;
}
if (row.Cells[i].Controls.OfType<DropDownList>().ToList().Count > 0)
{
row.Cells[i].Controls.OfType<DropDownList>().FirstOrDefault().Visible = isChecked;
}
if (isChecked && !isUpdateVisible)
{
isUpdateVisible = true;
}
if (!isChecked )
{
chkAll.Checked = false;
}
}
}
}
btnUpdate.Visible = isUpdateVisible;
}
<asp:GridView ID="GridViewTravelerDetails" runat="server" DataKeyNames="TravelerViewID"
AutoGenerateColumns="False" AllowPaging="true" OnPageIndexChanging="GridViewTravelerDetails_PageIndexChanging"
PageSize="50" Width="100%" BackColor="White" BorderColor="#3366CC" BorderStyle="None"
BorderWidth="1px" AllowSorting="true" CellPadding="4" GridLines="Both">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ExtractAction" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblaction" runat="server" Text='<%# Eval("ExtractAction") %>'></asp:Label>
<asp:DropDownList ID="ddlaction" runat="server" Visible="false">
<asp:ListItem Text="Select one" Value="Select one" />
<asp:ListItem Text="__________" Value="__________" />
<asp:ListItem Text="Create" Value="Create" />
<asp:ListItem Text="Update" Value="Update" />
<asp:ListItem Text="Delete" Value="Delete" />
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TravelerType" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblTravelerType" runat="server" Text='<%# Eval("TravelerType") %>'></asp:Label>
<asp:DropDownList ID="ddlTravelerType" runat="server" Visible="false">
<asp:ListItem Text="Select one" Value="Select one" />
<asp:ListItem Text="General" Value="General" />
<asp:ListItem Text="Contractor" Value="Contractor" />
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="WorkCountry" HeaderText="WorkCountry" ReadOnly="true" />
<asp:BoundField DataField="CAI" HeaderText="CAI" ReadOnly="true" />
<asp:BoundField DataField="Firstname" HeaderText="Firstname" ReadOnly="true" />
<asp:BoundField DataField="Lastname" HeaderText="Lastname" ReadOnly="true" />
<asp:BoundField DataField="Email" HeaderText="Email" ReadOnly="true" />
<asp:BoundField DataField="GUID" HeaderText="GUID" ReadOnly="true" />
<asp:BoundField DataField="HRCostCenter" HeaderText="HRCostCenter" ReadOnly="true" />
<asp:BoundField DataField="subunit" HeaderText="subunit" ReadOnly="true" />
<asp:BoundField DataField="WorkLocation" HeaderText="WorkLocation" ReadOnly="true" />
<asp:BoundField DataField="SourceCompany" HeaderText="SourceCompany" ReadOnly="true" />
<asp:BoundField DataField="WorkCompany" HeaderText="WorkCompany" ReadOnly="true" />
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
</asp:GridView>