开发者

WinForms DataGridView - Full text display and dataSource update

Earlier today i w开发者_开发技巧as 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 http://img237.imageshack.us/img237/3015/datagridview.jpg

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


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


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).


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);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜