Click here to Skip to main content
14,422,431 members
Rate this:
Please Sign up or sign in to vote.
See more:
dataGridView1.Rows[0].Cells[0].Value = texstudentID.Text;
dataGridView1.Rows[0].Cells[1].Value = texstudentName.Text;
dataGridView1.Rows[0].Cells[2].Value = texstudentFamily.Text;
dataGridView1.Rows[0].Cells[3].Value = texstudentNationID.Text;
dataGridView1.Rows[0].Cells[4].Value = texstudentField.Text;
dataGridView1.Rows[0].Cells[5].Value = openFileDialog1.FileName;
dataGridView1.Rows[0].Cells[6].Value = texstudentNationID.Text;
dataGridView1.Rows.Add();


i want add a row after last row in dataGridView1.

but i face this error :
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.

pliz help me!
Posted
Updated 25-Nov-19 0:40am
Rate this:
Please Sign up or sign in to vote.

Solution 3


An example of copy row from dataGridView and added a new row in The same dataGridView
DataTable Dt = new DataTable();
Dt.Columns.Add("Column1");
Dt.Columns.Add("Column2");

DataRow dr = Dt.NewRow();
DataGridViewRow dgvR = (DataGridViewRow)dataGridView1.CurrentRow.Clone();
dr[0] = dgvR.Cells[0].Clone();
dr[1] = dgvR.Cells[1].Clone();

Dt.Rows.Add(dR);
dataGridView1.DataSource = Dt;
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

Refer to this link.It has solution to exactly what you are looking for.

Rows cannot be programmatically added to the datagridview's row collection when the control is data-bound[^]

[Additional]:

Rows cannot be programmatically...[msdn][^]

[Update]:

//create datatable and columns,
DataTable dtable = new DataTable();
dtable.Columns.Add(new DataColumn("Column 1"));
dtable.Columns.Add(new DataColumn("Column 2"));
 
//simple way create object for rowvalues here i have given only 2 add as per your requirement
object[] RowValues = { "", "" };
 
//assign values into row object
RowValues[0] = "your value 1";
RowValues[1] = "your value 2";
 
//create new data row
DataRow dRow;
dRow = dtable.Rows.Add(RowValues);
dtable.AcceptChanges();
 
//now bind datatable to gridview... 
gridview.datasource=dbtable;
gridview.databind();

Regards..:laugh:
   
v2
Comments
Mohammad Sharify 3-Jul-13 11:55am
   
thank u very much...
but this code (that i found in links):
this.Validate();
studentBindingSource.EndEdit();
dataGridView1.Refresh();
not worked !
Thanks7872 3-Jul-13 13:04pm
   
If you are using datasource to bind your datagridview then you can not add row directly to datagridview,instead you have to add rows directly to datasource.
Mohammad Sharify 4-Jul-13 5:25am
   
i want to add row just in datagridview that i can remove it,whenever i want.
Thanks7872 4-Jul-13 5:29am
   
Refer to updated answer.
Mohammad Sharify 4-Jul-13 15:53pm
   
Thank you very much for answering my question!
Ayman Jaballa 31-May-14 22:09pm
   
Your answer update working just fine ... thanks
Rate this:
Please Sign up or sign in to vote.

Solution 2

Depending on what you are trying to do, you may be able to use the GridView's Footer row. If you really don't want the row in your data source and you only want to add a single row then the footer may work for you.

Set the GridView's ShowFooter property to true.

<asp:gridview id="GridView1" runat="server" ShowFooter="true" onrowdatabound="GridView1_RowDataBound"></asp:gridview>


... And in the GridView's RowDataBound event set the values in your footer, like so...

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
      if (e.Row.RowType == DataControlRowType.Footer)
      {
          e.Row.Cells[0].Text = "value1";
          e.Row.Cells[1].Text = "value2";
          e.Row.Cells[2].Text = "value3";
          e.Row.Cells[3].Text = "value4";
          e.Row.Cells[4].Text = "value5";

      }
  }
   
v2
Comments
Mohammad Sharify 4-Jul-13 5:21am
   
Thank u
i dont understand asp codes :

<asp:gridview id="GridView1" runat="server" ShowFooter="true" onrowdatabound="GridView1_RowDataBound">

. i work with C# ...
(pliz explain above codes!)
and i realy want to add row just in datagridview.(not add to data source)
idenizeni 4-Jul-13 15:05pm
   
My mistake, I was in an ASP.NET frame of mind and misread your question. I think Rohan Leuva's revised answer is your best bet.
Mohammad Sharify 4-Jul-13 15:50pm
   
Thanks a lot!
idenizeni 5-Jul-13 18:12pm
   
Don't forget to give Rohan credit, if his answer helped, by marking his answer as an accepted solution.
Rate this:
Please Sign up or sign in to vote.

Solution 4

how to postion new data row between tow rows in the same table
   
Comments
Richard Deeming 27-Jul-18 12:28pm
   
If you want to ask a question, then ASK A QUESTION[^]. DO NOT post your question as a "solution" to someone else's question!

But be warned, you're going to need to provide a lot more information than you have here if you want someone to help you.
Rate this:
Please Sign up or sign in to vote.

Solution 6

I found other easy way to add rows.

I use the INSERT keyb to insert a new row on my DataGridView, but you can use this code into a button for example.

//capture the key press on the form
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (MyDataGrid.Focused && keyData == Keys.Insert)
    {
        int row = MyDataGrid.CurrentRow.Index;
        int myRows = MyDataGrid.Rows.Count - 1;   //control the insert is on the last row of the grid
        if (row == myRows)
        {
            this.insert = true;
            switch (MyDataGrid.CurrentCell.OwningColumn.Name.ToLower())
            {
                case "NumericColumn":   //here control is the column where the user press INSERT required a number
                    SendKeys.Send("0");
                    break;
                default:       //if not can insert a space
                    SendKeys.Send(" ");
                    break;
            }
        }
        else this.insert = false;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}


Whis this action simulate the user press a space on teh last rows, so the grid trigger the event UserAddedRow. For me this is the easy way to add a new row, on my project.

Happy coding
   
Comments
CHill60 25-Nov-19 6:34am
   
Please do not post multiple solutions to the same question. How are readers meant to know which is the correct one? You can use the green "Improve Solution" link to add extra information to an existing solution.
However, in neither of your posts have you actually answered this six year old question.
Sergi Ortiz Gomez 25-Nov-19 6:39am
   
Oh sorry, I am new in the site and I don´t know so good the rules, I´ll try to delete the previous because is much better this new solutioin.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100