使Gridview列不可见,但仍然访问其数据

本文关键字:访问 数据 Gridview | 更新日期: 2023-09-27 18:02:26

我已经读了很多关于这个的帖子,特别是在这个网站上,但我仍然找不到我想要的东西。我在。cs文件中填充我的Gridview,而不是在。aspx中添加到它的Columns部分。我有两个字段,我希望能够仍然引用,但希望他们不可见。我知道Gridview对这类事情不是特别好……但有没有什么方法能让我做到这一点呢?

cs

protected void searchFill()
{
    orderByString = orderByList.SelectedItem.Value;
    fieldString = searchTextBox.Text;
    string sqlStatement = "SELECT fName AS [First], lName as [Last], addr AS [Address], email AS [Email], phone AS [Phone], ccType AS [Credit Card Type], length AS [Length], IdentityColumn, processed FROM SecureOrders WHERE fName LIKE '%" + fieldString + "%' OR lName LIKE'%" + fieldString + "%' OR addr LIKE'%" + fieldString + "%' OR addr2 LIKE'%" + fieldString + "%' OR city LIKE'%" + fieldString + "%' OR state LIKE'%" + fieldString + "%' OR zip LIKE'%" + fieldString + "%' OR zip LIKE'%" + fieldString + "%' OR country LIKE'%" + fieldString + "%' OR email LIKE'%" + fieldString + "%' OR phone LIKE'%" + fieldString + "%' OR ccType LIKE'%" + fieldString + "%' OR ccNum LIKE'%" + fieldString + "%' OR ccExp LIKE'%" + fieldString + "%' OR cwaSource LIKE'%" + fieldString + "%' OR cwaJoined LIKE'%" + fieldString + "%' OR length LIKE'%" + fieldString + "%' OR delivery LIKE'%" + fieldString + "%' OR price LIKE'%" + fieldString + "%' OR url LIKE'%" + fieldString + "%' ORDER BY " + orderByString;
    using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
    {
        connection.Open();
        SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlStatement, connection);
        dataAdapter.SelectCommand.Parameters.AddWithValue("@fieldString", fieldString);
        dataAdapter.SelectCommand.Parameters.AddWithValue("@orderByString", orderByString);
        SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
        DataSet dataSet = new DataSet();
        dataAdapter.Fill(dataSet, "SecureOrders");
        DataView source = new DataView(dataSet.Tables[0]);
        DefaultGrid.DataSource = source;
        DefaultGrid.DataBind();
        connection.Close();
    }
}

 <asp:GridView ID="DefaultGrid" 
        runat = "server" 
        DataKeyNames = "IdentityColumn"
        onselectedindexchanged = "DefaultGrid_SelectedIndexChanged"
        autogenerateselectbutton = "true"
        enableviewstate = "false"
        selectedindex="1">
    <SelectedRowStyle BackColor="Azure"
        forecolor="Black"
        font-bold="true" />
    <Columns>
    <asp:TemplateField HeaderText = "Processed">
        <ItemTemplate>
            <asp:CheckBox 
                ID="CheckBoxProcess" 
                AutoPostBack="true" 
                Checked='<%#Eval("processed") %>'
                OnCheckedChanged="CheckBoxProcess_CheckedChanged"
                runat="server"
                Enabled="true" />
        </ItemTemplate>
    </asp:TemplateField>
    </Columns>
 </asp:GridView>

使Gridview列不可见,但仍然访问其数据

当你想使用列值时,只需在GridView中使用<%# Eval("ColumnName") %>
所有的数据源(在你的例子中是数据集)值都可以在GridView上下文中的DataBinder中使用。

您可以对想要隐藏的值使用HiddenFields。当您可以隐藏控件时,不需要创建整个列。

<asp:HiddenField runat="server" ID="hdf_Identity" Value='<%# Eval("IdentityField") %>' />

当你想找到它们时,只需在gridview行中搜索它们。以下是一些msdn文档,可能会有所帮助。

你可以用display:hidden创建一个css类"HiddenCol"。然后用该类为想要隐藏的列设置样式。