【问题标题】:renaming column header texts in gridview with autogeneratecolumns = true使用 autogeneratecolumns = true 重命名 gridview 中的列标题文本
【发布时间】:2013-11-27 20:41:29
【问题描述】:

我有一个gridview,在用户按下带有查询代码的按钮后显示来自1个表的查询数据,这个gridview有autogeneratecolumns = false,我自己添加了BoundField DataField标题文本,例如,第一列db 的名称为“prd_nome”,我将其更改为“nome”,一切正常。

现在我在数据库中创建了另一个表,具有不同的名称和不同的列名,我还添加了另一个按钮,其中包含查询后面的代码以从该表中获取数据,但为了显示数据现在 autogeneratecolumns = true,我测试了按钮并且它可以工作,但是标题文本与列名相同,并且 ID 列也显示为两个查询。

如何将标题文本硬编码为我想要的内容以及如何隐藏 ID 列?通过标签?通过boundfield数据字段?通过 AS sql 运算符? 如果有人可以帮助我,我将不胜感激,因为我是 c# 新手

这是当前的gridview asp代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>

这是传递第一个表中的查询的按钮的代码:

protected void Button1_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();

    SqlConnection connection = new SqlConnection(GetConnectionString());
    connection.Open();
    SqlCommand sqlCmd = new SqlCommand("SELECT * FROM [ERPDQ].[dbo].[prd] WHERE prd_desc LIKE ('torta%')", connection);
    SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

          sqlDa.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        GridView1.DataSource = dt;
        GridView1.DataBind();

    }
    connection.Close();
}

这是传递有关第二个表的查询的按钮中的代码:

protected void Button6_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();

    SqlConnection connection = new SqlConnection(GetConnectionString());
    connection.Open();
    SqlCommand sqlCmd = new SqlCommand("SELECT * FROM [ERPDQ].[dbo].[outra]", connection);
    SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

    sqlDa.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        GridView1.DataSource = dt;
        GridView1.DataBind();

    }
    connection.Close();
}

}

【问题讨论】:

  • 请以您想要的标题文本的方式进行查询

标签: c# asp.net gridview dynamic autogeneratecolumn


【解决方案1】:

你没有提到有一个GridView或者有两个GridView。

如果您的页面上有两个 GridView,那么只需为两个 GridView 添加 onrowdatabound 事件。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"  onrowdatabound="GridView1_RowDataBound"></asp:GridView>

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="true" onrowdatabound="GridView2_RowDataBound"></asp:GridView>

如果只有 1 个 GridView 则在页面加载之前定义两个布尔变量 (btn1Click,btn2Click)。在 button1 点击事件设置 btn1Click 为 ture 和 btn2Click 为 false ,在 button2 点击事件设置 btn2Click 为 true 和 btn1Click 为 false 并在OnRowDataBound 事件根据 btn click 设置列名。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  //supposing id is the first cell,change the index according to your grid
  // hides the first column
  e.Row.Cells[0].Visible = false; 

  if(btn1Click)
  {
     //to set header text
     if (e.Row.RowType == DataControlRowType.Header)
     {
        e.Row.Cells[1].Text = "Cell Text";
        e.Row.Cells[2].Text = "Cell Text";
     }
  }
  else if(btn2Click)
  {
     //to set header text
     if (e.Row.RowType == DataControlRowType.Header)
     {
        e.Row.Cells[1].Text = "Cell Text";
        e.Row.Cells[2].Text = "Cell Text";
      }
  }
}

如果对您有用,请将两个答案都标记为正确。

【讨论】:

    【解决方案2】:

    使用 AutoGenerateColumns=True 时,您可以在 select 语句中使用 AS sql 运算符更改列的标题文本(正如您已经建议的那样)。

    虽然通常避免使用 AutoGenerateColumns=True,因为它不灵活。正如您指出的那样,您会被简单的事情所困扰,例如隐藏一列。

    这里是一个如何使用RowDataBound 事件隐藏 ID 列的示例

    protected void rowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            for (int i = 0; i < e.Row.Controls.Count; i++)
            {
                var headerCell = e.Row.Controls[i] as DataControlFieldHeaderCell;
                if (headerCell != null)
                {
                    if (headerCell.Text == "name_of_id_column")
                    {
                        headerCell.Visible = false;
                        Page.Items["IDCellIndex"] = i;
                        break;
                    }
                }
            }   
        }
        else if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Footer)
        {
            int idCellIndex = Convert.ToInt32(Page.Items["IDCellIndex"]);
    
            e.Row.Controls[idCellIndex].Visible = false;
        }
    }
    

    【讨论】:

      【解决方案3】:

      您可以在 GridView RowDataBound 事件中执行此操作

      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
        //supposing id is the first cell,change the index according to your grid
        // hides the first column
        e.Row.Cells[0].Visible = false; 
      
        //to set header text
        if (e.Row.RowType == DataControlRowType.Header)
        {
           e.Row.Cells[1].Text = "Cell Text";
           e.Row.Cells[2].Text = "Cell Text";
        }
      }
      

      【讨论】:

      • 这里gridview绑定数据源不固定所以不能指定为静态
      【解决方案4】:

      谢谢大家,我相信我会根据项目实施后的方向使用一些答案。

      关于我的 gridview 列标题重命名问题,我发现 AS 运算符可以很好地满足当前需求,再次感谢提供的所有帮助

      【讨论】:

        猜你喜欢
        • 2011-11-25
        • 1970-01-01
        • 1970-01-01
        • 2012-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-18
        相关资源
        最近更新 更多