从数据库中删除Gridview列中的标题文本

时间:2014-12-02 00:13:09

标签: c# asp.net

您好我想知道当我从数据库中提取构建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();
        }
    }


}

1 个答案:

答案 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>