如何以编程方式向datagridview添加新行

LK *_*ung 144 c# datagridview row winforms

如果添加行 DataTable

DataRow row = datatable1.NewRow();
row["column2"]="column2";
row["column6"]="column6";
datatable1.Rows.Add(row);
Run Code Online (Sandbox Code Playgroud)

怎么样DataGridView

Hab*_*bib 234

你可以做:

DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells[0].Value = "XYZ";
row.Cells[1].Value = 50.2;
yourDataGridView.Rows.Add(row);
Run Code Online (Sandbox Code Playgroud)

要么:

DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells["Column2"].Value = "XYZ";
row.Cells["Column6"].Value = 50.2;
yourDataGridView.Rows.Add(row);
Run Code Online (Sandbox Code Playgroud)

其他方式:

this.dataGridView1.Rows.Add("five", "six", "seven","eight");
this.dataGridView1.Rows.Insert(0, "one", "two", "three", "four");
Run Code Online (Sandbox Code Playgroud)

来自:http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx

  • 谢谢,如果我的datagridview没有行,我该如何添加行? (21认同)
  • 在阅读了大约20个解决方案(由谷歌发现)之后,这是我理解的第一个解决方案. (7认同)
  • 您不能像以前那样直接引用列:row.Cells ["Column2"].Value ="XYZ"; ...你必须先查找索引:row.Cells [yourDataGridView.Columns ["Column2"].Index] .Value ="XYZ"; (6认同)
  • 理想情况下,您应该克隆“DataGridView”的“RowTemplate”。当“DataGridView”中的不同行具有不同的样式时,这将成为一个更大的问题。 (3认同)
  • @DavidYeung那么MGA的回答可以帮到你,你的数据来源是什么?它是一个数据表吗?如果是这样,您可以将行添加到数据表,然后刷新数据源 (2认同)

小智 43

像这样:

var index = dgv.Rows.Add();
dgv.Rows[index].Cells["Column1"].Value = "Column1";
dgv.Rows[index].Cells["Column2"].Value = 5.6;
//....
Run Code Online (Sandbox Code Playgroud)


小智 35

假设您有一个未绑定到数据集的datagridview,并且您希望以编程方式填充新行...

这是你如何做到的.

// Create a new row first as it will include the columns you've created at design-time.

int rowId = dataGridView1.Rows.Add();

// Grab the new row!
DataGridViewRow row = dataGridView1.Rows[rowId];

// Add the data
row.Cells["Column1"].Value = "Value1";
row.Cells["Column2"].Value = "Value2";

// And that's it! Quick and painless... :o)
Run Code Online (Sandbox Code Playgroud)

  • +1:这是唯一实际使用`datagridview.Columns.Add("columnname")`定义的列名的解决方案,不需要DataTable,以微笑结束 (5认同)

Mah*_*mal 32

像这样:

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

 string[] row1 = new string[] { "column2 value", "column6 value" };
 dataGridView1.Rows.Add(row1);
Run Code Online (Sandbox Code Playgroud)

或者您需要单独设置值,使用属性.Rows(),如下所示:

 dataGridView1.Rows[1].Cells[0].Value = "cell value";
Run Code Online (Sandbox Code Playgroud)


Def*_*on1 22

使用Add()在没有行的DGV中添加新行会引发SelectionChanged事件,然后才能插入任何数据(或在Tag属性中绑定对象).

RowTemplate创建一个克隆行是更安全的imho:

//assuming that you created columns (via code or designer) in myDGV
DataGridViewRow row = (DataGridViewRow) myDGV.RowTemplate.Clone();
row.CreateCells(myDGV, "cell1", "cell2", "cell3");

myDGV.Rows.Add(row);
Run Code Online (Sandbox Code Playgroud)

  • Markand:调用DataGridViewRow.CreateCells来初始化Cells集合. (5认同)
  • Clone()方法返回一行但没有Cell.row.Cells.Count为0. (3认同)

Sno*_*arg 8

如果dgrview为空,这就是我添加行的方式:( myDataGridView在我的示例中有两列)

DataGridViewRow row = new DataGridViewRow();
row.CreateCells(myDataGridView);

row.Cells[0].Value = "some value";
row.Cells[1].Value = "next columns value";

myDataGridView.Rows.Add(row);
Run Code Online (Sandbox Code Playgroud)

根据文档:"CreateCells()清除现有单元格并根据提供的DataGridView模板设置其模板".

  • 谢谢这对我非常有用,我错过了"row.CreateCells(myDataGridView);" (2认同)

And*_*ers 7

如果网格绑定到DataSet /表,最好使用类似BindingSource

var bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
grid.DataSource = bindingSource;

//Add data to dataTable and then call

bindingSource.ResetBindings(false)    
Run Code Online (Sandbox Code Playgroud)


小智 7

这是另一种方法来做到这一点

 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        dataGridView1.ColumnCount = 3;

        dataGridView1.Columns[0].Name = "Name";
        dataGridView1.Columns[1].Name = "Age";
        dataGridView1.Columns[2].Name = "City";

        dataGridView1.Rows.Add("kathir", "25", "salem");
        dataGridView1.Rows.Add("vino", "24", "attur");
        dataGridView1.Rows.Add("maruthi", "26", "dharmapuri");
        dataGridView1.Rows.Add("arun", "27", "chennai"); 
    }
Run Code Online (Sandbox Code Playgroud)


sil*_*one 5

如果您需要操作除 Cell Value 字符串之外的任何内容,例如添加标签,请尝试以下操作:

DataGridViewRow newRow = (DataGridViewRow)mappingDataGridView.RowTemplate.Clone();
newRow.CreateCells(mappingDataGridView);

newRow.Cells[0].Value = mapping.Key;
newRow.Cells[1].Value = ((BusinessObject)mapping.Value).Name;
newRow.Cells[1].Tag = mapping.Value;

mappingDataGridView.Rows.Add(newRow);
Run Code Online (Sandbox Code Playgroud)


Moh*_*ian 5

如果你正在绑定一个 List

List<Student> student = new List<Student>();

dataGridView1.DataSource = student.ToList();
student .Add(new Student());

//Reset the Datasource
dataGridView1.DataSource = null;
dataGridView1.DataSource = student;
Run Code Online (Sandbox Code Playgroud)

如果您正在绑定 DataTable

DataTable table = new DataTable();

 DataRow newRow = table.NewRow();

// Add the row to the rows collection.
table.Rows.Add(newRow);
Run Code Online (Sandbox Code Playgroud)