Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

gridview enableviewstate

Status
Not open for further replies.

wimani

Programmer
Joined
Jan 6, 2008
Messages
4
Location
US
In the gridview (enableviewstate = false), the first column is a boundfield having a DataField = "CallDetailID" and the second column is a checkbox (enableviewstate = false), and there are other columns.

The code-behind for a button on the page goes through all the checkboxes that are selected and retrieves the CallDetailID. When the CallDetailID boundfield visibility is true then I can retrieve the IDs, but when the visibility is set to false the IDs cannot be retrieved.

Is there a setting on the gridview that has to be changed?

Here is some of the aspx and c# code:
Code:
<asp:GridView runat="server" ID="gvCallDetails" AutoGenerateColumns="false" OnSorting="gvCallDetails_Sorting" 
                                                        OnRowCommand="gvCallDetails_RowCommand" OnRowDataBound="gvCallDetails_DataBound" BorderStyle="Solid" 
                                                        CssClass="dealgrid" BackColor="white" CellPadding="0" CellSpacing="0" GridLines="None" BorderColor="#4D80D1" 
                                                        BorderWidth="1px" AllowSorting="true" AllowPaging="false"  PageSize="10" EnableViewState="False" DataKeyNames="CallDetailID">
                                                        <RowStyle CssClass="gridbody" />
                                                        <AlternatingRowStyle CssClass="altgridbody" />
                                                        <HeaderStyle HorizontalAlign="Center" CssClass="GridviewFixedHeader" ForeColor="White"  />
                                                        <Columns>
                                                            <asp:BoundField DataField="CallDetailID" Visible="false" ReadOnly="true" />
                                                            <asp:TemplateField>
                                                                <HeaderTemplate>
                                                                    <asp:CheckBox CssClass="cbody" ID="chkHeaderSelect" runat="server" AutoPostBack="true" EnableViewState="false" />
                                                                </HeaderTemplate>
                                                                <ItemTemplate>
                                                                    <asp:CheckBox CssClass="cbody" ID="chkSelected" runat="server" EnableViewState="false" />
                                                                </ItemTemplate>
                                                                <ItemStyle HorizontalAlign="Center"/>
                                                                <HeaderStyle Width="60px" />
                                                            </asp:TemplateField>
Code:
                 int iResult;
                 StringBuilder sbCallDetail = new StringBuilder();
                 //GridViewRow row = new GridViewRow();

                 for (int i = 0; i <= gvCallDetails.Rows.Count - 1; i++)
                 {
                     if ((((CheckBox)gvCallDetails.Rows[i].FindControl("chkSelected")).Checked) == true)
                     {
                         sbCallDetail.Append(gvCallDetails.Rows[i].Cells[0].Text + ',');
                     }
                 }
gvCallDetails.Rows.Cells[0].Text doesn't retrieve the CallDetailID for the selected checkboxes unless the visibility of the CallDetailID boundfield is true.
 
Think it's working now..

Changed:
sbCallDetail.Append(gvCallDetails.Rows.Cells[0].Text + ',');

To:
sbCallDetail.Append(Convert.ToString(gvCallDetails.DataKeys[0]) + ',');

And now I'm able to retrieve the values of CallDetailID
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top