How to Change Column Name in DataGridView in Runtime

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kashifabbasi786
    New Member
    • Sep 2008
    • 4

    How to Change Column Name in DataGridView in Runtime

    I want to change the DataGridView Heading on run time by giving in textbox.


    how i will do this,plz help me

    Thnx
  • kashifabbasi786
    New Member
    • Sep 2008
    • 4

    #2
    How to Change Column Name in DataGridView in Runtime

    HI,

    I want to change the columnName of DataGridView on Runtime,The datasource of DataGridView is DataTable which i fil from DataBase,

    i does not want to change the name of column in query.


    Plz reply

    Comment

    • MrMancunian
      Recognized Expert Contributor
      • Jul 2008
      • 569

      #3
      Can you post a piece of code in which you initiate the gridview? Furthermore, it would be nice if you told us what language you are using...

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        All the columns in the DataGridView have a HeaderText Property which you can set to a different string for display purposes.
        All thought it is recomended to have the correct query in the first place
        Last edited by Frinavale; Nov 9 '09, 08:54 PM. Reason: Added link to documentation/examples for the DataGridViewColumn.HeaderText property.

        Comment

        • AlmightyJu
          New Member
          • Sep 2008
          • 7

          #5
          If you mean in VB.NET then its

          DataGrid.Column s(0).HeaderText = MyTextBox.Text

          and use that wherever you need to.

          hope it helps

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            THREADS MERGED.
            Please do not double post your questions. It is against the posting guidelines.

            MODERATOR

            Comment

            • jrff9173
              New Member
              • Nov 2009
              • 1

              #7
              Just figured it out.

              DataGridView1.C olumns(DataGrid View1.SelectedC olumns.ToString ).HeaderText = TextBox2.Text

              Where:

              Datagridview1 is your datagridview control
              and Textbox2 is your Control to name the column


              Originally posted by AlmightyJu
              If you mean in VB.NET then its

              DataGrid.Column s(0).HeaderText = MyTextBox.Text

              and use that wherever you need to.

              hope it helps
              That only lets you name the first column

              EDIT: Code does not work. Please reply telling me where I went wrong

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                If you have a valid column selected, setting the HeaderText property will change its visual style.
                The code you used had no error checking, which is very bad form.

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Originally posted by jrff9173
                  Just figured it out.

                  DataGridView1.C olumns(DataGrid View1.SelectedC olumns.ToString ).HeaderText = TextBox2.Text

                  Where:

                  Datagridview1 is your datagridview control
                  and Textbox2 is your Control to name the column



                  That only lets you name the first column

                  EDIT: Code does not work. Please reply telling me where I went wrong
                  You probably went wrong where you are setting the HeaderText.
                  I'm not sure what you are trying to do but if the TextBox2.Text does not have any Text in it you could be referring to Null/Nothing which will throw a NullException error.

                  Or You may have gone wrong accessing your column....does the following column exist?

                  DataGridView1.C olumns(DataGrid View1.SelectedC olumns.ToString )

                  Actually it's more likely that this is your problem.
                  I'm pretty sure that DataGridView1.S electedColumns. ToString will return you "DataGridViewCo lumnCollection" .

                  In all likelihood you do not have a column named "DataGridViewCo lumnCollection" so when you try to set the HeaderText of this non-existent column you are getting a NullReferenceEx ception.

                  You probably want something like the following:
                  Code:
                  Dim newHeaderText As String = IIf(String.IsNullOrEmpty(TextBox2.Text), "", TextBox2.Text)
                  
                  If DataGridView1.SelectedColumns IsNot Nothing AndAlso DataGridView1.SelectedColumns.Count > 0 Then
                    For Each selectedColumn As DataGridViewColumn In DataGridView1.SelectedColumns
                      selectedColumn.HeaderText = newHeaderText
                    Next
                  End If
                  -Frinny

                  Comment

                  Working...