在C#.net中以编程方式在datagridview中添加行 [英] Add row in datagridview programmatically in C#.net

查看:65
本文介绍了在C#.net中以编程方式在datagridview中添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我想在网格视图上加载数据(行)后以编程方式添加行。

例如我在datagridview中加载了三行,现在我想在最后一行中添加行并写入pay。



但是这时显示以下错误:

当控件受数据绑定时,无法以编程方式将行添加到DataGridView的行集合中。



非常感谢



方法部分:

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 :

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;
                      }
                    }

推荐答案

在数据绑定模式下使用DataGridView时,只能添加/删除/修改通过对绑定的数据源执行此操作。我假设你的数据源是一个DataTable:

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:
// 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;





编辑:




      //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;
   }
}


这篇关于在C#.net中以编程方式在datagridview中添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆