【问题标题】:Change name of the columns in Datagridview when only a few are selected仅选择少数几个时更改 Datagridview 中列的名称
【发布时间】:2016-12-02 08:41:51
【问题描述】:

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

        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();

【问题讨论】:

    标签: c# sql asp.net datagridview


    【解决方案1】:

    这可能对你有用

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

    你可以更改列的名称

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

    【讨论】:

    • 选择列有效,如何更改列名?
    • 乐于助人。您可以将此标记为答案。 :) 这向更广泛的社区表明您找到了解决方案
    • 我会的!我只能在 400 万内完成
    【解决方案2】:

    您可以更改 sql select 语句以仅选择您想要的列并给出标题名称,如下所示

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2019-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多