Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display datagridview rows and items in designtime

I would like to display DataGridView rows during designmode/designtime. Is there a way to achieve this?

I have tried using this function: datagridview1.Rows.Add(); in the control's constructor but the rows and their items are displayed only in runtime.

The reason why i need to display rows and their items in designtime is that i am customizing the appearance of the datagridview control. So each time i make changes like changing the AlternatingRowColor, RowFont, etc i will have to run the control to see the changes.

This eats up time.

If i could display all the rows in designtime/designmode then i would be able to see changes immediately as i make them which will save much time.

I am customizing the appearance of the Datagridview Control in Windows Forms and I'm using C#.

I am not defining the control in code but rather have dragged-n-dropped the control from the Controls Tab and onto a Winform.

Thanks.

like image 963
Russell Chidhakwa Avatar asked Oct 07 '19 07:10

Russell Chidhakwa


2 Answers

I'd add a designer verb to the context menu of the DataGridView and assign it by desired behavior. For example (as a start point to preview with dummy data):

using System;
using System.ComponentModel.Design;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.Design;
public class MyDataGridView : DataGridView
{
    private IDesignerHost designerHost;
    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        if (DesignMode && Site != null)
        {
            designerHost = Site.GetService(typeof(IDesignerHost)) as IDesignerHost;
            var designer = (ControlDesigner)designerHost?.GetDesigner(this);
            designer?.Verbs?.Add(new DesignerVerb("Preview with dummy data", (o, a) =>
            {
                //Some logic to add dummy rows, just for example
                this.Rows.Clear();
                if (Columns.Count > 0)
                {
                    var values = Columns.Cast<DataGridViewColumn>()
                        .Select(x => GetDummyData(x)).ToArray();
                    for (int i = 0; i < 2; i++)
                        Rows.Add(values);
                }
            }));
            designer?.Verbs?.Add(new DesignerVerb("Clear data", (o, a) =>
            {
                this.Rows.Clear();
            }));
        }
    }
    private object GetDummyData(DataGridViewColumn column)
    {
        //You can put some logic to generate dummy data based on column type, etc.
        return "Sample";
    }
}

Then as a result, you will see two menu items added to the context menu and by clicking on "Preview with dummy data", you will see two dummy rows added to the control at design time:

enter image description here

like image 188
Reza Aghaei Avatar answered Oct 10 '22 19:10

Reza Aghaei


I think it's not possible in WinForms DataGridView, only in WPF. Maybe you can achieve this by extending it somehow, but it's probably not easy and doesn't worth it.

https://social.msdn.microsoft.com/Forums/windows/en-US/bc6c84c1-0a3a-4c97-8966-30be371576d9/how-to-add-some-row-to-datagridview-control-at-design-time?forum=winforms

Adding rows to the DataGridView in design time is not supported. Based on my understanding, this is because we often bind a data source to the DataGridView to generate rows, not add them directly.

like image 1
Alex P. Avatar answered Oct 10 '22 19:10

Alex P.