Click here to Skip to main content
15,918,617 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I want to add row programmatically in grid view after loading data(rows) on it.
for example i loaded three rows in datagridview , now I wanna add row in last of rows and write in it "pay".

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

thanks a lot

the section of method :
C#
string re_select_mablagh_ghest = string.Empty;
                        string re_serial_kala = string.Empty;
                        string ret_mablgh_soud_yek_month = string.Empty;
                        string ret_mablagh_ghest_yek_month = string.Empty;
                        string re_sele_tedad_tasvie_ghest = string.Empty;
                        DataTable mab_kal = new DataTable();
                        mab_kal = ff.select_mablagh_ghest(code_m_buyer.Text.ToString(), comboBox1.Text.ToString(), comboBox2.Text.ToString());
                        foreach (DataRow ii in mab_kal.Rows)
                        {
                            re_select_mablagh_ghest = ii["mablag_ghest"].ToString();
                        }

                        DataTable qw = new DataTable();
                        qw = ff.sele_serial_kal(code_m_buyer.Text.ToString(), re_select_mablagh_ghest, comboBox1.Text.ToString(), comboBox2.Text.ToString());
                        foreach (DataRow qq in qw.Rows)
                        {
                            re_serial_kala = qq["serial_kala"].ToString();
                        }

                        string ted_fu_ag = ff.tedad_full_aghsat(code_m_buyer.Text.ToString(), comboBox1.Text.ToString(), re_serial_kala);

                        DataTable mab_soud_yek_mo = new DataTable();
                        mab_soud_yek_mo = ff.mablagh_soud_yek_mon(code_m_buyer.Text.ToString(), comboBox1.Text.ToString(), re_serial_kala);
                        foreach (DataRow cx in mab_soud_yek_mo.Rows)
                        {
                            ret_mablgh_soud_yek_month = cx["soud_mounth"].ToString();
                        }

                        DataTable mablagh_ghest_yek_mon = new DataTable();
                        mablagh_ghest_yek_mon = ff.mablagh_ghest_yek_mon(code_m_buyer.Text.ToString(), comboBox1.Text.ToString(), re_serial_kala);
                        foreach (DataRow qt in mablagh_ghest_yek_mon.Rows)
                        {
                            ret_mablagh_ghest_yek_month = qt["mablag_ghest"].ToString();
                        }

                        re_sele_tedad_tasvie_ghest = ff.sele_tedad_pay_aghsat(code_m_buyer.Text.ToString(), comboBox1.Text.ToString(), "pay", re_serial_kala);

                        double re_sum = 0;
                        if (comboBox2.Text != string.Empty)
                        {
                            re_sum = ff.tasvie_befor_moed(Convert.ToDouble(ted_fu_ag), Convert.ToDouble(re_sele_tedad_tasvie_ghest), Convert.ToDouble(ret_mablgh_soud_yek_month), Convert.ToDouble(ret_mablagh_ghest_yek_month));
                        }
                        else
                        {
                            MessageBox.Show(" please enter your serial : ");
                            return;
                        }
                      DialogResult aa  =  MessageBox.Show(string.Concat(string.Concat("money : ", re_sum), "\n do you sure  "), " ", MessageBoxButtons.OKCancel);
if (aa == DialogResult.OK)
{
                            
DataTable gg = new DataTable();
string re_sum_pay_true = string.Empty;
                          ff.delete_ghesting_with_status_pay_where(code_m_buyer.Text.ToString(), comboBox2.Text, "don't pay");
                           
re_sum_pay_true = ff.sum_status_pay_that_true(code_m_buyer.Text, "pay", comboBox2.Text);


                          //your code

                          DataTable dt = new DataTable();
                          DataSet ds = new DataSet();

                          dt = ghestingTableAdapter.GetData();
                          DataRow newRow = dt.NewRow();
                          newRow["Explain"] = "pay";
                          dt.Rows.Add(newRow);
                          dataGridView1.DataSource = dt.DefaultView;



gg=ff.select_ghesting_whith_where_coloumn_pay_and_dont_pay(code_m_buyer.Text,comboBox2.Text);
                          dataGridView1.DataSource = gg;
                      }
                      else
                      {
                          return;
                      }
                    }
Posted
Updated 8-Apr-15 4:07am
v4
Comments
[no name] 7-Apr-15 6:22am    
Hi,
Ref : Click here
bernova 7-Apr-15 16:24pm    
thanks.but I have same error

1 solution

When using a DataGridView in databound-mode you can only add/remove/modify its rows by doing this to the bound datasource. I assume your datasource is a DataTable:
C#
// somewhere you get your DataTable from..
DataTable dt = DataTableFromSomewhere();

// create a new DataRow
DataRow newRow = dt.NewRow();

// fill it
newRow["someColumnName"] = "pay";

// add the DataRow to the DataTable
dt.Rows.Add(newRow);

// bind the DataTable to the DataGridView
yourDataGridView.DataSource = dt.DefaultView;


Edit:
C#
      //your code

      DataTable dt = ff.select_ghesting_whith_where_coloumn_pay_and_dont_pay(code_m_buyer.Text,comboBox2.Text);
      DataRow newRow = dt.NewRow();
      newRow["Explain"] = "pay";
      dt.Rows.Add(newRow);
      dataGridView1.DataSource = dt.DefaultView;
   }
   else
   {
      return;
   }
}
 
Share this answer
 
v2
Comments
bernova 8-Apr-15 8:40am    
Edit your code , but don't create row in last of rows
DataTable dt = new DataTable();
DataSet ds = new DataSet();

dt = ghestingTableAdapter.GetData();
DataRow newRow = dt.NewRow();
newRow["Explain"] = "pay";
dt.Rows.Add(newRow);
dataGridView1.DataSource = dt.DefaultView;
Sascha Lefèvre 8-Apr-15 8:47am    
Please elaborate - what is the problem with that?
bernova 8-Apr-15 8:56am    
don't create row in last of rows
Sascha Lefèvre 8-Apr-15 9:03am    
Does it create a new row but not as the last one? Or does it not create a new row at all?
bernova 8-Apr-15 9:07am    
don't create new row at all

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