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
小智 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)
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)
如果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模板设置其模板".
如果网格绑定到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)
如果您需要操作除 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)
如果你正在绑定一个 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)
归档时间: |
|
查看次数: |
717475 次 |
最近记录: |