0

Why deviceID is empty, I am filling it from SP. At backend it gives perfect result and even fill Gridview perfectly but at .cs level it creates problem

Select DeviceID, TerminalSNO, SoftwareVersion from Transport.Devices

it gives perfect result but in asp.net it picks "" from gridview

GV:

<asp:GridView ID="GridviewDevicesBulkUpdate" runat="server" Width="100%" AutoGenerateColumns="False"
    DataKeyNames="DeviceID" CssClass="table table-hover table-striped table-bordered">
    <Columns>
        <asp:BoundField DataField="DeviceID" HeaderText="DeviceID" Visible="false" ItemStyle-CssClass="visible-desktop" />
        <asp:BoundField DataField="TerminalSNO" HeaderText="Serial No." ItemStyle-CssClass="visible-desktop"
            HeaderStyle-CssClass="visible-desktop" />
        <asp:BoundField DataField="SoftwareVersion" HeaderText="Software Version" ItemStyle-CssClass="visible-desktop"
            HeaderStyle-CssClass="visible-desktop" />
        <asp:TemplateField HeaderText="Includes ?">
            <ItemTemplate>
                <asp:CheckBox runat="server" ID="CheckBoxIncludes" Text="." CssClass="checkbox checkbox-danger" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

.cs

foreach (GridViewRow Row in GridviewDevicesBulkUpdate.Rows)
{

    CheckBox chkUpdate = (CheckBox)Row.Cells[3].FindControl("CheckBoxIncludes");
    if (chkUpdate.Checked == true)
    {

        string DeviceID = Row.Cells[0].Text.Trim();

        if (ManageTransport.ManageDevices.UpdateAllDevices(IsCardPwdUpdated, SoftwareVersion, IsActive, int.Parse(DeviceID)))
        {
            Response = true;
        }
        else
        {
            Response= false;
        }
    }
}
Hugo Yates
  • 2,081
  • 2
  • 26
  • 24
Gringo
  • 3
  • 2
  • Don't use `Row.Cells[3].FindControl("CheckBoxIncludes");` but `Row.FindControl("CheckBoxIncludes");`. The `GridViewRow` is the `NamingContainer` of the `CheckBox`. – Tim Schmelter Mar 18 '15 at 12:19
  • but it throws error at string DeviceID = Row.Cells[0].Text.Trim(); – Gringo Mar 18 '15 at 12:20
  • that was not meant as a solution(then i would have answered) but as a suggestion. Instead of a `BoundField` i would use a `TemplateField` with an invisible label that text has the `DeviceID`. Then you could use `(Label)Row.FindControl("LblDeviceID")).Text` – Tim Schmelter Mar 18 '15 at 12:21
  • oh yes ok, lemme check – Gringo Mar 18 '15 at 12:23

2 Answers2

0

Add RowType of GridView e.g if (Row.RowType == DataControlRowType.DataRow)

CheckBox chkUpdate = (CheckBox)Row.FindControl("CheckBoxIncludes");

your code:

foreach (GridViewRow Row in GridviewDevicesBulkUpdate.Rows)
 {
    if (Row.RowType == DataControlRowType.DataRow)
    {
    CheckBox chkUpdate = (CheckBox)Row.FindControl("CheckBoxIncludes");


     if (chkUpdate.Checked == true)
        {
         string DeviceID = Row.Cells[0].Text.Trim();
         if (ManageTransport.ManageDevices.UpdateAllDevices(IsCardPwdUpdated, SoftwareVersion, IsActive, int.Parse(DeviceID)))
         {
           Response = true;
         }
         else
         {
         Response= false;
         }
      }
    }
    }
A_Sk
  • 4,532
  • 3
  • 27
  • 51
  • This is not necessary. If you use `GridviewDevicesBulkUpdate.Rows` only rows are returned which `RowType` is `DataControlRowType.DataRow`. You get the header or footer only in `RowCreated` or `RowDataBound`. This is documented in the [`GridView.Rows`-property](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rows%28v=vs.110%29.aspx): _"Only rows with their `RowType` property set to `DataControlRowType.DataRow` are stored in the Rows collection. The GridViewRow objects that represent the header, footer, and pager rows are not included in the collection."_ – Tim Schmelter Mar 18 '15 at 13:32
0

I guess the reason is that you use an invisible BoundField. You can read about it here:

Retrieve data from visible false BoundField of Gridview

So you could either follow the suggestions there or use a TemlateField with a label:

<Columns>
     <ItemTemplate>
        <Label runat="server" ID="LblDeviceID" Text='<%# Eval("DeviceID") %> ' Visible="false"  />
    </ItemTemplate>
    <asp:BoundField DataField="TerminalSNO" HeaderText="Serial No." ItemStyle-CssClass="visible-desktop"
        HeaderStyle-CssClass="visible-desktop" />
    <asp:BoundField DataField="SoftwareVersion" HeaderText="Software Version" ItemStyle-CssClass="visible-desktop"
        HeaderStyle-CssClass="visible-desktop" />
    <asp:TemplateField HeaderText="Includes ?">
        <ItemTemplate>
            <asp:CheckBox runat="server" ID="CheckBoxIncludes" Text="." CssClass="checkbox checkbox-danger" />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

Then you can retrieve the value in codebehind in following way:

Label lblDeviceID = (Label)Row.FindControl("LblDeviceID");
string DeviceID = lblDeviceID.Text;

Meanwhile i'm always using TemplateFields because you could use any html there and you don't run into such issues. They are simply more flexible.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939