仅选择少数时,更改Datagridview中列的名称

时间:2016-12-02 08:41:51

标签: c# sql asp.net datagridview

我希望在只选择几个时更改列的名称。
这是我的代码,用于选择列。
现在,我怎样才能更改他们的名字?

        OleDbDataAdapter myadapt = new OleDbDataAdapter(mycommand);
        myadapt.Fill(myset, "Properties");
        DataTable tbSearchResult = myset.Tables["Properties"];
        DataTable tbTemp = new DataTable();

        tbTemp = tbSearchResult.DefaultView.ToTable(true, "DescriptionCities", "DescriptionSurface", "DescriptionBathroom", "DescriptionPrices", "DescriptionRooms", "DescriptionType");

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

2 个答案:

答案 0 :(得分:2)

这可能会为你做到这一点

string[] selectedColumns = new[] { "DescriptionCities", "DescriptionSurface", "DescriptionBathroom", "DescriptionPrices", "DescriptionRooms", "DescriptionType" };
DataTable dt= new DataView(tbSearchResult).ToTable(false, selectedColumns);

并更改您可以执行的列的名称

dt.Columns["DescriptionCities"].ColumnName = "Cities";

答案 1 :(得分:0)

您可以更改sql select语句以仅选择所需的列,并提供标题名称,如下所示

select DescriptionCities as Cities, DescriptionSurface as [Description Surface] from YourTable