如何将数据表的标题名称/列名称更改为 dataGridView

Posted

技术标签:

【中文标题】如何将数据表的标题名称/列名称更改为 dataGridView【英文标题】:How to Change the Header Name / Column Name Of a Datatable Fed to dataGridView 【发布时间】:2019-04-28 18:15:06 【问题描述】:

我正在使用DataTable 填充DataGridView 以进行搜索/过滤。

DataGridView 显示实际的数据库列名。

但我想更改这些列名。例如tbl_Users 有一个标记为LocalNo 的列,我想将其更改为Unit No.

这是我的代码

namespace FrequencyBook

public partial class Form11 : Form

    private static Form11 alreadyOpened = null;
    public Form11()
    
        InitializeComponent();
        
            if (alreadyOpened != null && !alreadyOpened.IsDisposed)

            
                alreadyOpened.Focus();            // Bring the old one to top
                Shown += (s, e) => this.Close();  // and destroy the new one.
                return;
            

            // Otherwise store this one as reference
            alreadyOpened = this;
        
    

    private DataTable dt = new DataTable();

    private void Form11_Load(object sender, EventArgs e)
    
        dataGridView11.DataSource = GetSearchForm();
    


    private DataTable GetSearchForm()
    
        string connString = ConfigurationManager.ConnectionStrings["FrequencyBook.Properties.Settings.db_FrequenciesConnectionString"].ConnectionString;

        using (OleDbConnection conn = new OleDbConnection(connString))

        
            using (OleDbCommand cmd = new OleDbCommand("Select * FROM tbl_Users", conn))

            
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                dt.Load(reader);
            
        

        return dt;
    

    private void closeFormToolStripMenuItem_Click(object sender, EventArgs e)
    
        this.Close();
    

    private void resetFormToolStripMenuItem_Click(object sender, EventArgs e)
    
        tboxSearchLicensee.Clear();
        tboxSearchCallsign.Clear();
        tboxSearchLocation.Clear();
        tboxSearchBand.Clear();
        tboxSearchRID.Clear();
        tboxSearchLocalNo.Clear();
    

    private void tboxSearchLicensee_TextChanged(object sender, EventArgs e)
    
        DataView dv = dt.DefaultView;
        dv.RowFilter = "Licensee LIKE '%" + tboxSearchLicensee.Text + "%'" ;
    

    private void tboxSearchCallsign_TextChanged(object sender, EventArgs e)
    
        DataView dv = dt.DefaultView;
        dv.RowFilter = "Callsign LIKE '%" + tboxSearchCallsign.Text + "%'" ;
    

    private void tboxSearchLocation_TextChanged(object sender, EventArgs e)
    
        DataView dv = dt.DefaultView;
        dv.RowFilter = "Location LIKE '%" + tboxSearchLocation.Text + "%'" ;
    

    private void tboxSearchBand_TextChanged(object sender, EventArgs e)
    
        DataView dv = dt.DefaultView;
        dv.RowFilter = "Band LIKE '%" + tboxSearchBand.Text + "%'" ;
    

    private void tboxSearchRID_TextChanged(object sender, EventArgs e)
    
        DataView dv = dt.DefaultView;
        dv.RowFilter = string.Format("RID1 LIKE '%0%' OR RID2 LIKE '%0%' OR RID3 LIKE '%0%' OR RID4 LIKE '%0%'", tboxSearchRID.Text);

        //DataView dv = dt.DefaultView;
        //dv.RowFilter = "RID1 LIKE '%" + tboxSearchRID.Text + "%'" ;
    

    private void tboxSearchLocalNo_TextChanged(object sender, EventArgs e)
    
        DataView dv = dt.DefaultView;
        dv.RowFilter = "LocalNo LIKE '%" + tboxSearchLocalNo.Text + "%'" ;
    

我在这里读过这篇文章: How to change the DataTable Column Name? 第一个答案对我来说很有意义,但我不知道我需要在哪里添加新行。我已经在几个地方尝试过,但没有 JOY。

【问题讨论】:

您可以在返回“dt”之前更改 GetSearchForm 中链接中提到的列名。您也可以在 Form_Load 中执行此操作。您需要将GetSearchForm返回的值赋给一个变量,并且在设置DataSource之前,您可以更改Column名称 您可以更改dataGridView1.Columns[N].HeaderText = "[SomethingElse]";。使用一些来源来匹配对:DataColumn[N].ColumnName -> DGV.Column[N].HeaderText。看起来您只是在过滤行。 谢谢阿努。我知道它会起作用,但我一直把它放在错误的位置,我没有将 DataTable.Columns 更改为 dt.Columns。在返回 dt 之前工作完美; .....再次感谢您 其实@Anu Viswan,我确实发现了一个问题。虽然您的解决方案确实解决并修复了标题名称,但当我用搜索词填写实际表单时没有任何反应。这显然是因为下面的 dv.RowFilter 行。 【参考方案1】:

这是我的新代码。它适用于标题/列名称更改,但它破坏了实际的搜索/过滤功能。我实际上将新行改回原来的名称并让表单再次工作。有什么建议?这是我的搜索/过滤表单的屏幕截图: My search form

namespace FrequencyBook

public partial class Form11 : Form

    private static Form11 alreadyOpened = null;
    public Form11()
    
        InitializeComponent();
        
            if (alreadyOpened != null && !alreadyOpened.IsDisposed)

            
                alreadyOpened.Focus();            // Bring the old one to top
                Shown += (s, e) => this.Close();  // and destroy the new one.
                return;
            

            // Otherwise store this one as reference
            alreadyOpened = this;
        
    

    private DataTable dt = new DataTable();

    private void Form11_Load(object sender, EventArgs e)
    
        dataGridView11.DataSource = GetSearchForm();
    


    private DataTable GetSearchForm()
    
        string connString = ConfigurationManager.ConnectionStrings["FrequencyBook.Properties.Settings.db_FrequenciesConnectionString"].ConnectionString;

        using (OleDbConnection conn = new OleDbConnection(connString))

        
            using (OleDbCommand cmd = new OleDbCommand("Select * FROM tbl_Users", conn))

            
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                dt.Load(reader);
            
        
        dt.Columns["ID"].ColumnName = "ID";
        dt.Columns["Licensee"].ColumnName = "Licensee";
        dt.Columns["Callsign"].ColumnName = "Callsign";
        dt.Columns["LocalNo"].ColumnName = "Unit";
        dt.Columns["Location"].ColumnName = "Location";
        dt.Columns["FreqBand"].ColumnName = "Band";
        dt.Columns["Email"].ColumnName = "Email";
        dt.Columns["Phone"].ColumnName = "Phone";
        dt.Columns["RID1"].ColumnName = "RID1";
        dt.Columns["RID2"].ColumnName = "RID2";
        dt.Columns["RID3"].ColumnName = "RID3";
        dt.Columns["RID4"].ColumnName = "RID4";
        dt.Columns["Notes"].ColumnName = "Notes";

        return dt;
    

    private void closeFormToolStripMenuItem_Click(object sender, EventArgs e)
    
        this.Close();
    

    private void resetFormToolStripMenuItem_Click(object sender, EventArgs e)
    
        tboxSearchLicensee.Clear();
        tboxSearchCallsign.Clear();
        tboxSearchLocation.Clear();
        tboxSearchBand.Clear();
        tboxSearchRID.Clear();
        tboxSearchLocalNo.Clear();
    

    private void tboxSearchLicensee_TextChanged(object sender, EventArgs e)
    
        DataView dv = dt.DefaultView;
        dv.RowFilter = "Licensee LIKE '%" + tboxSearchLicensee.Text + "%'" ;
    

    private void tboxSearchCallsign_TextChanged(object sender, EventArgs e)
    
        DataView dv = dt.DefaultView;
        dv.RowFilter = "Callsign LIKE '%" + tboxSearchCallsign.Text + "%'" ;
    

    private void tboxSearchLocation_TextChanged(object sender, EventArgs e)
    
        DataView dv = dt.DefaultView;
        dv.RowFilter = "Location LIKE '%" + tboxSearchLocation.Text + "%'" ;
    

    private void tboxSearchBand_TextChanged(object sender, EventArgs e)
    
        DataView dv = dt.DefaultView;
        dv.RowFilter = "Band LIKE '%" + tboxSearchBand.Text + "%'" ;
    

    private void tboxSearchRID_TextChanged(object sender, EventArgs e)
    
        DataView dv = dt.DefaultView;
        dv.RowFilter = string.Format("RID1 LIKE '%0%' OR RID2 LIKE '%0%' OR RID3 LIKE '%0%' OR RID4 LIKE '%0%'", tboxSearchRID.Text);

        //DataView dv = dt.DefaultView;
        //dv.RowFilter = "RID1 LIKE '%" + tboxSearchRID.Text + "%'" ;
    

    private void tboxSearchLocalNo_TextChanged(object sender, EventArgs e)
    
        DataView dv = dt.DefaultView;
        dv.RowFilter = "Unit LIKE '%" + tboxSearchLocalNo.Text + "%'" ;
    

    private void button1_Click(object sender, EventArgs e)
    
        //This button has changed to btnReturnToUsers
        this.Hide();//Hide the 'current' form,  form11 
                    //show another form ( form12 )   
        Form12 frm = new Form12();
        frm.ShowDialog();
        //Close the form.(form11)
        this.Close();
    

    private void btnReturnToMain_Click(object sender, EventArgs e)
    
        this.Hide();//Hide the 'current' form,  form11 
                    //show another form ( form2 )   
        Form2 frm = new Form2();
        frm.ShowDialog();
        //Close the form.(form11)
        this.Close();
    

【讨论】:

以上是关于如何将数据表的标题名称/列名称更改为 dataGridView的主要内容,如果未能解决你的问题,请参考以下文章

当列更改数据库中的名称时,如何扩展或更新解析器以允许向后兼容

将名称附加或粘贴到 R 中的列名称

@NamedQuery 将表/列名称更改为大写导致异常

WordPress:如何将类别页面标题更改为仅类别名称?

jQuery UI datepicker:如何将下拉列表中的月份名称从短名称更改为长名称?

如何将图像上传的标题更改为 django 类中的名称