172

行を追加する場合DataTable

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

どうDataGridViewですか?

4

19 に答える 19

278

できるよ:

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

于 2012-04-08T15:07:44.330 に答える
59

このような:

var index = dgv.Rows.Add();
dgv.Rows[index].Cells["Column1"].Value = "Column1";
dgv.Rows[index].Cells["Column2"].Value = 5.6;
//....
于 2013-11-14T04:51:31.997 に答える
46

データセットにバインドされていない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)
于 2016-01-01T23:45:43.943 に答える
34

このような:

 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";
于 2012-04-08T15:05:20.643 に答える
27

Add()を使用して行のないDGVに新しい行を追加すると、データを挿入する(またはTagプロパティでオブジェクトをバインドする)前に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);
于 2013-09-26T15:13:33.647 に答える
14

これは、dgrviewが空の場合に行を追加する方法です:(myDataGridViewには私の例では2つの列があります)

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テンプレートに従ってそれらのテンプレートを設定します」。

于 2017-02-28T23:13:31.563 に答える
8

グリッドがDataSet/テーブルに対してバインドされている場合は、次のようなBindingSourceを使用することをお勧めします。

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

//Add data to dataTable and then call

bindingSource.ResetBindings(false)    
于 2014-01-17T10:03:10.583 に答える
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"); 
    }
于 2017-08-08T07:45:47.467 に答える
5

タグの追加など、セル値文字列以外のものを操作する必要がある場合は、次のことを試してください。

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);
于 2015-01-20T17:49:37.847 に答える
5

リストをバインドしている場合

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);
于 2019-03-31T10:55:33.333 に答える
3
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);
于 2017-08-07T07:56:48.243 に答える
1

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;
于 2015-10-17T02:45:16.960 に答える
1

Windowsアプリケーションについて考えてみます。ボタンクリックイベントを使用して、このコードをその中に入れます。

dataGridView1.Rows
                .Add(new object[] { textBox1.Text, textBox2.Text, textBox3.Text });
于 2016-01-15T18:02:17.657 に答える
1
//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; 
于 2016-02-02T14:59:42.090 に答える
1

をすでに定義している場合は、をDataSource取得して、としてキャストできます。DataGridViewDataSourceDatatable

次に、新しいものを追加しDataRow、フィールド値を設定します。

に新しい行を追加しDataTable、変更を受け入れます。

C#では次のようになります。

DataTable dataTable = (DataTable)dataGridView.DataSource;
DataRow drToAdd = dataTable.NewRow();

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

dataTable.Rows.Add(drToAdd);
dataTable.AcceptChanges();
于 2016-02-03T16:26:49.463 に答える
1
//header
dataGridView1.RowCount = 50;
dataGridView1.Rows[0].HeaderCell.Value = "Product_ID0";


//add row by cell 
 dataGridView1.Rows[1].Cells[0].Value = "cell value";
于 2016-03-25T15:32:40.407 に答える
1

グリッドビューのソースとしてDataTableを追加したい場合は、-

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;
于 2020-11-02T16:17:10.947 に答える
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;
于 2020-11-28T04:20:37.913 に答える
-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ただし、これは私が作成した拡張メソッドであることに注意してください。

于 2017-01-02T17:55:18.407 に答える