none
How to view a cell in a DataGridView

    Question

  • Hi,

    I have a few datagridviews on my forms which ocassionally have cells that contain too much text to view at once.
    I have the row height set to autosize, but sometimes the contents are so large that the height of the row is more than the height of the entire datagrid view.

    When I use scroll, it puts the next row at the top of the view, rather than letting me see the bottom half of the row I'm looking at.

    Making the view larger is not an option.

    Is it possible to either change the scroll so that the cell view moves up as any normal window would (rather than to the next row), or to add a "balloon type" popup to the cursor so that I can place the mouse over it to see the contents (currently I get 1 line of text which I also can't read!)
    Sunday, July 05, 2009 10:50 AM

Answers

  • Hi GraemeP,

     

    Based on my understanding, the key problem is how to show the large content of the cell. As far as I know, there are two ways:

    1.    Show the text on a tooltip. This is supported directly in DataGridView. You can move mouse onto the cell, if the content is large, a tooltip will show automatically.

    2.    Create a form to show the large content. We can add a RichTextBox in the form and let it fill the form. When the content is large and the user do an special operation, such as middle clicking the cell, the form will show. This is my code snippet:

    private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)

    {

        if (e.Button == MouseButtons.Middle)

        {

            object value = this.dataGridView1[e.ColumnIndex, e.RowIndex].FormattedValue;

            if (value != null)

            {

                string text = value.ToString();

                if (text.Length > 200)

                {

                    MessageForm form = new MessageForm();

                    form.Text = text;

                    form.Show();

                }

            }

        }

    }

    Let me know if this helps.
    Aland Li

    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Tuesday, July 07, 2009 5:22 AM
    Moderator
  • Private Sub dataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs) 
        
        
        If e.Button = MouseButtons.Middle Then 
            
            
            Dim value As Object = Me.dataGridView1(e.ColumnIndex, e.RowIndex).FormattedValue 
            
            If value IsNot Nothing Then 
                
                
                Dim text As String = value.ToString() 
                
                If text.Length > 200 Then 
                    
                    
                   msgbox(text)                
                End If 
                
            End If 
            
        End If 
    End Sub 

    For future Reference, you can use: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Ashray Lavsi
    If my post answers your Question, don't forget to "Mark it as Answer"
    Monday, July 13, 2009 8:57 PM
  • Have edited the code.. It should work now..
    Ashray Lavsi
    If my post answers your Question, don't forget to "Mark it as Answer"
    • Marked as answer by GraemeP Tuesday, July 14, 2009 5:29 PM
    Tuesday, July 14, 2009 4:49 PM

All replies

  • Hi GraemeP,

     

    Based on my understanding, the key problem is how to show the large content of the cell. As far as I know, there are two ways:

    1.    Show the text on a tooltip. This is supported directly in DataGridView. You can move mouse onto the cell, if the content is large, a tooltip will show automatically.

    2.    Create a form to show the large content. We can add a RichTextBox in the form and let it fill the form. When the content is large and the user do an special operation, such as middle clicking the cell, the form will show. This is my code snippet:

    private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)

    {

        if (e.Button == MouseButtons.Middle)

        {

            object value = this.dataGridView1[e.ColumnIndex, e.RowIndex].FormattedValue;

            if (value != null)

            {

                string text = value.ToString();

                if (text.Length > 200)

                {

                    MessageForm form = new MessageForm();

                    form.Text = text;

                    form.Show();

                }

            }

        }

    }

    Let me know if this helps.
    Aland Li

    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Tuesday, July 07, 2009 5:22 AM
    Moderator
  • Hi sorry for the delay in replying.
    That sounds great but could someone help me converting this to VB?

    Thanks
    Monday, July 13, 2009 8:53 PM
  • Private Sub dataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs) 
        
        
        If e.Button = MouseButtons.Middle Then 
            
            
            Dim value As Object = Me.dataGridView1(e.ColumnIndex, e.RowIndex).FormattedValue 
            
            If value IsNot Nothing Then 
                
                
                Dim text As String = value.ToString() 
                
                If text.Length > 200 Then 
                    
                    
                   msgbox(text)                
                End If 
                
            End If 
            
        End If 
    End Sub 

    For future Reference, you can use: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Ashray Lavsi
    If my post answers your Question, don't forget to "Mark it as Answer"
    Monday, July 13, 2009 8:57 PM
  • Hi that's great - except that I'm getting: 

    "Type "MessageForm" is not defined"
    Tuesday, July 14, 2009 4:46 PM
  • Have edited the code.. It should work now..
    Ashray Lavsi
    If my post answers your Question, don't forget to "Mark it as Answer"
    • Marked as answer by GraemeP Tuesday, July 14, 2009 5:29 PM
    Tuesday, July 14, 2009 4:49 PM
  • Great thanks
    Tuesday, July 14, 2009 5:30 PM