Search code examples
c#asp.netgridviewrowheader

How to display RowHeader in a GridView at runtime in C# Asp.Net?


I am new to asp.net, Actually I am designing a web page which is similar to my Windows Form. my windows form consists of a DataGridView which consists of 12 columns and 8 rows at any time. I am showing my windows form below enter image description here

How can i do this in web page. In windows I am adding 8 rows programmatically to the DatagridView like

dgv.Rows.Add(8);
foreach (DataGridViewRow dgR in dgv.Rows)
        {
            dgR.HeaderCell.Value = Convert.ToChar(dgR.Index + 65).ToString();
        }

but i don't know how to write in Web. Can anybody please help regarding this.


Solution

  • It's a little bit more overhead in asp.net webforms (guess that´s what you are using). Best way would be to create a DataTable. Here is a little example (untested though)

    var dt = new DataTable();
    
    //create columns
    for(int i = 1; i <= 12; i++){
        dt.Columns.Add(new DataColumn(i.ToString(CultureInfo.InvariantCulture), typeof(string)));
    }
    
    //create rows
    for(int i = 0; i < 8; i++){
        var newRow = dt.NewRow();
        for(int j = 1; j <= 12; j++){
            newRow[j.ToString(CultureInfo.InvariantCulture)] = string.Empty;
        }
        dt.Rows.Add(newRow);
    }
    
    myGridView.DataSource = dt;
    myGridView.DataBind();
    

    I hope that helps: