CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2011
    Posts
    9

    Unhappy How to rename DataGridView Header by double click

    I am having a datagrid view. In that suppose I give the header name as wrong, suppose say, instead of "test", i gave it as "tst". So i need to rename in this case. So what i expect is I need to double click that header and rename it. But in code I can't directly give it as "test", since it can be whatever. By double clicking i need to change in that display itself. I can able to catch the double click event. It is as below. Please suggest me a code which makes me to edit and rename the header.

    private void dgvParametersEdit_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {

    }

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How to rename DataGridView Header by double click

    see if this can help you (attached: sources)

    Note:
    1)
    the event for doubleclick is ColumnHeaderMouseDoubleClick
    2)
    frmInputBox is a second form I added to let user input a text to put in the proper column. If you have any difficoult, downolad attached zip and look at sources. I used Vs 2010.
    Code:
     private void dgvParametersEdit_ColumnHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                frmInputBox fx = new frmInputBox("Column renaming", "insert new column text", dgvParametersEdit.Columns[e.ColumnIndex].HeaderText);
                if (fx.ShowDialog(this) == DialogResult.OK)
                {
                    dgvParametersEdit.Columns[e.ColumnIndex].HeaderText = fx.ReturnValue;
                }
            }
    Attached Files Attached Files
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Nov 2011
    Posts
    9

    Re: How to rename DataGridView Header by double click

    Thanks for your inputs. To rename yo have implemented a new form.
    But I dont think they will accept in my project to add a new form for renaming.
    I have attached the code of mine. In that frmPsetSignalsEditor.cs is my main form.
    In that you can see a blue colored icon with symbol '+', its for adding the new signal or pset in my case. It will display a new form once yo click that and that form is frmAddNewNspcp.cs. ( In that form if we enter the signal name and press ok it will start to display in the grid).

    Since I already have a form, i would like to use the same form for renaming also.
    I don't know whether this is possible or not, but why I am saying is, after adding the signal
    I want to update the name in many things( like in driver and sheet, in our project we have a
    current driver and also excel sheet in which we add the entered signal ) So if in the same form
    I try to rename means, that ok button becomes same to both cases and once i rename the signal
    name, the changes will happen everywhere.

    Also if yo see, we will validate the name in such a way that, the same name should not exist
    anywhere in our sheet and you can see that code in btnNewPSetOrSignal_Click in frmPsetSignalsEditor.cs.

    That function will do all validation and also updates the name every place where it is neccessary.
    dgvParametersEdit_CellDoubleClick is the event which is called when that header is double clicked.
    In that place they would have mentioned ( if e.RowIndex < 0, return ). So I think we need to add our code. In place of return which will rename the header and it should update every where as like what the
    function btnNewPSetOrSignal_Click does.

    I have attached the code in 2 zip files..
    Attached Files Attached Files

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How to rename DataGridView Header by double click

    I am not going to step into your project.
    You do not need a new form to rename a column header.
    If you have a grid and you want to change a column header at runtime, what you need is:
    Code:
    yourGridName.Columns[theIndexOrTheNameOfTheCoulmn].HeaderText ="the text you want to display as header"
    in any case:
    want to update the name in many things( like in driver and sheet, in our project we have a
    current driver and also excel sheet in which we add the entered signal ) So if in the same form
    I try to rename means, that ok button becomes same to both cases
    and once i rename the signal
    name, the changes will happen everywhere.
    it might be because of english is not my language, but I did not understand what you wrote.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to rename DataGridView Header by double click

    I want to update the name in many things( like in driver and sheet, in our project we have a
    current driver and also excel sheet in which we add the entered signal ) So if in the same form
    I try to rename means, that ok button becomes same to both cases and once i rename the signal
    name, the changes will happen everywhere.
    That is why God invented Flags. Simply set a mode flag when you double click the header then in the OK button code check the mode flag so you know what you should do.
    Always use [code][/code] tags when posting code.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured