如果将行添加到DataTable
DataRow row = datatable1.NewRow();
row["column2"]="column2";
row["column6"]="column6";
datatable1.Rows.Add(row);
DataGridView
??
答案 0 :(得分:227)
你可以这样做:
DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells[0].Value = "XYZ";
row.Cells[1].Value = 50.2;
yourDataGridView.Rows.Add(row);
或:
DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells["Column2"].Value = "XYZ";
row.Cells["Column6"].Value = 50.2;
yourDataGridView.Rows.Add(row);
另一种方式:
this.dataGridView1.Rows.Add("five", "six", "seven","eight");
this.dataGridView1.Rows.Insert(0, "one", "two", "three", "four");
来自:http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx
答案 1 :(得分:41)
像这样:
var index = dgv.Rows.Add();
dgv.Rows[index].Cells["Column1"].Value = "Column1";
dgv.Rows[index].Cells["Column2"].Value = 5.6;
//....
答案 2 :(得分:32)
像这样:
dataGridView1.Columns[0].Name = "column2";
dataGridView1.Columns[1].Name = "column6";
string[] row1 = new string[] { "column2 value", "column6 value" };
dataGridView1.Rows.Add(row1);
或者您需要单独设置值,使用属性.Rows()
,如下所示:
dataGridView1.Rows[1].Cells[0].Value = "cell value";
答案 3 :(得分:31)
假设您有一个未绑定到数据集的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)
答案 4 :(得分:19)
在没有添加()的行的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);
答案 5 :(得分:6)
如果网格绑定到DataSet /表,最好使用像
这样的BindingSourcevar bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
grid.DataSource = bindingSource;
//Add data to dataTable and then call
bindingSource.ResetBindings(false)
答案 6 :(得分:5)
如果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);
根据文档:" CreateCells()清除现有单元格并根据提供的DataGridView模板设置其模板"。
答案 7 :(得分:4)
这是另一种方法
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");
}
答案 8 :(得分:3)
如果您需要操作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);
答案 9 :(得分:2)
yourDGV.Rows.Add(column1,column2...columnx); //add a row to a dataGridview
yourDGV.Rows[rowindex].Cells[Cell/Columnindex].value = yourvalue; //edit the value
您还可以创建一个新行,然后将其添加到DataGridView中,如下所示:
DataGridViewRow row = new DataGridViewRow();
row.Cells[Cell/Columnindex].Value = yourvalue;
yourDGV.Rows.Add(row);
答案 10 :(得分:1)
如果您要绑定列表
List<Student> student = new List<Student>();
dataGridView1.DataSource = student.ToList();
student .Add(new Student());
//Reset the Datasource
dataGridView1.DataSource = null;
dataGridView1.DataSource = student;
如果要绑定数据表
DataTable table = new DataTable();
DataRow newRow = table.NewRow();
// Add the row to the rows collection.
table.Rows.Add(newRow);
答案 11 :(得分:0)
当DataGrid绑定到表时,我发现此功能不止一次。
DataTable dt = (DataTable)dgvData.DataSource;
DataRow row = dt.NewRow();
foreach (var something in something)
{
row["ColumnName"] = something ;
}
dt.Rows.Add(row);
dgvData.DataSource = dt;
答案 12 :(得分:0)
如果有人想将DataTable添加为gridview的源,那么-
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("column1"));
dt.Columns.Add(new DataColumn("column2"));
DataRow dr = dt.NewRow();
dr[0] = "column1 Value";
dr[1] = "column2 Value";
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
答案 13 :(得分:0)
//header
dataGridView1.RowCount = 50;
dataGridView1.Rows[0].HeaderCell.Value = "Product_ID0";
//add row by cell
dataGridView1.Rows[1].Cells[0].Value = "cell value";
答案 14 :(得分:0)
如果您已经定义了DataSource
,则可以获取DataGridView
的{{1}}并将其转换为DataSource
。
然后添加新的Datatable
并设置字段值。
将新行添加到DataRow
并接受更改。
在C#中它会是这样的......
DataTable
答案 15 :(得分:0)
//Add a list of BBDD
var item = myEntities.getList().ToList();
//Insert a new object of type in a position of the list
item.Insert(0,(new Model.getList_Result { id = 0, name = "Coca Cola" }));
//List assigned to DataGridView
dgList.DataSource = item;
答案 16 :(得分:0)
来自dataGridView的复制行示例,并在同一dataGridView中添加了一个新行:
DataTable Dt = new DataTable();
Dt.Columns.Add("Column1");
Dt.Columns.Add("Column2");
DataRow dr = Dt.NewRow();
DataGridViewRow dgvR = (DataGridViewRow)dataGridView1.CurrentRow;
dr[0] = dgvR.Cells[0].Value;
dr[1] = dgvR.Cells[1].Value;
Dt.Rows.Add(dR);
dataGridView1.DataSource = Dt;
答案 17 :(得分:-1)
string[] splited = t.Split('>');
int index = dgv_customers.Rows.Add(new DataGridViewRow());
dgv_customers.Rows[index].Cells["cust_id"].Value=splited.WhichIsType("id;");
但请注意,WhichIsType
是我创建的扩展方法。
答案 18 :(得分:-1)
考虑使用Windows应用程序并使用Button Click Event将此代码放入其中。
dataGridView1.Rows
.Add(new object[] { textBox1.Text, textBox2.Text, textBox3.Text });