ASP.NET - 有条理地隐藏和显示Griview列

时间:2015-03-16 04:06:53

标签: c# asp.net sql-server gridview

我有一个gridview,当选择搜索选项中的一个条件时,它应该隐藏某些列。否则,应该查看该列。

我已经使用此代码隐藏了一列:

grdMarketingReport1.Columns[2].Visible = false;

但是当我搜索应该显示该列的另一个条件时,它不再显示。请注意,gridview属性Autogeneratecolumn设置为false

更新

按钮点击事件

  if ((txtHiddenLoc.Text == "ALL") && (txtHiddenBus.Text == "ALL"))
                        {
                            executeQuery();
                            grdMarketingReport1.Columns[2].Visible = true;
                        }


else if (((txtHiddenLoc.Text == "ALL") && (txtHiddenBus.Text != "ALL")) 

                        {
                            executeQuery();
                            grdMarketingReport1.Columns[2].Visible = false;

                        }

...

public void executeQuery()
{

    SqlCommand cmd = new SqlCommand("spMarketingReport1", con);
    cmd.CommandTimeout = 120;
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@RP", txtHiddenRP.Text);
    cmd.Parameters.AddWithValue("@Location", txtHiddenLoc.Text);
    cmd.Parameters.AddWithValue("@DateFrom1", txtdatefrom1.Text);
    cmd.Parameters.AddWithValue("@DateTo1", txtdateto1.Text);

    try
    {
        con.Open();
        grdMarketingReport1.EmptyDataText = "No Records Found";
        grdMarketingReport1.DataSource = cmd.ExecuteReader();
        grdMarketingReport1.DataBind();

    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        con.Close();
        con.Dispose();
    }

它正确隐藏了列,但当另一个条件应用于查看列时,列上的数据/值将被清空。

3 个答案:

答案 0 :(得分:0)

我会使用您的搜索条件设置您的列的可见性。同时尝试重新排序顺序。

bool displayCol = ((txtHiddenLoc.Text == "ALL") && (txtHiddenBus.Text == "ALL"));
executeQuery();
grdMarketingReport1.Columns[2].Visible = dispalyCol;

如果这不起作用,请尝试隐藏并在网格视图的DataBoundPre-Render事件中显示该列。

答案 1 :(得分:0)

您可以在Grid

的RowDataBound事件上执行此操作
Protected Sub grdMarketingReport1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles grdMarketingReport1.RowDataBound

    If e.Row.RowType = DataControlRowType.Header Then
        If Checkbox.Checked = True Then
            e.Row.Cells(2).Style("display") = "none"
        End IF
    End IF

    If e.Row.RowType = DataControlRowType.DataRow Then
        If Checkbox.Checked = True Then
            e.Row.Cells(2).Style("display") = "none"
        End IF
    End IF

End Sub

答案 2 :(得分:0)

我想我找到了最简单的答案,只是在gridview的数据绑定之前显示并隐藏代码。就像这样:

**grdMarketingReport1.Columns[2].Visible = true;
  grdMarketingReport1.DataBind();**

并在executeQuery()

中包含了TRY CLAUSE中的条件
    try
    {
        con.Open();

        if ((txtHiddenBus.Text == "NONE") && (txtHiddenRP.Text == "NONE"))
        {
            grdMarketingReport1.EmptyDataText = "No Records Found";
            grdMarketingReport1.DataSource = cmd.ExecuteReader();
            grdMarketingReport1.Columns[1].Visible = false;
            grdMarketingReport1.Columns[2].Visible = false;
            grdMarketingReport1.Columns[3].Visible = false;
            grdMarketingReport1.Columns[4].Visible = false;
            grdMarketingReport1.Columns[5].Visible = false;
            grdMarketingReport1.Columns[6].Visible = false;
            grdMarketingReport1.DataBind();
        }

        else if ((txtHiddenBus.Text != "NONE") && (txtHiddenRP.Text == "NONE"))
        {
            grdMarketingReport1.EmptyDataText = "No Records Found";
            grdMarketingReport1.DataSource = cmd.ExecuteReader();
            grdMarketingReport1.Columns[1].Visible = false;
            grdMarketingReport1.Columns[2].Visible = false;

            grdMarketingReport1.Columns[4].Visible = false;
            grdMarketingReport1.Columns[5].Visible = false;
            grdMarketingReport1.Columns[6].Visible = false;
            grdMarketingReport1.DataBind();
        }

        else
        {
            grdMarketingReport1.EmptyDataText = "No Records Found";
            grdMarketingReport1.DataSource = cmd.ExecuteReader();
            grdMarketingReport1.Columns[1].Visible = true;
            grdMarketingReport1.Columns[2].Visible = true;
            grdMarketingReport1.Columns[3].Visible = true;
            grdMarketingReport1.Columns[4].Visible = true;
            grdMarketingReport1.Columns[5].Visible = true;
            grdMarketingReport1.Columns[6].Visible = true;
            grdMarketingReport1.DataBind();
        }



    }