简体   繁体   中英

How to bind the hiding columns in Gridview

I'm binding Gridview1 to Sqldatasource1 shown below:

   <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:ShiftScheduleConnectionString %>' DeleteCommand="DELETE FROM [Example] WHERE [ID] = @ID" InsertCommand="INSERT INTO [Example] ([AssociateName], [Login], [Logout], [Home_Login], [Home_Logout]) VALUES (@AssociateName, @Login, @Logout, @Home_Login, @Home_Logout)" SelectCommand="SELECT * FROM [Example]" UpdateCommand="UPDATE [Example] SET [AssociateName] = @AssociateName, [Login] = @Login, [Logout] = @Logout, [Home_Login] = @Home_Login, [Home_Logout] = @Home_Logout WHERE [ID] = @ID">
        <DeleteParameters>
            <asp:Parameter Name="ID" Type="Int32"></asp:Parameter>
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="AssociateName" Type="String"></asp:Parameter>
            <asp:Parameter Name="Login" Type="String"></asp:Parameter>
            <asp:Parameter Name="Logout" Type="String"></asp:Parameter>
            <asp:Parameter Name="Home_Login" Type="String"></asp:Parameter>
            <asp:Parameter Name="Home_Logout" Type="String"></asp:Parameter>
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="AssociateName" Type="String"></asp:Parameter>
            <asp:Parameter Name="Login" Type="String"></asp:Parameter>
            <asp:Parameter Name="Logout" Type="String"></asp:Parameter>
            <asp:Parameter Name="Home_Login" Type="String"></asp:Parameter>
            <asp:Parameter Name="Home_Logout" Type="String"></asp:Parameter>
            <asp:Parameter Name="ID" Type="Int32"></asp:Parameter>
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:GridView ID="GridView1" OnRowUpdating="GridView1_RowUpdating" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:CommandField ShowEditButton="True"></asp:CommandField>
            <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" InsertVisible="False" SortExpression="ID"></asp:BoundField>
            <asp:BoundField DataField="AssociateName" HeaderText="AssociateName" SortExpression="AssociateName"></asp:BoundField>
            <asp:BoundField DataField="Login" HeaderText="Login" SortExpression="Login"></asp:BoundField>
            <asp:BoundField DataField="Logout" HeaderText="Logout" SortExpression="Logout"></asp:BoundField>
            <asp:BoundField DataField="Home_Login" HeaderText="Home_Login" SortExpression="Home_Login"></asp:BoundField>
            <asp:BoundField DataField="Home_Logout" HeaderText="Home_Logout" SortExpression="Home_Logout"></asp:BoundField>
        </Columns>
    </asp:GridView>

after clicking a button, a column should be invisible.

  protected void Button1_Click(object sender, EventArgs e)
{
    GridView1.Columns[2].Visible = false;
}

this is working fine..but after editing and updating any data in gridview, the hidden column(AssociateName) is not binding and showing the bank value. Is there any solution to update hidden column as well..

It happens because you are probably doing after a page reload, so the element state loses the column visible status and use the visible property you have in the aspx page (in this case is visible="true" because it is omitted).

To fix it, you have to change the way you reload the page so it doesn't lose the status or set visible="false" , according to the rule you want, in the PageLoad method too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM