Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.

Answered Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.

  • 2012년 7월 27일 금요일 오후 5:55
     
     

    So I figured out that I need to add the rows the the datasorce, not the dataGridview1 but how do I do that?  I would put code up here, but I'm not sure what you guys would need... lol

    thanks!

모든 응답

  • 2012년 7월 27일 금요일 오후 6:00
     
      코드 있음

    If datasource of DatagridView1 control is DataTable then instead of adding a new row in Datagridview you have to add row in DataTable. Row added in DataTable will automatically get displayed in DataGridView. Similar to delete the item you have to delete row in DataTable

    Dim dr as DataRow = dt.NewRow
    dr.Item("Column1") = "Some Value"
    dt.Rows.Add(dr)



    Gaurav Khanna | Microsoft VB.NET MVP

  • 2012년 7월 27일 금요일 오후 6:08
     
     
    so how would I go about implementing this?
  • 2012년 7월 27일 금요일 오후 6:14
     
     
    I mean if I just copy and past that code in there, I get 11 errors...  btw, this is C#
  • 2012년 7월 28일 토요일 오후 3:07
     
     답변됨 코드 있음

    Below is C# code to add New Row in DataTable

                DataRow dr = dt.NewRow();
                dr["ColumnName"] = "Some Value"//ColumnName will name of column in dataTable
                dt.Rows.Add(dr);



    Gaurav Khanna | Microsoft VB.NET MVP

  • 2012년 7월 28일 토요일 오후 6:58
     
      코드 있음

    VB.NET

    Public Class Form1
        Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
            Dim dt As DataTable = New DataTable("Services")
            dt.Columns.Add("Identifier", GetType(Integer))
            dt.Columns.Add("Service", GetType(String))
            dt.Columns.Add("Cost", GetType(Double))
            dt.Columns("Identifier").AutoIncrement = True
            dt.Columns("Identifier").AllowDBNull = False
            dt.Columns("Identifier").ReadOnly = True
            dt.Columns("Identifier").AutoIncrementSeed = 1
            dt.Rows.Add(Nothing, "Shirt", 5.99)
            dt.Rows.Add(Nothing, "Skirt", 6.99)
            dt.Rows.Add(Nothing, "Sheet", 10.99)
            dt.Rows.Add(Nothing, "Coat", 20.99)
            dt.AcceptChanges()
            DataGridView1.DataSource = dt
            AddMoreRows()
        End Sub
        Private Sub AddMoreRows()
            Dim dt As DataTable = DirectCast(DataGridView1.DataSource, DataTable)
            dt.Rows.Add(Nothing, "Full length dress", 15.99)
            dt.Rows.Add(Nothing, "Trousers", 12.99)
            dt.Rows.Add(Nothing, "Tee shirt", 2.99)
        End Sub
    End Class

    C Sharp

    public class Form1
    {
    	private void Form1_Shown(object sender, System.EventArgs e)
    	{
    		DataTable dt = new DataTable("Services");
    		dt.Columns.Add("Identifier", typeof(int));
    		dt.Columns.Add("Service", typeof(string));
    		dt.Columns.Add("Cost", typeof(double));
    		dt.Columns["Identifier"].AutoIncrement = true;
    		dt.Columns["Identifier"].AllowDBNull = false;
    		dt.Columns["Identifier"].ReadOnly = true;
    		dt.Columns["Identifier"].AutoIncrementSeed = 1;
    		dt.Rows.Add(null, "Shirt", 5.99);
    		dt.Rows.Add(null, "Skirt", 6.99);
    		dt.Rows.Add(null, "Sheet", 10.99);
    		dt.Rows.Add(null, "Coat", 20.99);
    		dt.AcceptChanges();
    		DataGridView1.DataSource = dt;
    		AddMoreRows();
    	}
    	private void AddMoreRows()
    	{
    		DataTable dt = (DataTable)DataGridView1.DataSource;
    		dt.Rows.Add(null, "Full length dress", 15.99);
    		dt.Rows.Add(null, "Trousers", 12.99);
    		dt.Rows.Add(null, "Tee shirt", 2.99);
    	}
    	public Form1()
    	{
    		//INSTANT C# NOTE: Converted event handler wireups:
    		this.Shown += new System.EventHandler(Form1_Shown);
    	}
    }


    KSG