C# 将行添加到网格视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19257451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:31:27  来源:igfitidea点击:

Add row to grid view

c#asp.netgridview

提问by GmodCake

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

是否可以以编程方式向 C# ASP 中的 GridView 添加一行?

If yes, how ?

如果是,如何?

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

我想直接从代码中添加静态数据,而不是从数组或数据源

采纳答案by christiandev

DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
dr["Column1"] = string.Empty;
dt.Rows.Add(dr);

You can then bind your GridViewto the DataTable...

然后,您可以将您GridViewDataTable...

gv.DataSource = dt;
gv.DataBind();

回答by Dimo

I still recommend that you use binding/datasource, but of course you don't have to. The following should do what you want:

我仍然建议您使用绑定/数据源,但您当然不必这样做。以下应该做你想做的:

        DataGridViewTextBoxColumn columntype = new DataGridViewTextBoxColumn();
        columntype.HeaderText = "Type";
        columntype.Width = 80;
        dataGridView1.Columns.Add(columntype);

回答by Yuriy Galanter

GridViewRowsCollectiondoesn't have .Addmethod, so you can't do it directly to the GridView.

GridViewRowsCollection没有.Add方法,所以你不能直接对 GridView 做。

There're alternatives. For example if you bind it to a DataTable - you can add custom row with data to the DataTable.

有替代品。例如,如果您将其绑定到 DataTable - 您可以将带有数据的自定义行添加到 DataTable。

Another alternative - do it on client by adding a row to rendered HTML Table.

另一种选择 - 通过向呈现的 HTML 表添加一行在客户端上执行此操作。

回答by cvetyab

dataGridView1.Columns[0].Name = "column1";
dataGridView1.Columns[1].Name = "column2";

string[] row1 = new string[] { "column1 value", "column2 value" };
dataGridView1.Rows.Add(row1);

回答by Akshay Mishra

Rows can be inserted on GridView_RowCreated, Like

可以插入行GridView_RowCreated,例如

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);
}