您好我想知道当我从数据库中提取构建Gridview时,如何更改Gridview中列的标题文本。
以下是我构建GridView的方法。
SqlConnection Conn = new SqlConnection("REMOVED");
SqlDataReader rdr = null;
string commandString = "SELECT OrderNumber, CreatedDate, CreatedBy, CustomerID, Store_Number, Package FROM dbo.Orderheader";
try
{
Conn.Open();
SqlCommand Cmd = new SqlCommand(commandString, Conn);
rdr = Cmd.ExecuteReader();
GridView1.DataSource = rdr;
GridView1.DataBind();
}
catch (Exception ex)
{
// Log error
}
finally
{
if (rdr != null)
{
rdr.Close();
}
if (Conn != null)
{
Conn.Close();
}
}
}
答案 0 :(得分:0)
在header row object
事件中获取databound
并更改所需名称
void GridView1_DataBound(Object sender, EventArgs e)
{
// Get the header row.
GridViewRow headerRow = GridView1.HeaderRow;
headerRow.Cells[0].Text = "Order";
headerRow.Cells[1].Text = "Date";
}
OR
将AutoGenerateColumns
设置为False
并使用列bound fields
,
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<columns>
<asp:BoundField HeaderText="Order" DataField="OrderNumber" />
<asp:BoundField HeaderText="Date" DataField="CreatedDate" />
</columns>
</asp:GridView>