ASP.net使未绑定列不可见

本文关键字:绑定 net ASP | 更新日期: 2023-09-27 18:03:36

我试图在我未绑定的gridview中获得一列是不可见的,特别是id。我将网格与ADO.net绑定,所以我不能将html代码中的标记设置为不可见。下面是一些代码…

    protected void btn_search_Click(object sender, EventArgs e)
    {
        SqlConnection cs = new SqlConnection("Data Source=WILSON-PC; Initial Catalog=KamManOnline; Integrated Security=TRUE");

        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        da.SelectCommand = new SqlCommand("SELECT oid, Supplier, DepartmentA, DepartmentB, DepartmentC, OrderNumber, CONVERT(varchar(10), PORecieptDate, 101) AS PORecieptDate, CONVERT(varchar(10), ProductRecieved, 101) AS ProductRecieved, CONVERT(varchar(10),POEntryDate, 101) AS POEntryDate FROM tbl_OrderD WHERE (Supplier = @Supplier OR @Supplier IS NULL) AND (DepartmentA = @DepartmentA OR @DepartmentA IS NULL) AND (OrderNumber = @OrderNumber OR @OrderNumber IS NULL) AND (PORecieptDate BETWEEN @FromA AND @ToA OR (@FromA IS NULL AND @ToA IS NULL)) AND (ProductRecieved BETWEEN @FromB AND @ToB OR (@FromB IS NULL AND @ToB IS NULL)) AND (POEntryDate BETWEEN @FromC AND @ToC OR (@FromC IS NULL AND @ToC IS NULL))", cs);

        if (!chk_Suppliers.Checked) da.SelectCommand.Parameters.Add("@Supplier", SqlDbType.VarChar).Value = ddl_SupplierView.Text.ToString();
        else da.SelectCommand.Parameters.Add("@Supplier", SqlDbType.VarChar).Value = DBNull.Value;
        if (!chk_Departments.Checked) da.SelectCommand.Parameters.Add("@DepartmentA", SqlDbType.VarChar).Value = ddl_DepartmentView.Text.ToString();
        else da.SelectCommand.Parameters.Add("@DepartmentA", SqlDbType.VarChar).Value = DBNull.Value;
        if (txt_orderNumView.Text != "") da.SelectCommand.Parameters.Add("@OrderNumber", SqlDbType.VarChar).Value = txt_orderNumView.Text.ToString();
        else da.SelectCommand.Parameters.Add("@OrderNumber", SqlDbType.VarChar).Value = DBNull.Value;
        if (txt_PORecievedFrom.Text != "" && txt_PORecievedTo.Text != "")
        {
            da.SelectCommand.Parameters.Add("FromA", SqlDbType.SmallDateTime).Value = txt_PORecievedFrom.Text.ToString();
            da.SelectCommand.Parameters.Add("ToA", SqlDbType.Date).Value = txt_PORecievedTo.Text.ToString();
        }
        else
        {
            da.SelectCommand.Parameters.Add("FromA", SqlDbType.Date).Value = DBNull.Value;
            da.SelectCommand.Parameters.Add("ToA", SqlDbType.Date).Value = DBNull.Value;
        }
        if (txt_ProductRecievedFrom.Text != "" && txt_ProductRecievedTo.Text != "")
        {
            da.SelectCommand.Parameters.Add("FromB", SqlDbType.Date).Value = txt_PORecievedFrom.Text;
            da.SelectCommand.Parameters.Add("ToB", SqlDbType.Date).Value = txt_PORecievedTo.Text;
        }
        else
        {
            da.SelectCommand.Parameters.Add("FromB", SqlDbType.Date).Value = DBNull.Value;
            da.SelectCommand.Parameters.Add("ToB", SqlDbType.Date).Value = DBNull.Value;
        }
        if (txt_POEntryFrom.Text != "" && txt_poEntryTo.Text != "")
        {
            da.SelectCommand.Parameters.Add("FromC", SqlDbType.Date).Value = txt_PORecievedFrom.Text;
            da.SelectCommand.Parameters.Add("ToC", SqlDbType.Date).Value = txt_PORecievedTo.Text;
        }
        else
        {
            da.SelectCommand.Parameters.Add("FromC", SqlDbType.Date).Value = DBNull.Value;
            da.SelectCommand.Parameters.Add("ToC", SqlDbType.Date).Value = DBNull.Value;
        }
        ds.Clear();
        da.Fill(ds);
        gv_search.DataSource = ds.Tables[0];
        gv_search.DataBind();

    }

这是基本的老式ASP.net搜索函数。然而,在这个gridview中,我启用了这样的选择…

    protected void gv_search_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect("selectedOrder.aspx?oid=" + gv_search.SelectedValue.ToString());
    }

我知道我可以设置oid为datakey选项卡。但是,我想让它隐形。我已经试过这个方法了…

    protected void gv_search_RowCreated(object sender, GridViewRowEventArgs e)
    {
        gv_search.Columns[0].Visible = false;
    }

它给我这个错误…

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

有人知道怎么做吗?由于

ASP.net使未绑定列不可见

声明要在aspx文件中显示的列。告诉网格不要使用AutoGenerateColumns="False"属性生成列。

<asp:DataGrid id="DataGrid1" 
   runat="server" CssClass="grid" 
   AutoGenerateColumns="False">
   <Columns>
      <asp:BoundColumn 
         DataField="OrderID" ReadOnly="True" 
         HeaderText="Order ID" />
      <asp:BoundColumn 
         DataField="ShipName" HeaderText="Ship to" 
         ReadOnly="True" />
      <asp:BoundColumn 
         DataField="ShipCountry" HeaderText="Country" 
         ReadOnly="True" />
     <asp:BoundColumn 
         DataField="ShipCountry" HeaderText="Country" 
         Visible="False" />
   </Columns>
</asp:DataGrid>

设置所需列为Visible="False"

将其改为DataBound事件:

protected void gv_search_DataBound(object sender, EventArgs e)
{
    gv_search.Columns[0].Visible = false;
}