开发者

ASP.net Making Unbound column invisible

I'm trying to get a column in my unbound gridview to be invisible, specifically the id. I'm binding the grid with ADO.net, so I cant just set the tag in the html code to invisible. Here is some code...

    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, CONVE开发者_开发百科RT(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();


    }

this is your basic good old ASP.net search function. However, in this gridview I enable selecting like this...

    protected void gv_search_SelectedIndexChanged(object sender, EventArgs e)
    {

        Response.Redirect("selectedOrder.aspx?oid=" + gv_search.SelectedValue.ToString());
    }

I know I can set the oid to be the datakey tab. However, I want to make this invisible. I have tried this method here...

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

It gave me this error...

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

Does anyone know how to do this? Thanks


Declare the columns you want displayed inside the aspx file. Tell the grid to not generate columns using the AutoGenerateColumns="False" property.

<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>

Set the desired columns to Visible="False".


Change it to the DataBound event:

protected void gv_search_DataBound(object sender, EventArgs e)
{

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜