How to add a new row to datagridview programmatically

前端 未结 18 1733
滥情空心
滥情空心 2020-11-22 09:40

if add row to DataTable

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


        
相关标签:
18条回答
  • 2020-11-22 09:46

    If anyone wanted to Add DataTable as a source of gridview then--

    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;
    
    0 讨论(0)
  • 2020-11-22 09:47

    here is another way to do such

     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"); 
        }
    
    0 讨论(0)
  • 2020-11-22 09:48

    If you need to manipulate anything aside from the Cell Value string such as adding a Tag, try this:

    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);
    
    0 讨论(0)
  • 2020-11-22 09:49

    Like this:

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

    Or you need to set there values individually use the propery .Rows(), like this:

     dataGridView1.Rows[1].Cells[0].Value = "cell value";
    
    0 讨论(0)
  • 2020-11-22 09:50

    This is how I add a row if the dgrview is empty: (myDataGridView has two columns in my example)

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

    According to docs: "CreateCells() clears the existing cells and sets their template according to the supplied DataGridView template".

    0 讨论(0)
  • 2020-11-22 09:50
    yourDGV.Rows.Add(column1,column2...columnx); //add a row to a dataGridview
    yourDGV.Rows[rowindex].Cells[Cell/Columnindex].value = yourvalue; //edit the value
    

    you can also create a new row and then add it to the DataGridView like this:

    DataGridViewRow row = new DataGridViewRow();
    row.Cells[Cell/Columnindex].Value = yourvalue;
    yourDGV.Rows.Add(row);
    
    0 讨论(0)
提交回复
热议问题