Add row to grid view

前端 未结 5 1984
予麋鹿
予麋鹿 2021-02-14 07:57

Is it possible to programmatically add a row to a GridView in C# ASP?

If yes, how ?

I want to add static data directly from the code, not from an array nor an da

5条回答
  •  北恋
    北恋 (楼主)
    2021-02-14 08:40

    Rows can be inserted on GridView_RowCreated, Like

    protected void gvItems_RowCreated(object sender, GridViewRowEventArgs e)
    {                    
        GridViewRow NewHeader = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
        NewHeader.Font.Bold = true;
        NewHeader.CssClass = "heading";
    
        //Item#
        TableCell NewHeaderCell = new TableCell();
        NewHeaderCell.Text = "#";
        NewHeaderCell.HorizontalAlign = HorizontalAlign.Left;
        NewHeader.Cells.Add(NewHeaderCell);
    
        //Item#
        NewHeaderCell = new TableCell();
        NewHeaderCell.Text = "Item#";
        NewHeaderCell.HorizontalAlign = HorizontalAlign.Left;
        NewHeader.Cells.Add(NewHeaderCell);
    
        //Amount
        NewHeaderCell = new TableCell();
        NewHeaderCell.Text = "Amount";
        NewHeaderCell.HorizontalAlign = HorizontalAlign.Right;
        NewHeader.Cells.Add(NewHeaderCell);
        GridView1.Controls[0].Controls.AddAt(e.Row.RowIndex + 
            rowIndex, NewHeader);
    }
    

提交回复
热议问题