views:

207

answers:

1

I am using checkboxs in a GridView. I need to determine the value in the cell in the 2nd column, if a checkbox has been checked.

I am using C# 2008 and ASP.net

                    <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="False" CellPadding="4" GridLines="None" Width="100%" AllowPaging="True" PageSize="20" 
                    onpageindexchanging="gvOrders_PageIndexChanging" ForeColor="#333333">
                    <Columns>
                        <asp:TemplateField HeaderText="VerifiedComplete" >
                            <ItemTemplate>
                                <asp:CheckBox ID="cbPOID" runat="server"/>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="PurchaseOrderID" HeaderText="PurchaseOrderID" HtmlEncode="False" ></asp:BoundField>
                        <asp:BoundField DataField="VENDOR_ID" HeaderText="Vendor ID"></asp:BoundField>
                        <asp:BoundField DataField="VENDOR_NAME" HeaderText="Vendor Name"></asp:BoundField>
                        <asp:BoundField DataField="ITEM_DESC" HeaderText="Item Desc"></asp:BoundField>
                        <asp:BoundField DataField="SYS_DATE" HeaderText="System Date"></asp:BoundField>
                    </Columns>
                    <FooterStyle CssClass="GridFooter" BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <PagerStyle CssClass="GridPager" ForeColor="#333333" BackColor="#FFCC66" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                    <HeaderStyle CssClass="GridHeader" BackColor="#990000" Font-Bold="True" ForeColor="White"  />
                    <RowStyle CssClass="GridItem" BackColor="#FFFBD6" ForeColor="#333333" />
                    <AlternatingRowStyle  CssClass="GridAltItem" BackColor="White" />
                </asp:GridView>





        protected void btnDisable_Click(object sender, EventArgs e)
    {
            foreach (GridViewRow gvr in gvOrders.Rows)
            {

                if (((CheckBox)gvr.FindControl("cbPOID")).Checked == true)
                {
                    string strPrimaryid = gvr.Cells[2].ToString();
                }
            }

    }
+1  A: 

Another way to do this, to avoid accessing the cells of the GridView by index, is to convert the desired BoundField to a TemplateField in the designer. Then use the FindControl method to get the text value in that cell. By default, the designer will give the Label control of the new TemplateField a name like "Label1."

I would add some pictures to show the designer part, if that process were easier. At any rate, the aspx would change like so, which you could do manually too:

<asp:BoundField DataField="PurchaseOrderID" ...</asp:BoundField>

becomes

<asp:TemplateField HeaderText="PurchaseOrderID">
 <ItemTemplate>
  <asp:Label ID="Label1" runat="server" Text='<%# Bind("PurchaseOrderID") %>'></asp:Label>
 </ItemTemplate>
 <EditItemTemplate>
  <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PurchaseOrderID") %>'></asp:TextBox>
 </EditItemTemplate>
</asp:TemplateField>

Then you would make this simple code change:

string strPrimaryid = gvr.FindControl("Label1").Text;

However, I think a better way to do this is by utilizing the DataKeys features of the GridView control. The first step would be to set the DataKeyNames property of your GridView:

<asp:GridView ID="gvOrders" runat="server" DataKeyNames="PurchaseOrderID" ...>

Then, assuming your PurchaseOrderID column is of type int, you would change that same line to be:

int primaryid = (int)gvOrders.DataKeys[gvr.RowIndex].Value;
Sean