none
C# Rename a DataTable Column (viewed by a DataGridView)

    Question

  • I have a DataTable (unbound) which is the datasource to a DataGridView.  I want to allow the user to change the column name (e.g. DataTable read in from CSV file, but want to allow to change header values).  I don't seem to be able to easily do this.  So two questions:
    1 - simply renaming the column using dataTable.Columns[index].ColumnName causes the DataGridView to not show any columns past (and including) the column whose name is changed (e.g. col1, col2, col3, col4 - change col3 to COLUMN3, and only col1, and col2 show up and NO DataRows are displayed).
    2 - Is there an easy way of editing column headers?  Or do I need to catch the double-click event, create a textbox, put it over the proper header and then use that to edit the header?

    Thanks in advance for any help on this!
    Saturday, November 17, 2007 5:05 PM

Answers

  •  jafwin wrote:

    1 - simply renaming the column using dataTable.Columns[index].ColumnName causes the DataGridView to not show any columns past (and including) the column whose name is changed (e.g. col1, col2, col3, col4 - change col3 to COLUMN3, and only col1, and col2 show up and NO DataRows are displayed).


    I create a test CSV file whose content likes this:

    col1,col2,col3
    aa1,bb1,cc1
    aa2,bb2,cc2
    aa3,bb3,cc3

    Then I test with the following code, but no problem with changing the column name.

    Code Block

    private void Form1_Load(object sender, EventArgs e)

            {

                dt = new DataTable();

     

                StreamReader sr = new StreamReader(@"c:\test\test.csv");

                string[] headers = sr.ReadLine().Split(',');

                foreach (string header in headers)

                {

                    dt.Columns.Add(header);

                }

     

                while (sr.Peek() >= 0)

                {

                    DataRow dr = dt.NewRow();

                    dr.ItemArray = sr.ReadLine().Split(',');

                    dt.Rows.Add(dr);

                }

     

                this.dataGridView1.DataSource = dt;

            }

     

            DataTable dt;

     

            private void button1_Click(object sender, EventArgs e)

            {

                dt.Columns[2].ColumnName = "Column3";

            }

     

     



     jafwin wrote:

    2 - Is there an easy way of editing column headers?  Or do I need to catch the double-click event, create a textbox, put it over the proper header and then use that to edit the header?


    Since the column header cell is not editable, you have to put a TextBox over the cell for editing.

    Friday, November 23, 2007 5:45 AM

All replies

  • So, it looks like several people have viewed this, but no responses yet - maybe I didn't explain everything.  The reason I I want to change the DataTable (and not use just a DataGridViewColumn.HeaderText) is for this to be data driven.  I'll pass the DataTable to be outputed later and I want the updated column names to be in the data (rather than just the DataGridView).

    Help please? Smile
    Monday, November 19, 2007 12:31 PM
  •  jafwin wrote:

    1 - simply renaming the column using dataTable.Columns[index].ColumnName causes the DataGridView to not show any columns past (and including) the column whose name is changed (e.g. col1, col2, col3, col4 - change col3 to COLUMN3, and only col1, and col2 show up and NO DataRows are displayed).


    I create a test CSV file whose content likes this:

    col1,col2,col3
    aa1,bb1,cc1
    aa2,bb2,cc2
    aa3,bb3,cc3

    Then I test with the following code, but no problem with changing the column name.

    Code Block

    private void Form1_Load(object sender, EventArgs e)

            {

                dt = new DataTable();

     

                StreamReader sr = new StreamReader(@"c:\test\test.csv");

                string[] headers = sr.ReadLine().Split(',');

                foreach (string header in headers)

                {

                    dt.Columns.Add(header);

                }

     

                while (sr.Peek() >= 0)

                {

                    DataRow dr = dt.NewRow();

                    dr.ItemArray = sr.ReadLine().Split(',');

                    dt.Rows.Add(dr);

                }

     

                this.dataGridView1.DataSource = dt;

            }

     

            DataTable dt;

     

            private void button1_Click(object sender, EventArgs e)

            {

                dt.Columns[2].ColumnName = "Column3";

            }

     

     



     jafwin wrote:

    2 - Is there an easy way of editing column headers?  Or do I need to catch the double-click event, create a textbox, put it over the proper header and then use that to edit the header?


    Since the column header cell is not editable, you have to put a TextBox over the cell for editing.

    Friday, November 23, 2007 5:45 AM
  • I also need to change the column name of a data table - and I want to be able to capture the change. 

     

    DataTable.GetChanges() does not seem to capture the column name change.  Is there any other way to check if the name of a column was renamed.

     

    I was comparing the column names of the old and new tables.  this way I could know if a column was added or deleted - but it does not tell me if a column was renamed - which I need for history.  Do you have any ideas how I could accomplish this? 

    Friday, December 07, 2007 3:44 PM
  • I found a solution to check if a column was renamed:

     

    When renaming the column I added the following lines of code:

    Code Block

     

    //Rename the row

    if (!m_Groups.Groups.Columns[strGroup].ExtendedProperties.ContainsKey(m_Groups.OriginalGroupName))

    {

    //save tag with the original name for when the group is saved

    m_Groups.Groups.Columns[strGroup].ExtendedProperties.Add(m_Groups.OriginalGroupName, strGroup);

    }

     

     

    Then when saving the groups I loop through the columns and check if the column contains the key.  If it does then I know the column was renamed. 

     

    I hope that this can help somebody else.

    Monday, December 10, 2007 7:12 PM