从数据网格视图中选择一部分数据 c#
Selecting a portion of data from a Data grid view c#
我正在尝试 select 使用鼠标拖动从文本文件获取信息的 datagridview 中的一部分数据,然后使用此 selected 数据更新 datagridview。
更新:
无法清除网格视图的初始错误已解决,现在当 selected 数据试图重新输入网格时,这行文本出现在数据网格中数据应该是:
"DataGridViewRow { Index=-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);
}
}
这是在数据网格视图中显示信息的初始代码:
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;
}
如有任何帮助,我们将不胜感激!
干杯。
您无法清除 DataGridView.Rows,因为您指定了数据源。
您必须清除 DataGridView 的数据源。
而不是
dataGridView1.Rows.Clear();
写:
dataset.Tables[0].Clear();
编辑:
您的代码有误...您不能在 Table 中添加 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);
}
我正在尝试 select 使用鼠标拖动从文本文件获取信息的 datagridview 中的一部分数据,然后使用此 selected 数据更新 datagridview。
更新:
无法清除网格视图的初始错误已解决,现在当 selected 数据试图重新输入网格时,这行文本出现在数据网格中数据应该是: "DataGridViewRow { Index=-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);
}
}
这是在数据网格视图中显示信息的初始代码:
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;
}
如有任何帮助,我们将不胜感激!
干杯。
您无法清除 DataGridView.Rows,因为您指定了数据源。 您必须清除 DataGridView 的数据源。
而不是
dataGridView1.Rows.Clear();
写:
dataset.Tables[0].Clear();
编辑: 您的代码有误...您不能在 Table 中添加 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);
}