I have read many posts on this, especially here on this site, but I still can't find exactly what I'm looking for. I'm filling my Gridview in the .cs file, rather than adding to the Columns section of it in the .aspx. I have two fields I want to be able to still reference, but want them invisible. I know Gridview isn't particularly good for this sort of thing...but is there some way that I can achieve this that I'm missing?
.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();
}
}
.aspx
<asp:GridView ID="DefaultGrid"
runat = "server"
DataKeyNames = "IdentityColumn"
onselectedindexchanged = "DefaultGrid_SelectedIndexChanged"
autogenerateselectbutton = "true"
enableviewstate = "false"
selectedindex="1">
<SelectedRowStyle BackColo开发者_如何学运维r="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>
Wherever you want to use the column value just use <%# Eval("ColumnName") %>
inside the GridView.
All the datasource (dataset in your case) values are available in the DataBinder within the context of the GridView.
You can just use HiddenFields for the values you want to hide. There is no need to create a whole column when you can just have the control hidden.
<asp:HiddenField runat="server" ID="hdf_Identity" Value='<%# Eval("IdentityField") %>' />
When you want to find them just search for them in the gridview row. Here is some msdn documentation that may help.
You can create a css class "HiddenCol" with display:hidden. Then style whichever column you want to hide with that class.
精彩评论