Cara menambahkan baris baru ke datagridview pemrograman

jika add-turut untuk DataTable

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

Bagaimana tentang DataGridView??

Mengomentari pertanyaan (1)
Larutan

Yang dapat anda lakukan:

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

atau:

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

Cara lain:

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

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

Komentar (6)

Seperti ini:

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

Katakanlah anda memiliki sebuah datagridview yang tidak terikat ke dataset dan anda ingin pemrograman mengisi baris baru...

Berikut ini's bagaimana anda melakukannya.

// 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)
Komentar (1)

Seperti ini:

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

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

Atau anda perlu mengatur ada nilai-nilai secara individual menggunakan propery .Baris(), seperti ini:

 dataGridView1.Rows[1].Cells[0].Value = "cell value";
Komentar (4)

Menambahkan baris baru di DGV dengan tidak ada baris dengan Add() meningkatkan SelectionChanged acara sebelum anda dapat memasukkan setiap data (atau mengikat sebuah benda di Tag properti).

Membuat tiruan-turut dari RowTemplate lebih aman 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);
Komentar (3)

Jika grid terikat terhadap DataSet / tabel yang lebih baik untuk menggunakan BindingSource seperti

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

//Add data to dataTable and then call

bindingSource.ResetBindings(false)    
Komentar (0)

Ini adalah bagaimana saya menambahkan baris jika dgrview kosong: (myDataGridView memiliki dua kolom dalam contoh saya)

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

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

myDataGridView.Rows.Add(row);

Menurut dokumen: "CreateCells() membersihkan sel-sel yang ada dan menetapkan mereka template sesuai dengan yang disediakan DataGridView template".

Komentar (2)

berikut ini adalah cara lain untuk melakukan seperti

 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"); 
    }
Komentar (0)

Jika anda perlu untuk memanipulasi apa-apa selain dari Sel Nilai string seperti menambahkan Tag, coba ini:

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);
Komentar (0)
yourDGV.Rows.Add(column1,column2...columnx); //add a row to a dataGridview
yourDGV.Rows[rowindex].Cells[Cell/Columnindex].value = yourvalue; //edit the value

anda juga dapat membuat baris baru dan kemudian menambahkannya ke DataGridView seperti ini:

DataGridViewRow row = new DataGridViewRow();
row.Cells[Cell/Columnindex].Value = yourvalue;
yourDGV.Rows.Add(row);
Komentar (0)

Jika anda mengikat sebuah Daftar

List student = new List();

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

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

Jika anda mengikat DataTable

DataTable table = new DataTable();

 DataRow newRow = table.NewRow();

// Add the row to the rows collection.
table.Rows.Add(newRow);
Komentar (0)
//header
dataGridView1.RowCount = 50;
dataGridView1.Rows[0].HeaderCell.Value = "Product_ID0";

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

Jika anda´ve sudah didefinisikan sumber data, Anda bisa mendapatkan DataGridView´s sumber data dan melemparkan itu sebagai Datatable.

Kemudian tambahkan baru DataRow dan menetapkan nilai-Nilai Bidang.

Tambahkan baris baru untuk DataTable dan Menerima perubahan.

Dalam C# ini akan menjadi sesuatu seperti ini..

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

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

dataTable.Rows.Add(drToAdd);
dataTable.AcceptChanges();
Komentar (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; 
Komentar (1)

Contoh copy-turut dari dataGridView dan menambahkan baris baru dalam sama 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;
Komentar (0)
string[] splited = t.Split('>');
int index = dgv_customers.Rows.Add(new DataGridViewRow());
dgv_customers.Rows[index].Cells["cust_id"].Value=splited.WhichIsType("id;");

Tapi perlu diketahui, WhichIsType adalah metode penyuluhan yang saya buat.

Komentar (2)

Pertimbangkan Aplikasi Windows dan menggunakan Tombol Klik Event menempatkan kode ini di dalamnya.

dataGridView1.Rows
                .Add(new object[] { textBox1.Text, textBox2.Text, textBox3.Text });
Komentar (0)