Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Any one know how to generate grid when columns are not fixed means we do not have knowledge of fields in our table through MVC, Please help me in same
Posted

1 solution

Could you please be more specific?
If you're referring to how to add columns to a DataGridView where you don't know what the columns would be, it would look something like this;

C#
private void Form1_Load(object sender, EventArgs e)
{
    try
    {
        DataGridViewColumn column;
        DataGridViewCell cell = new DataGridViewTextBoxCell();
        for (int i = 0; i < 5; i++)
        {
            column = new DataGridViewColumn();
            column.Name = "column" + (i+1).ToString();
            column.HeaderText = "Column " + (i+1).ToString();
            column.ValueType = typeof(string);
            
            column.CellTemplate = cell;

            dataGridView1.Columns.Add(column);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}


NOTE: If you're going to be adding columns dynamically, make sure the column doesn't already exist before trying to add it.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900