使用HTML或C#在GridView中更改列名

时间:2011-10-09 12:38:07

标签: c# asp.net sql visual-studio gridview

重点是我在项目中使用了GridView。 我已使用GridViewSQLConnSQlDataAdapter

将值分配给DataSet
GridView1.DataSource=ds;
GridView1.DataBind();

关键是输出显示了对最终用户不友好的表的列名。

如何更改?

2 个答案:

答案 0 :(得分:5)

使用您自己的GridView列,可以分配gridview的Header文本。转到GridView的属性 - >列 - >添加列并将DataBound设置为DB列名称和标题文本属性。

并且不要忘记将GridGe的AutoGeneratedColumns属性设置为false

答案 1 :(得分:0)

您可以按顺序位置更改列名称 - 尽管如果列重新排序,这不是一种强大的方法:

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

标题单元格中有一个名为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>

它指定用于页面自动化,所以:

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

会非常强大。

当然你可以通过列

使用案例或其他东西来骑自行车

HTH