[英]Selecting a portion of data from a Data grid view c#
I am trying to select a portion of data from a datagridview with a mouse drag which is getting information from a text file, and then update the datagridview with this selected data. 我试图用鼠标拖动从datagridview中选择一部分数据,这是从文本文件中获取信息,然后用此选定的数据更新datagridview。
UPDATE: 更新:
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 { Index=-1 }" 解决了无法清除网格视图的最初错误,现在,当试图将选定的数据重新输入到网格中时,此行文本出现在数据网格中,数据应在其中放置:“ 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);
}
}
Here is the initial code for displaying the information in the datagridview: 这是用于在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;
}
Any help would be appreciated! 任何帮助,将不胜感激!
Cheers. 干杯。
You can not clear the DataGridView.Rows, because you have specified a datasource. 您无法清除DataGridView.Rows,因为您已指定数据源。 You must clear the DataSource of the DataGridView.
您必须清除DataGridView的DataSource。
Instead of 代替
dataGridView1.Rows.Clear();
write : 写:
dataset.Tables[0].Clear();
EDIT: You have a mistake in your Code ... You can't add a DataGridViewRow in a Table This code fixes your issue: 编辑:您的代码中有错误...您不能在表中添加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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.