【问题标题】:Selecting a portion of data from a Data grid view c#从数据网格视图c#中选择一部分数据
【发布时间】:2015-05-01 15:08:23
【问题描述】:

我正在尝试使用鼠标拖动从文本文件中获取信息的数据网格视图中选择部分数据,然后使用此选定数据更新数据网格视图。

更新:

Initial error of being unable to clear the grid view is resolved, now when the selected data is trying to be re-entered into the grid this line of text appears in the datagrid where the data should be: "DataGridViewRow { 索引=-1 }"

   private void btnUpdate_Click(object sender, EventArgs e)
    {
        List<DataGridViewRow> rowCollection = new List<DataGridViewRow>();
        foreach (DataGridViewRow row in dataGridView1.SelectedRows)
        {
            rowCollection.Add(dataGridView1.Rows[row.Index]);
        }

           dataset.Tables[0].Clear();

        foreach (DataGridViewRow row in rowCollection)
        {
           // dataGridView1.Rows.Add(row);
            dataset.Tables[tableName].Rows.Add(row);

        }
    }

这是在datagridview中显示信息的初始代码:

foreach (string row in rows)
            {
                //Adds time to the array
                var items = new List<string> { timeS };
                items.AddRange(row.Split(delimeter.ToArray()));

                speed = Convert.ToDouble(items[2]);
                //converts 'speed' to a double and divides by 10 to display the correct value
                speed = speed / 10;
                items[2] = speed.ToString();
                dataset.Tables[tableName].Rows.Add(items.ToArray());
                //Adds the interval time to Dtime for each row
                Dtime = Dtime.AddSeconds(inter);

            }
            //selects where to display the data
            this.dataGridView1.DataSource = dataset.Tables[0].DefaultView;
        }

任何帮助将不胜感激!

干杯。

【问题讨论】:

    标签: c# datagridview datasource


    【解决方案1】:

    您无法清除 DataGridView.Rows,因为您已指定数据源。 您必须清除 DataGridView 的 DataSource。

    代替

    dataGridView1.Rows.Clear();
    

    写:

    dataset.Tables[0].Clear();
    

    编辑: 您的代码中有错误...您无法在表中添加 DataGridViewRow 此代码解决了您的问题:

        foreach (DataGridViewRow row in rowCollection)
        {
            DataRow r =  dataset.Tables[tableName].NewRow();
            //
            //write the data in the DataRow and then add the datarow in your           datatable
            dataset.Tables[tableName].Rows.Add(r);
    
        }
    

    【讨论】:

    • 这已经修复了与不清除列表有关的错误,但不是我正在努力用网格视图中的选定数据重新填充它
    • 'List' 抛出错误'使用泛型类型'Systems.Collections.Generic.List' 需要 1 个类型参数,我尝试将其设置为 但它没有'不工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多