如何添加新字符串 datagridview 软件

如果添加字符串
DataTable



DataRow row = datatable1.NewRow//;
row["column2"]="column2";
row["column6"]="column6";
datatable1.Rows.Add/row/;


关于什么
DataGridView

??
已邀请:

裸奔

赞同来自:

你能做到吗:


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-u ... .aspx

三叔

赞同来自:

类似于这个:


var index = dgv.Rows.Add//;
dgv.Rows[index].Cells["Column1"].Value = "Column1";
dgv.Rows[index].Cells["Column2"].Value = 5.6;
//....

喜特乐

赞同来自:

假设你有 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/

八刀丁二

赞同来自:

类似于这个:


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";

三叔

赞同来自:

添加新行 DGV 没有线路

Add//

原因

事件 SelectionChanged 在插入任何数据之前 /或标签属性中的对象绑定/.

从中创建一个克隆字符串

RowTemplate

安全恕我直言:


//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/;

帅驴

赞同来自:

这就是我添加字符串的方式 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/;


根据 docs: "CreateCells// 清除现有单元格并按照提供的模板设置其模板 DataGridView".

知食

赞同来自:

如果网格绑定到 DataSet / 表,那么它更好 BindingSource 作为


var bindingSource = new BindingSource//;
bindingSource.DataSource = dataTable;
grid.DataSource = bindingSource;

//Add data to dataTable and then call

bindingSource.ResetBindings/false/

郭文康

赞同来自:

这是另一种做出的方法


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"/;
}

郭文康

赞同来自:

如果您需要操作任何操作,除了单元格值的行,例如添加标记,请尝试执行此操作:


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/;

快网

赞同来自:

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/;

知食

赞同来自:

如果你绑一个列表


List<student> student = new List<student>//;

dataGridView1.DataSource = student.ToList//;
student .Add/new Student///;

//Reset the Datasource
dataGridView1.DataSource = null;
dataGridView1.DataSource = student;


如果你被捆绑了 DataTable


DataTable table = new DataTable//;

DataRow newRow = table.NewRow//;

// Add the row to the rows collection.
table.Rows.Add/newRow/;


</student></student>

詹大官人

赞同来自:

复制字符串的示例 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;

三叔

赞同来自:

考虑应用程序 Windows 和这个活动 Button Click 将此代码放入其中。


dataGridView1.Rows
.Add/new object[] { textBox1.Text, textBox2.Text, textBox3.Text }/;

莫问

赞同来自:

//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;

快网

赞同来自:

如果您已经识别过 a
DataSource

, 你可以得到
DataGridView

s
DataSource

并带上它 A
Datatable

.

然后添加新
DataRow

并设置字段的值。

添加一个新字符串
DataTable

并接受变化。

在 C# 一年是如此..


DataTable dataTable = /DataTable/dataGridView.DataSource;
DataRow drToAdd = dataTable.NewRow//;

drToAdd["Field1"] = "Value1";
drToAdd["Field2"] = "Value2";

dataTable.Rows.Add/drToAdd/;
dataTable.AcceptChanges//;

卫东

赞同来自:

//header
dataGridView1.RowCount = 50;
dataGridView1.Rows[0].HeaderCell.Value = "Product_ID0";


//add row by cell
dataGridView1.Rows[1].Cells[0].Value = "cell value";

郭文康

赞同来自:

string[] splited = t.Split/'>'/;
int index = dgv_customers.Rows.Add/new DataGridViewRow///;
dgv_customers.Rows[index].Cells["cust_id"].Value=splited.WhichIsType/"id;"/;


但要记住
WhichIsType

- 这是我创建的扩展方法。

要回复问题请先登录注册