How to display datagridview rows and items in designtime
Asked Answered
S

3

6

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.

Submediant answered 7/10, 2019 at 7:26 Comment(1)
check this out #18628373Fitzwater
L
4

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

Ludovico answered 7/10, 2019 at 16:19 Comment(5)
Aghael: I will try the solution you presented. When i was reading your code it made senseSubmediant
No problem, the screenshot has taken from the same MyDataGridView class at design-time. By the way, you can call me Reza. In fact Aghaei is my last name ;)Ludovico
I have tried your solution and it addresses exactly my problem. I really appreciate. Now i can customize the DataGridView without having to run it each time.Submediant
Cool! That's great! Its good enough to start. You can enhance the logic of generating dummy data later.Ludovico
This solution will help many who desire to customize the DataGridView control in designmode. That's great!Submediant
K
1

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.

Kendrakendrah answered 7/10, 2019 at 7:35 Comment(0)
A
0

As pointed out in the answer of Alex P., it is not possible. With a bit of experience, you'll be able to foretell the appearance.

If you have a lot of visual designing (i.e. due to your customer's detailed requirements), you can also design a little test application, where you can have i.e. a color list and font picker and apply selected properties to the DataGridView properties like DefaultCellStyle.BackColor, .ForeColor, Font, etc. at runtime. Simply using button or .SelectionChanged event for each property. You could probably find something finished on CodeProject or Github (this is related).

Aerostatics answered 7/10, 2019 at 8:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.