ASP.net使Unbound列不可见

时间:2011-07-26 04:17:02

标签: c# asp.net visual-studio-2010 ado.net

我试图让我的未绑定网格视图中的列不可见,特别是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

有谁知道怎么做?感谢

2 个答案:

答案 0 :(得分:3)

声明要在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"

答案 1 :(得分:0)

将其更改为DataBound事件:

protected void gv_search_DataBound(object sender, EventArgs e)
{

    gv_search.Columns[0].Visible = false;
}