C# 使用autogeneratecolumns=true重命名gridview中的列标题文本
我有一个gridview,在用户按下带有代码隐藏查询的按钮后显示1个表中的查询数据,该gridview的autogeneratecolumns=false,我自己添加了BoundField数据字段标题文本,例如,数据库中的第一列名为“prd_nome”,我将其更改为“nome”,一切都按计划进行 现在,我在db中创建了另一个表,具有不同的名称和不同的列名,我还添加了另一个按钮,该按钮带有代码隐藏查询,以从该表中获取数据,但为了现在显示数据autogeneratecolumns=true,我测试了该按钮,它可以工作,但是,标题文本与列名相同,并且两个查询都显示ID列 如何将标题文本硬编码到我想要的内容,以及如何隐藏ID列?通过标签?通过边界字段数据字段?通过AS sql操作符? 如果有人能帮助我,我将不胜感激,因为我是个新手 以下是当前gridview asp代码:C# 使用autogeneratecolumns=true重命名gridview中的列标题文本,c#,asp.net,gridview,dynamic,autogeneratecolumn,C#,Asp.net,Gridview,Dynamic,Autogeneratecolumn,我有一个gridview,在用户按下带有代码隐藏查询的按钮后显示1个表中的查询数据,该gridview的autogeneratecolumns=false,我自己添加了BoundField数据字段标题文本,例如,数据库中的第一列名为“prd_nome”,我将其更改为“nome”,一切都按计划进行 现在,我在db中创建了另一个表,具有不同的名称和不同的列名,我还添加了另一个按钮,该按钮带有代码隐藏查询,以从该表中获取数据,但为了现在显示数据autogeneratecolumns=true,我测试了
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>
下面是传递关于第二个表的查询的按钮的代码:
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();
}
}您可以在GridView行数据绑定事件上执行此操作
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";
}
}
使用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;
}
}
受保护的void行数据绑定(对象发送方,GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.Header)
{
for(int i=0;i
您没有提到有一个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>
如果对您有效,请将两个答案都标记为正确。谢谢大家,我相信根据项目实施后的方向,我会使用一些答案
关于我的gridview列标题重命名问题,我发现AS操作符可以满足当前的需要,再次感谢提供的所有帮助请以您想要的标题文本的方式进行查询。此处gridview绑定数据源是不固定的,因此您不能将其指定为静态的
<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>
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";
}
}
}