Reputation: 79
EDIT: I want to clarify that I want to keep the checkboxes on the webpage via the gridview, however I want to convert the checkboxe controls to strings when I go to export to an excel document.
I'm exporting a gridview to excel and the gridview has a checkboxfield and I want to change it to true or false so that the export works.
I am having an issue finding the checkboxfield when I loop through my gridview. I've tried numerous posts such as Remove Grid Boxes and Get Gridview checkbox value
I've gone through the debugger and checking the locals and the type seems correct. Maybe I'm not looking at the right one.
string getType = gv.Rows1.Cells[7].GetType().ToString(); says "DataControlFieldCell"... so I guess my question is, How do I drill down to get the containingField value?
gv > Rows > Results View > 1 > Cells > Results View > [7] = Type is "Controls.DataControlFieldCell.... Drill down to containingField = controls.CheckBoxField
<asp:GridView ID="gvCompleteReport" runat="server" DataSourceID="odsGetCompleteReport" AutoGenerateColumns="false"
CssClass="gridview" DataKeyNames="organization, service">
<Columns>
<asp:BoundField HeaderText="Organization" DataField="organization" SortExpression="organization" />
<asp:BoundField HeaderText="Service" DataField="service" SortExpression="service" />
<asp:BoundField HeaderText="Category" DataField="Material Category" />
<asp:BoundField HeaderText="Material" DataField="Material Accepted" />
<asp:BoundField HeaderText="Acceptance CD" DataField="acceptance_ds" />
<asp:BoundField HeaderText="County" DataField="County" />
<asp:CheckBoxField HeaderText="Residential" DataField="residential_fl" />
<asp:CheckBoxField HeaderText="Commercial" DataField="commercial_fl" />
<asp:BoundField HeaderText="Svc Type" DataField="service_type_ds" />
</Columns>
</asp:GridView>
Code Behind
int count = gv.Controls.Count;
int rowCount = gv.Rows.Count;
//Loop through the rows
for (int i = 1; i < gv.Rows.Count; i++)
{
int rowCellCount = gv.Rows[i].Cells.Count;
//Loop through the cells
for (int ci = 0; ci < rowCellCount; ci++)
{
//This never gets hit
if (gv.Rows[i].Cells[ci].GetType() == typeof(CheckBoxField))
{
CheckBox cb = gv.Rows[i].Cells[ci].Controls[0] as CheckBox;
//Tried: Control cb = gv.Rows[i].Cells[ci].Controls[0] as CheckBox (or CheckBoxField);
Control currentControl = cb;
gv.Controls.Remove(currentControl);
gv.Controls.AddAt(i, new LiteralControl((currentControl as CheckBox).Checked ? "True" : "False"));
}
else
{
}
}
}
Upvotes: 2
Views: 1726
Reputation: 119
If I understand your stand, you just want "True" or "False" to be written there. Then you may use directly BoundField instead of checkboxfield. Database has True/False values stored for bit and those would be printed on page, I think.
Upvotes: 0
Reputation: 7276
You can convert that checkboxFiled into a TemplateField
Something like:
<asp:TemplateField HeaderText="Residential">
<ItemTemplate>
<asp:CheckBox ID="chbResidential" runat="server" Text='<%#Bind("residential_fl") %>'></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
Then you can find the CheckBox in the code-behind:
CheckBox cb = (CheckBox) gv.Rows[i].FindControl("chbResidential");
Upvotes: 1