将行添加到datagridview [英] add row to datagridview

查看:97
本文介绍了将行添加到datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码从数据库填充了datagridview

 DataSet ds =  DataSet();
            OleDbDataAdapter da =  OleDbDataAdapter(" ,tool.getconnection());
            //  OleDbDataAdapter da =新的OleDbDataAdapter(,tool.getconnection()); 
            da.Fill(ds);
            BindingSource bds =  BindingSource(ds,ds.Tables [ 0 ].TableName);
            dataGridView1.DataSource = bds; 



我试图使用以下代码添加行

 dataGridView1.Rows.Add(" " );



但发生错误:
当控件是数据绑定的时,不能以编程方式将行添加到DataGridView的行集合中.
我该如何解决这个问题
此致

http://csharp.net-informations.com/datagridview/csharp-datagridview-database- Operations.htm [ ^ ]


using (OleDbConnection connection = new OleDbConnection(@ExtraConnection))
      {

          OleDbCommand command = new OleDbCommand("SELECT * FROM "+txtSheet.Text, connection);
              command.CommandTimeout = 20;
              connection.Open();
              OleDbDataAdapter SDA = new OleDbDataAdapter(command);
              OleDbCommandBuilder SCB = new OleDbCommandBuilder(SDA);
              DataSet m_DataSet = new DataSet();
              SDA.Fill(m_DataSet);
              connection.Close();
                foreach (DataTable table in m_DataSet.Tables)
                  foreach (DataRow row in table.Rows)
                      data.Rows.Add(row.ItemArray);


      }


 DataTable数据=  new  DataTable();
     
        公共 StudentBatchSave()
        {
            InitializeComponent();

             .data.Columns.Add(" ,Type.GetType(" )));
             .data.Columns.Add(" ,Type.GetType(" )));
             .data.Columns.Add(" ,Type.GetType(" )));
         
             .dataGridView2.DataSource =数据;
        } 


I used the following code to fill my datagridview from database

DataSet ds = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter("select ID, [number], [date], name, place, placeID, numbertype, [note], [time] FROM CallsRegistray ORDER BY [date] DESC ", tool.getconnection());
            //OleDbDataAdapter da = new OleDbDataAdapter(, tool.getconnection());
            da.Fill(ds);
            BindingSource bds = new BindingSource(ds, ds.Tables[0].TableName);
            dataGridView1.DataSource = bds;



I tried to use the following code to add row

dataGridView1.Rows.Add("test", "test");



but there is an error occur :
Rows cannot be programmatically added to the DataGridView''s rows collection when the control is data-bound

how can I resolve this problem
best regards

解决方案

http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/8db841fc-ffa7-4519-b6f5-d054c7190948[^]
http://csharp.net-informations.com/datagridview/csharp-datagridview-database-operations.htm[^]


using (OleDbConnection connection = new OleDbConnection(@ExtraConnection))
      {

          OleDbCommand command = new OleDbCommand("SELECT * FROM "+txtSheet.Text, connection);
              command.CommandTimeout = 20;
              connection.Open();
              OleDbDataAdapter SDA = new OleDbDataAdapter(command);
              OleDbCommandBuilder SCB = new OleDbCommandBuilder(SDA);
              DataSet m_DataSet = new DataSet();
              SDA.Fill(m_DataSet);
              connection.Close();
                foreach (DataTable table in m_DataSet.Tables)
                  foreach (DataRow row in table.Rows)
                      data.Rows.Add(row.ItemArray);


      }


DataTable data = new DataTable();
     
        public StudentBatchSave()
        {
            InitializeComponent();

            this.data.Columns.Add("StudentCode", Type.GetType("System.Int64"));
            this.data.Columns.Add("StudentName", Type.GetType("System.String"));
            this.data.Columns.Add("StudentFamily", Type.GetType("System.String"));
         
            this.dataGridView2.DataSource = data;
        }


这篇关于将行添加到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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