views:

40

answers:

2

I have a gridview in which the first column in a checkbox. The grid displays one or more records that are returned from the database upon user search (the grid is populated in a proc called "protected void DisplayGridData()"). If the "inactive" column of that record is "1", I would like to have the checkbox disabled. How can I accomplish that?

The html code is:

            <asp:GridView ID="gvCanadaOrders" runat="server" AutoGenerateColumns="False" CellPadding="2" CellSpacing="2" GridLines="None" Width="100%" AllowPaging="True" PageSize="30"
                onpageindexchanging="gvCanadaOrders_PageIndexChanging" ForeColor="#333333" DataKeyNames="RecID">
                <Columns>
                    <asp:TemplateField HeaderText="Disable" >
                        <ItemTemplate>
                            <asp:CheckBox ID="cbPONumber" runat="server"/>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <%--<asp:BoundField DataField="Rec_ID" HeaderText="Rec_ID" HtmlEncode="False"></asp:BoundField>--%>
                    <asp:BoundField DataField="Inactive" HeaderText="Inactive" HtmlEncode="False" ></asp:BoundField>
                    <asp:BoundField DataField="PO_Number" HeaderText="PO Number" HtmlEncode="False" ></asp:BoundField>
                    <asp:BoundField DataField="VENDOR_NAME" HeaderText="Vendor Name"></asp:BoundField>
                    <asp:BoundField DataField="ITEM_DESC" HeaderText="Item Description"></asp:BoundField>
                    <asp:BoundField DataField="MFG_PART_NO" HeaderText="MFG Part Number"></asp:BoundField>
                    <asp:BoundField DataField="System_DATE" HeaderText="System Date"></asp:BoundField>
                    <asp:BoundField DataField="PRINT_DATE" HeaderText="Print 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>

The code that loads the gridview is:

protected void btnDisable_Click(object sender, EventArgs e)    
{
    try
    {
        string strPONumber = "";
        foreach (GridViewRow gvr in gvCanadaOrders.Rows)
        {
            if (((CheckBox)gvr.FindControl("cbPONumber")).Checked == true)
            {
                string strRec_ID = gvCanadaOrders.DataKeys[gvr.RowIndex].Value.ToString();

                //Update table here, diable inactive field;
                SqlConnection con = new SqlConnection(strConn);
                string sqlCanadaOrdersUpdate = "usp_CanadaOrders_Close";
                SqlCommand cmdCanadaOrdersUpdate = new SqlCommand(sqlCanadaOrdersUpdate, con);

                cmdCanadaOrdersUpdate.CommandType = CommandType.StoredProcedure;

                cmdCanadaOrdersUpdate.Parameters.Add(new SqlParameter("@rec_ID", SqlDbType.VarChar, 50));
                cmdCanadaOrdersUpdate.Parameters["@rec_ID"].Value = strRec_ID;

                cmdCanadaOrdersUpdate.Parameters.Add(new SqlParameter("@usr_id", SqlDbType.VarChar, 50));
                cmdCanadaOrdersUpdate.Parameters["@usr_id"].Value = User.Identity.Name.Substring(User.Identity.Name.Length - 7);

                con.Open();

                SqlDataAdapter sqladaCanadaOrdersUpdate = new SqlDataAdapter(cmdCanadaOrdersUpdate);
                cmdCanadaOrdersUpdate.ExecuteNonQuery();
            }
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Purchase Order " + strPONumber + " has been disabled.');", true);
        }
    }

The code for gridview databoundrow is:

void gvCanadaOrders_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Display the company name in italics.
        e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
    }
}
A: 

Handle the RowDataBound event and use e.Row parameter to find your checkbox.

Jeroen
+1  A: 

Hook in to the RowDataBound event of the GridView. From there, you can cast the DataItem to your record type. A test for the flag then lets you modify the row controls.

I have this code in VB.net and did a quick conversion per your C# tag, so please forgive any errors.

void myGridView_RowDataBound(Object sender, GridViewRowEventArgs e) {
    if (e.Row.RowType == DataControlRowType.DataRow) {
        MyObject myObj = (myObj)e.Row.DataItem;
        if (myObj.flag) {
            CheckBox cb = (CheckBox)e.Row.FindControl("myCheckBox");
            cb.Enabled=false;
        }
    }
}

EDIT: Per your edit to the question, you do not have OnRowDataBound in your markup. You would need to add OnRowDataBound="gvCanadaOrders_RowDataBound". It works the same way you've gon OnPageIndexChanging.

rchern
how is the RowDataBound event called?
user279521
Specify the name in the markup or you can do it dynamically in your code-behind. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx
rchern
The name is specified in the markup, but the RowDataBound event never gets called (if I put a breakpoint in it)
user279521
Edit your original question with the markup and the function in your code-behind?
rchern
Thanks for helping me thru the process rchem.
user279521