我正在使用Gridview来显示数据库中的数据,任何正文都可以告诉我如何更改列名这里是我的代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string strcon = ConfigurationManager.ConnectionStrings["yccwebportalConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strcon))
{
string me = "select doc_name, doc_isvalid,doc_nextrenewdate,DATEDIFF(d, doc_nextrenewdate, GETDATE()) as Difference from tbldocstatus";
SqlCommand cmd = new SqlCommand(me, con);
con.Open();
GridView2.DataSource = cmd.ExecuteReader();
GridView2.DataBind();
con.Close();
}
}
}
<asp:GridView ID="GridView2" runat="server" Width="753px">
</asp:GridView>
答案 0 :(得分:2)
您应该在GridView的RowDataBound事件中执行此操作,该事件在数据绑定后为每个GridViewRow
触发。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "Date";
}
}
或者您可以将AutoGenerateColumns设置为false
并在aspx上以声明方式添加列:
<asp:gridview id="GridView1"
onrowdatabound="GridView1_RowDataBound"
autogeneratecolumns="False"
emptydatatext="No data available."
runat="server">
<Columns>
<asp:BoundField DataField="DateField" HeaderText="Date"
SortExpression="DateField" />
</Columns>
</asp:gridview>
答案 1 :(得分:0)
在Databound事件处理程序中输入:
GridView2.Columns[0].HeaderText = "Your preferred text"
列[0]是您要更改的列的列索引
答案 2 :(得分:0)
为返回的列提供别名,并将它们设置为列名。例如
string me = "select doc_name as 'Document Name', doc_isvalid as 'Is Valid'
from tbldocstatus";
Document Name
和Is Valid
将是您的专栏名称。
答案 3 :(得分:0)
试试这个: DGVArchive 是gridview的名称
DGVArchive.Columns[0].HeaderText = "text 1";
DGVArchive.Columns[1].HeaderText = "text2";