3

The point is I have used GridView in my project. And I have assigned the Values to GridView using the SQLConn, SQlDataAdapter and DataSet

GridView1.DataSource=ds;
GridView1.DataBind();

The point is that the output shows the column name of the tables which is unfriendly for end users.

How can I change it?

A.H.
  • 63,967
  • 15
  • 92
  • 126

2 Answers2

5

Use your own column of GridView and can assign the Header text of the gridview. Go to Properties of the GridView-->Columns-->Add the column and set the DataBound to the DB Column name and Header Text Property.

And Dont forget to set the AutoGeneratedColumns property to false of the gridview

Glory Raj
  • 17,397
  • 27
  • 100
  • 203
0

You can change the column name by ordinal position - although this is not a robust way if the columns get reordered:

grd.HeaderRow.Cells(iCount).Text = "my column name"

There is a (liitle known? little used?)property of the header cells called AccessibleHeaderText

<asp:TemplateField HeaderText="Default Name" AccessibleHeaderText="MY_FIXED_KEY_VALUE">
     <ItemTemplate>
         <asp:Label ID="CustRef" runat="server" Text='<%# Bind("MyField") %>'></asp:Label>
     </ItemTemplate>
</asp:TemplateField>

Its designated for page automation so:

For iCount = 0 To grd.HeaderRow.Cells.Count - 1
     Dim oCol As DataControlField = grd.Columns(iCount)
     If String.Compare(oCol.AccessibleHeaderText, "MY_FIXED_KEY_VALUE", True) = 0 Then
           grd.HeaderRow.Cells(iCount).Text = "my column name"
           Exit For
     End If
Next

Will be quite robust.

Of course you can use case or whatever for cycling though the columns

HTH

Ian P
  • 1,724
  • 1
  • 10
  • 12