【问题标题】:Changing the name of a field displayed in a DataGridView?更改 DataGridView 中显示的字段名称?
【发布时间】:2014-04-25 15:28:52
【问题描述】:

如何在C# 中更改DataGridView 中显示的字段名称?我正在使用SQLite Database

这是我的代码:

const string filename = @"database.db";
const string sql = "select * from legal_tbl;";
var conn = new SQLiteConnection("Data Source=" + filename + ";Version=3;");
try
{
   conn.Open();
   DataSet ds = new DataSet();
   var da = new SQLiteDataAdapter(sql, conn);
   da.Fill(ds);
   DataTable dt = ds.Tables[0];
   this.data_view.DataSource = dt;
}
catch (Exception ex)
{
   MessageBox.Show(ex + "");
}

但是,它显示的是数据库中字段的名称。我想改变它。 前任。 id_num => ID

【问题讨论】:

    标签: c# sql sqlite datagridview


    【解决方案1】:

    尝试更新列的 HeaderText 属性:

    this.data_view.Columns["id_num"].HeaderText = "ID";
    

    【讨论】:

    • 我不知道为什么这不起作用。也许,我错误地放置了该代码。顺便说一句,我粘贴在 try-catch 块中
    • @user3569641 确保在设置网格的数据源之后设置它。该列不会事先存在。如果这不是问题,那么列名可能不匹配。
    【解决方案2】:

    LarsTech 的答案是我会使用的答案,但作为替代方案,您可以为选择中的列设置别名:

    const string sql = "select id_num as ID, [etc...] from legal_tbl;";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      相关资源
      最近更新 更多