views:

455

answers:

3

Earlier today i was suggested in here to use a DataGridView to print messages that needed a individual mark as read.

I followed the suggestion, and with some reading online i managed to bind it to my message list with the following results after some tweaking.

alt text

Currently i have 2 issues, the first one is that i didn't find a way to resize the row height to display the full message, and the second one is that when the list is updated, the DataGridView doesn't display the modifications.

Any way to solve both problems? Or do i need to use something other than DataGridView, and in that case what should i be using?

Also, is there any way to urls contained in the message to become clickable and be opened in the default browser?

EDIT More info in relation to the binding.

Basically i have a class variable inside the form, and i do the initial binding with a button.

private void button1_Click(object sender, EventArgs e)
{
    list.Add(new Class1() { Message = "http://www.google.com/", Read = false });
    list.Add(new Class1() { Message = "Message way too long to fit in this small column width", Read = false });

    dataGridView1.DataSource = list;
}

I then have another button that adds some more entries just to test it, and i know the list is properly updated, but there are no changes in the dataGridView.

EDIT 2

If i wasn't clear before i need for the width to be fixed, and the cell height that contains the long text to be enlarged and display the text in 2 lines

A: 

I'll take a stab and see if I can help.

First off the row height. There are two DataGridView Methods called AutoResizeRow and AutoResizeRows which will adjust the height of the row to fit the contents.

Can you show us how you are binding your data to the DataViewGrid and how the data might be modified? That will help with the modifications not updating.

As for the link, unfortunately I can't seem to find an object which handles this sort of thing natively. Most likely you will first have to decide if the text going into the DataGridView is a link (using a Regular Expression, if you were me). Second, display it differently in the DataGridView (underline it, make it blue). Third, put a click event on it and when that cell is clicked handle that by throwing it out to a browser. I will look a little further into it though since this seems like a lot of work (and I will keep my fingers crossed that someone knows better than I do).

Tim C
Added more info to the initial post, i hope its easier to draw conclusions now.I tested the AutoResizeRows with different values, like DisplayedCells, but none of them split the text into more lines per height... what am i doing wrong?
brokencoding
Unfortunately I will have to look at this a bit more tonight but I will run some tests and see if I am help. Sorry for the wait!
Tim C
I dont mind waiting for help, especially when i'm already with a headache because of this particular problem.
brokencoding
+1  A: 

have you checked the options in the EditColumn using smart tag ?

  • you can add column of type DataGridViewLinkColumn, set its Text property to Message
  • Try removing any value from width and height properties for a column. In this way, it will set the column size (cell) size according to the data size.

hope this helps

Asad Butt
Changed it to DataGridViewLinkColumn, but that way all the text appears as a link, not just the urls.If i try to remove the column sizes it just returns that the property value is not valid. In the EditColumn options the best i could do was get a scroll bar to read the rest of the text, but that way the other column isnt visible witouth scrolling. I cant seem to find an option to keep a fixed width and show the rest of the text in a multiline way.
brokencoding
check this article, might help http://odetocode.com/Code/59.aspx
Asad Butt
The thing is that i dont want to resize the width of the column, i want the width to be fixed, and the cell height that contains the long text to be enlarged and display the text in 2 lines.Cant this be done?
brokencoding
A: 

Regarding the list not updating; there are two issues;

To notice add/remove, you need list binding events. The easiest way to do this is to ensure you use a BindingList<YourClass> rather than a List<YourClass>.

To notice changes to individual properties (in this context) you'll need to implement INotifyPropertyChanged on your type:

public class YourClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    private string message;
    public string Message
    {
        get { return message; }
        set { message = value; OnPropertyChanged("Message"); }
    }
    public bool isRead;
    [DisplayName("Read")]
    public bool IsRead
    {
        get { return isRead; }
        set { isRead = value; OnPropertyChanged("IsRead"); }
    }
}

For an example showing binding that to a list:

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        BindingList<YourClass> list = new BindingList<YourClass>();
        DataGridView grid = new DataGridView();
        grid.Dock = DockStyle.Fill;
        grid.DataSource = list;
        Button add = new Button();
        add.Text = "Add";
        add.Dock = DockStyle.Bottom;
        add.Click += delegate
        {
            YourClass newObj = new YourClass();
            newObj.Message = DateTime.Now.ToShortTimeString();
            list.Add(newObj);
        };
        Button edit = new Button();
        edit.Text = "Edit";
        edit.Dock = DockStyle.Bottom;
        edit.Click += delegate
        {
            if (list.Count > 0)
            {
                list[0].Message = "Boo!";
                list[0].IsRead = !list[0].IsRead;
            }
        };
        Form form = new Form();
        form.Controls.Add(grid);
        form.Controls.Add(add);
        form.Controls.Add(edit);
        Application.Run(form);
    }
Marc Gravell
Thnx alot, that solved the issue with the DataViewGrid not updating.If i could just figure out how to display the full text in a multiline manner per row, instead of a width scrollbar i would be enjoying my weekend just about now.
brokencoding