How to put datagridview column in edit mode

Posted on 2010-01-04
Last Modified: 2012-06-22
I have a bound datagridview.  I would like to have the user click a button that would put one entire column into edit mode.  Let the user go down the rows editing this column, then click a button to save the new data to the grid and then datasource.  I can get current cell in edit mode but not sure how to do a column.
Question by:mgmhicks
    LVL 23

    Accepted Solution

    the following link has multi-row editing. Some changes to this code would work for entire column
    LVL 12

    Assisted Solution

    You could Fake the Column Selection by Hi-Lighting the rectangle. Then as Each Item is clicked or the User uses the arrow you can select the row for editing. To only edit the Cell in the column you will have to turn of 'editability' for the other cells. I think this is a property of the Cell.

    The Code Snippet below does the 'fake' hilighting

    In the DataGrid MouseDown Event you will  find out if you have selected a column header through the hit test. You then find the co-ordinate of the column and construct a rectangle out of that. If you are selecting multiple columns you can store the index of the columns and the rectangle in  ArrayList.

    Make sure you clear the ArrayList when you HitType is None.
    protected void dataGrid1_MouseDown (object sender, System.Windows.Forms.MouseEventArgs e)
    m_HitTest = dataGrid1.HitTest(e.X,e.Y);
    if ( m_HitTest.Type == DataGrid.HitTestType.ColumnHeader )
    int xCoordinate = this.GetLeftmostColumnHeaderXCoordinate( m_HitTest.Column );
    int yCoordinate = this.GetTopmostColumnHeaderYCoordinate( e.X, e.Y );
    int columnWidth = dataGrid1.TableStyles[0].GridColumnStyles
    int columnHeight = this.GetColumnHeight( yCoordinate );
    columnRegion = new Rectangle( xCoordinate, yCoordinate, columnWidth, columnHeight );
    else if(m_HitTest.Type == DataGrid.HitTestType.None)
    In the DataGrid paint method you just fill the rectangles you contructed before in MouseDown Event.
    private void dataGrid1_Paint(object sender, PaintEventArgs e)
    Graphics g = e.Graphics;
    if (m_HitTest != null && m_HitTest.Type == DataGrid.HitTestType.ColumnHeader )
    for (int i = 0; i < this.m_RectangleOfSelectedColumns.Count; i++)
    Rectangle columnRegion = (Rectangle)m_RectangleOfSelectedColumns[i];
    SolidBrush blackBrush = new SolidBrush( Color.FromArgb( 255, 0, 0, 0 ) );
    SolidBrush darkGreyBrush = new SolidBrush( Color.FromArgb( 100, 229, 229, 229 ) );
    Pen blackPen = new Pen( blackBrush, 1F );
    g.FillRectangle( darkGreyBrush, columnRegion);
    g.DrawRectangle( blackPen, columnRegion );

    Open in new window

    LVL 5

    Assisted Solution

    The following link has complete code for the "Q" that you asked for:

    Author Closing Comment

    Thanks guys, all solutions were different ways to handle this, but all are pretty complicated, so I approached the problem just editing the traditional way.  

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now