Een nieuwe rij programmatisch toevoegen aan datagridview

indien rij toevoegen aan DataTable

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

Hoe zit het met DataGridView??


Antwoord 1, autoriteit 100%

U kunt het volgende doen:

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

of:

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

Andere manier:

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

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


Antwoord 2, autoriteit 20%

Zoals dit:

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

Antwoord 3, autoriteit 16%

Stel dat je een datagridview hebt die niet aan een dataset is gebonden en dat je programmatisch nieuwe rijen wilt vullen…

Zo doe je het.

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

Antwoord 4, Autoriteit 13%

Zoals dit:

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

Of u moet daar waarden instellen, gebruikt u de prophy .Rows(), zoals deze:

dataGridView1.Rows[1].Cells[0].Value = "cell value";

Antwoord 5, Autoriteit 10%

Een nieuwe rij toevoegen in een DGV zonder rijen met Toevoegen () verhoogt selectiechanged evenement voordat u gegevens (of bind een object in tag-eigenschap) in te voegen.

Maak een klonenrij van rowtemplate is veiliger 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);

Antwoord 6, Autoriteit 4%

Dit is hoe ik een rij voeg als de DGR-View leeg is: (MyDatagridView heeft twee kolommen in mijn voorbeeld)

DataGridViewRow row = new DataGridViewRow();
row.CreateCells(myDataGridView);
row.Cells[0].Value = "some value";
row.Cells[1].Value = "next columns value";
myDataGridView.Rows.Add(row);

Volgens documenten: “CreateCells() wist de bestaande cellen en stelt hun sjabloon in volgens de meegeleverde DataGridView-sjabloon”.


Antwoord 7, autoriteit 3%

Als het raster is gebonden aan een DataSet / tabel, is het beter om een BindingSource-achtige te gebruiken

var bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
grid.DataSource = bindingSource;
//Add data to dataTable and then call
bindingSource.ResetBindings(false)    

Antwoord 8, autoriteit 3%

hier is een andere manier om dit te doen

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

Antwoord 9, autoriteit 2%

Als je iets anders dan de celwaarde-tekenreeks moet manipuleren, zoals het toevoegen van een tag, probeer dan dit:

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

Antwoord 10, Autoriteit 2%

Als u een lijst bindt

List<Student> student = new List<Student>();
dataGridView1.DataSource = student.ToList();
student .Add(new Student());
//Reset the Datasource
dataGridView1.DataSource = null;
dataGridView1.DataSource = student;

Als u bindend DataTable

bent

DataTable table = new DataTable();
 DataRow newRow = table.NewRow();
// Add the row to the rows collection.
table.Rows.Add(newRow);

Antwoord 11

yourDGV.Rows.Add(column1,column2...columnx); //add a row to a dataGridview
yourDGV.Rows[rowindex].Cells[Cell/Columnindex].value = yourvalue; //edit the value

U kunt ook een nieuwe rij maken en vervolgens toevoegen aan de DataGridView als volgt:

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

Antwoord 12

Een voorbeeld van kopieerrij van datagridview en voegt een nieuwe rij toe in dezelfde 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;

Antwoord 13

Overweeg een Windows-toepassing en het gebruik van de knop Klik op deze code in.

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

Antwoord 14

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

Antwoord 15

//header
dataGridView1.RowCount = 50;
dataGridView1.Rows[0].HeaderCell.Value = "Product_ID0";
//add row by cell 
 dataGridView1.Rows[1].Cells[0].Value = "cell value";

Antwoord 16

Als u al een DataSourcehebt gedefinieerd, kunt u de DataGridView‘s DataSourcekrijgen en deze als een Datatable.

Voeg vervolgens een nieuwe DataRowtoe en stel de velden waarden in.

Voeg de nieuwe rij toe aan de DataTableen accepteer de wijzigingen.

In C # zou het zoiets zijn als deze ..

DataTable dataTable = (DataTable)dataGridView.DataSource;
DataRow drToAdd = dataTable.NewRow();
drToAdd["Field1"] = "Value1";
drToAdd["Field2"] = "Value2";
dataTable.Rows.Add(drToAdd);
dataTable.AcceptChanges();

Antwoord 17

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

Maar wees op de hoogte, WhichIsTypeis de extensie-methode die ik heb gemaakt.


Antwoord 18

Als iemand dat datum wil toevoegen als een bron van gridview dan –

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;

Antwoord 19

Ik heb dit bruikbaar meer dan eens gevonden wanneer het datagrid is gebonden aan een tafel.

       DataTable dt = (DataTable)dgvData.DataSource;
        DataRow row = dt.NewRow();
        foreach (var something in something)
        {
           row["ColumnName"] = something ;               
        }
        dt.Rows.Add(row);
        dgvData.DataSource = dt;

Other episodes