HelpThis
HelpThis

Reputation: 81

display a long text in a datagridview cell

I am trying to display a text of about 100-500 words contained in a string into two columns of a single row of a datatable which is then set to be the DataSource value for a DataGridView control.

Right now even though it renders it is very sluggish/scrolling takes forever.

I have already set the DefaultStyleMode to WordWrap = true, and adjusted the row height to display the text.

Are there any alternatives to speed this up or should i investigate adding a TextBox to the DataGridView's cell?

Upvotes: 2

Views: 4202

Answers (1)

Oscar Mederos
Oscar Mederos

Reputation: 29863

What about showing only a few characters and make it clickable so when the user clicks on it, the entire text can be shown in a pop-up?

First of all, you must have stored the original text somewhere in your application. Let's suppose you have the texts in an array string[] texts

All you have to do is:

  • Add only a portion of the text to the DataGridView control instead of the entire text

You can do this using Split method in the string class. Example:

string text = "Oscar Mederos";
string portion = text.Substring(0, 3); //portion will be "Osc"

You can add ... at the end of the string if you want.

  • Program the event CellClick of the DataGridView.

Suscribe in your application to that event, and do something like:

void DataGridView1_OnCellClick(object sender, DataGridViewCellEventArgs e)
{
    int rowClicked = e.RowIndex;
    int columnClicked = e.ColumnIndex;

    ///If the column clicked was the one that has the long texts, 
    //just find the original text in 'texts' using 'rowClicked' and show the 
    //message using MessageBox or creating a new Form for that purpose and 
    //showing it using ShowDialog()
}

Upvotes: 1

Related Questions