简体   繁体   English

如何在不删除数据 C# 的情况下将新数据添加到数据网格视图?

[英]how add new data to data grid view without delete data C#?

hello guys need to put data on data grid view without delete this my code but this code give me only last item in list box你好,伙计们需要将数据放在数据网格视图上而不删除我的代码,但是这段代码只给了我列表框中的最后一项

for (int i = 0; i < listBox1.Items.Count; i++)
        
 {
    textBox1.Text = listBox1.Items[i].ToString();               
            
    dataGridView1.DataSource = cls_all.Get_Test3(Convert.ToSingle(textBox1.Text));
 }

tried this code试过这段代码

 dataGridView1.Rows.Add(cls_all.Get_Test3(Convert.ToSingle(textBox1.Text)));

and get this msg System.InvalidOperationException: 'Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.'并获取此消息 System.InvalidOperationException: '当控件绑定数据时,无法以编程方式将行添加到 DataGridView 的行集合中。'

need way to put data without delete data grid view any help with it需要在不删除数据网格视图的情况下放置数据的方法

Use a DataTable instead of directly trying to add rows to a grid.使用 DataTable 而不是直接尝试将行添加到网格。

DataTable dt = new DataTable();
DataColumn dc = new DataColumn("MyColumn", typeof(String));
dt.Columns.Add(dc);
for (int i = 0; i < listBox1.Items.Count; i++)
{
   textBox1.Text = listBox1.Items[i].ToString(); // I do not know why you need this 
   dt.Rows.Add(new Object[] { listBox1.Items[i].ToString()});
}
dataGridView1.ItemsSource = dt.DefaultView;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM