How to Create a Dynamic DataGridView with Dynamic Cell Types in a Single Column

I am in the process of making a application for sending SOAP requests. Each request has a set of arguments or a lack of. Some arguments are numbers 0 to 100 or similar, some arguments can only be “Enabled” or “Disabled”, and some arguments are simply put to be a string of anykind.

The application will have you select a “Action” and this action will pull the arguments needed for the user. I want a DataGridView to show in column 1 the Argument Name and column 2 should be the value for the Argument.

However, in a goal to make this look nice, I wanted to see if there is a way to have some Combobox cells for the arguments that can only take specific values, and Textbox cells for the arguments that are meant to be a simple string.

I have tried including a variable in my argument object to define what kind of cell that row should be, then used CellFormatting (event) to catch when the cell is being drawn or made and format it with a specific type of cell. However, when I do this I get an error that seems like when it goes to paint the cell it throws a

An unhandled exception of type ‘System.NullReferenceException’ occurred in System.Windows.Forms.dll
Object reference not set to an instance of an object.

Below is my code
My datagrid has a source that looks like List<SOAPActionDataGridArgument> DataGridArgs

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>private void ArgumentDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 1)
{
if (ArgumentDataGridView.Rows[e.RowIndex].DataBoundItem is SOAPActionDataGridArgument item && item != null)
{
DataGridViewCell cell = ArgumentDataGridView[e.ColumnIndex, e.RowIndex];
if (cell != null)
{
switch (item.ControlType)
{
case "Combo1":
if (!(cell is DataGridViewComboBoxCell))
{
var comboCell = new DataGridViewComboBoxCell();
if (item.Variable?.AllowedValues != null)
{
comboCell.DataSource = item.Variable.AllowedValues;
ArgumentDataGridView[e.ColumnIndex, e.RowIndex] = comboCell;
}
}
break;
case "Combo2":
if (!(cell is DataGridViewComboBoxCell))
{
var comboCell = new DataGridViewComboBoxCell();
if (item.Variable?.AllowedValueRange != null)
{
int min = item.Variable.AllowedValueRange.Minimum;
int max = item.Variable.AllowedValueRange.Maximum;
comboCell.DataSource = Enumerable.Range(min, max - min + 1).Select(i => i.ToString()).ToArray();
ArgumentDataGridView[e.ColumnIndex, e.RowIndex] = comboCell;
}
}
break;
default:
if (!(cell is DataGridViewTextBoxCell))
{
ArgumentDataGridView[e.ColumnIndex, e.RowIndex] = new DataGridViewTextBoxCell();
}
break;
}
}
}
}
}
</code>
<code>private void ArgumentDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.ColumnIndex == 1) { if (ArgumentDataGridView.Rows[e.RowIndex].DataBoundItem is SOAPActionDataGridArgument item && item != null) { DataGridViewCell cell = ArgumentDataGridView[e.ColumnIndex, e.RowIndex]; if (cell != null) { switch (item.ControlType) { case "Combo1": if (!(cell is DataGridViewComboBoxCell)) { var comboCell = new DataGridViewComboBoxCell(); if (item.Variable?.AllowedValues != null) { comboCell.DataSource = item.Variable.AllowedValues; ArgumentDataGridView[e.ColumnIndex, e.RowIndex] = comboCell; } } break; case "Combo2": if (!(cell is DataGridViewComboBoxCell)) { var comboCell = new DataGridViewComboBoxCell(); if (item.Variable?.AllowedValueRange != null) { int min = item.Variable.AllowedValueRange.Minimum; int max = item.Variable.AllowedValueRange.Maximum; comboCell.DataSource = Enumerable.Range(min, max - min + 1).Select(i => i.ToString()).ToArray(); ArgumentDataGridView[e.ColumnIndex, e.RowIndex] = comboCell; } } break; default: if (!(cell is DataGridViewTextBoxCell)) { ArgumentDataGridView[e.ColumnIndex, e.RowIndex] = new DataGridViewTextBoxCell(); } break; } } } } } </code>
private void ArgumentDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 1)
    {
        if (ArgumentDataGridView.Rows[e.RowIndex].DataBoundItem is SOAPActionDataGridArgument item && item != null)
        {
            DataGridViewCell cell = ArgumentDataGridView[e.ColumnIndex, e.RowIndex];
            if (cell != null)
            {
                switch (item.ControlType)
                {
                    case "Combo1":
                        if (!(cell is DataGridViewComboBoxCell))
                        {
                            var comboCell = new DataGridViewComboBoxCell();
                            if (item.Variable?.AllowedValues != null)
                            {
                                comboCell.DataSource = item.Variable.AllowedValues;
                                ArgumentDataGridView[e.ColumnIndex, e.RowIndex] = comboCell;
                            }
                        }
                        break;
                    case "Combo2":
                        if (!(cell is DataGridViewComboBoxCell))
                        {
                            var comboCell = new DataGridViewComboBoxCell();
                            if (item.Variable?.AllowedValueRange != null)
                            {
                                int min = item.Variable.AllowedValueRange.Minimum;
                                int max = item.Variable.AllowedValueRange.Maximum;
                                comboCell.DataSource = Enumerable.Range(min, max - min + 1).Select(i => i.ToString()).ToArray();
                                ArgumentDataGridView[e.ColumnIndex, e.RowIndex] = comboCell;
                            }
                        }
                        break;
                    default:
                        if (!(cell is DataGridViewTextBoxCell))
                        {
                            ArgumentDataGridView[e.ColumnIndex, e.RowIndex] = new DataGridViewTextBoxCell();
                        }
                        break;
                }
            }
        }
    }
}

My Objects being used (Models) whatever u wanna call it.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class SOAPActionObject
{
public string Name { get; set; }
public List<SOAPActionArgument>? Arguments { get; set; }
public override string ToString()
{
return this.Name;
}
}
public class SOAPActionArgument
{
public string Name { get; set; }
public string Direction { get; set; }
public string RelatedStateVariable { get; set; }
public override string ToString()
{
return this.Name;
}
}
public class SOAPActionDataGridArgument
{
public SOAPActionArgument Argument { get; set; }
public string Value { get; set; }
public string ControlType { get; set; }
public SOAPActionStateVariable Variable { get; set; }
public SOAPActionDataGridArgument(SOAPActionArgument argument)
{
Argument = argument;
Value = "";
ControlType = "Text"; //Default
}
public SOAPActionDataGridArgument(SOAPActionArgument argument, string controlType, SOAPActionStateVariable variable)
{
Argument = argument;
Value = "";
ControlType = controlType;
Variable = variable;
}
}
public class SOAPActionStateVariable
{
public string Name { get; set; }
public string DataType { get; set; }
public ValueRange? AllowedValueRange { get; set; }
public List<string>? AllowedValues { get; set; }
}
public class ValueRange
{
public int Minimum { get; set; }
public int Maximum { get; set; }
}
</code>
<code>public class SOAPActionObject { public string Name { get; set; } public List<SOAPActionArgument>? Arguments { get; set; } public override string ToString() { return this.Name; } } public class SOAPActionArgument { public string Name { get; set; } public string Direction { get; set; } public string RelatedStateVariable { get; set; } public override string ToString() { return this.Name; } } public class SOAPActionDataGridArgument { public SOAPActionArgument Argument { get; set; } public string Value { get; set; } public string ControlType { get; set; } public SOAPActionStateVariable Variable { get; set; } public SOAPActionDataGridArgument(SOAPActionArgument argument) { Argument = argument; Value = ""; ControlType = "Text"; //Default } public SOAPActionDataGridArgument(SOAPActionArgument argument, string controlType, SOAPActionStateVariable variable) { Argument = argument; Value = ""; ControlType = controlType; Variable = variable; } } public class SOAPActionStateVariable { public string Name { get; set; } public string DataType { get; set; } public ValueRange? AllowedValueRange { get; set; } public List<string>? AllowedValues { get; set; } } public class ValueRange { public int Minimum { get; set; } public int Maximum { get; set; } } </code>
public class SOAPActionObject
{
    public string Name { get; set; }
    public List<SOAPActionArgument>? Arguments { get; set; }

    public override string ToString()
    {
        return this.Name;
    }
}
public class SOAPActionArgument
{
    public string Name { get; set; }
    public string Direction { get; set; }
    public string RelatedStateVariable { get; set; }

    public override string ToString()
    {
        return this.Name;
    }
}

public class SOAPActionDataGridArgument
{
    public SOAPActionArgument Argument { get; set; } 
    public string Value { get; set; } 
    public string ControlType { get; set; }
    public SOAPActionStateVariable Variable { get; set; }

    public SOAPActionDataGridArgument(SOAPActionArgument argument)
    {
        Argument = argument;
        Value = "";
        ControlType = "Text"; //Default
    }
    public SOAPActionDataGridArgument(SOAPActionArgument argument, string controlType, SOAPActionStateVariable variable)
    {
        Argument = argument;
        Value = "";
        ControlType = controlType;
        Variable = variable;
    }
}

public class SOAPActionStateVariable
{
    public string Name { get; set; }
    public string DataType { get; set; }
    public ValueRange? AllowedValueRange { get; set; }
    public List<string>? AllowedValues { get; set; }
}
public class ValueRange
{
    public int Minimum { get; set; }
    public int Maximum { get; set; }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật