Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote a code to add value to datagridview from textbox.............

C#
private void button1_Click(object sender, EventArgs e)
        {

            crsView.Rows.Add(txtName.Text, txtCredit.Text);
           

        }




but when I run the code the following message shows:


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

Please help me to ahead.
thanks in advance
Posted
Comments
milindaSnow 25-Mar-14 2:21am    
Have you bound a data source to the DataGridView ?. that might be the problem..
if so, instead of adding a record to the dataGridView, add it to the DataSource

1 solution

try this:-
Take data-table and add the row in it. Then bond that table as datasourse.
If you want to append to last row then add previous rows in data table then add new row.
C#
private void button1_Click(object sender, EventArgs e)
        {
              DataTable table = new DataTable();
              table.Rows.Add(txtName.Text.trim(),txtCredit.Text.trim());
              crsView.datasource=table;
              crsView.DataBind();
        }
 
Share this answer
 
Comments
nest101234 25-Mar-14 2:26am    
this code now show the message below:

"Input array is longer than the number of columns in this table."

please help me @TrushnaK
TrushnaK 25-Mar-14 2:46am    
it seems that you haven't bind anything in gridview.
take a look a new code.
private void button1_Click(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Credit", typeof(string));
table.Rows.Add(txtName.Text.trim(),txtCredit.Text.trim());
crsView.datasource=table;
}

but this code added columns first time to gridview.
better you create that table structure at loading time then load rows as multiple times in that.
nest101234 25-Mar-14 3:45am    
Thanks a lot.....@TrushnaK

It works...:-)
TrushnaK 25-Mar-14 5:32am    
welcome... mark it as answer if you think it satisfy.
nest101234 28-Mar-14 12:22pm    
how can i mark it as answer???? I found no option!!!!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900