Reputation: 83
i am using Gridview to display data from database can any body show me how to change column name here is my code
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>
Upvotes: 1
Views: 3582
Reputation: 3498
Give aliases to your columns which are returned and they will be set as your column name. For e.g
string me = "select doc_name as 'Document Name', doc_isvalid as 'Is Valid'
from tbldocstatus";
Document Name
and Is Valid
will be your column name.
Upvotes: 0
Reputation: 7301
try this :DGVArchive is the name of gridview
DGVArchive.Columns[0].HeaderText = "text 1";
DGVArchive.Columns[1].HeaderText = "text2";
Upvotes: 0
Reputation: 71
You should do that in GridView's RowDataBound event which is triggered for every GridViewRow
after it was databound.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "Date";
}
}
or you can set AutoGenerateColumns to false
and add the columns declaratively on 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>
Upvotes: 2
Reputation: 2624
On the Databound event handler type this:
GridView2.Columns[0].HeaderText = "Your preferred text"
Column[0] is your the column index of the column you want to change
Upvotes: 0