vb.net 更改数据绑定 datagridview 中单元格的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13138478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vb.net change value of a cell in a databound datagridview
提问by Osprey
I have a datagridview that has been populated from a datatable using the datasource property. I need to change the value of a cell without affecting the database. So far I could only find that I have to first change the databound item but no information about how to do that. Besides, I don't want to change the databound item.
我有一个使用 datasource 属性从数据表填充的 datagridview。我需要在不影响数据库的情况下更改单元格的值。到目前为止,我只能发现我必须首先更改数据绑定项,但没有关于如何执行此操作的信息。此外,我不想更改数据绑定项。
The reason for wanting to do this is that I have a list of records with a start and end time and there is a computed field that gives the difference in seconds. I need to change the value of the computed field and change the height of the row accordingly, that is, the height of each row is proportional to the duration it represents. Changes are done from a text box which increments quickly if the "increase" button is kept down and therefore I cannot change the database with each increment. Rather I would change the value in the table frequently and update the database once the increments stops.
想要这样做的原因是我有一个带有开始和结束时间的记录列表,并且有一个计算字段以秒为单位给出差异。我需要改变计算域的值,并相应地改变行的高度,即每行的高度与它所代表的持续时间成正比。更改是从一个文本框完成的,如果“增加”按钮保持按下状态,该文本框会快速增加,因此我无法在每次增加时更改数据库。相反,我会经常更改表中的值,并在增量停止后更新数据库。
I have tried updating the datagridview directly using this code:
我尝试使用以下代码直接更新 datagridview:
dgvTasks.Rows(SelectedRowIndex).Cells(5).Value = DateAdd(DateInterval.Minute, DateDiff(DateInterval.Minute, CDate("00:00:00"), CDate(txtDuration.Text & ":00")), dgvTasks.Rows(SelectedRowIndex).Cells(4).Value)
But receive the error:
但收到错误:
System.Data.ReadOnlyException: Column 'Column1' is read only.
System.Data.ReadOnlyException: 列 'Column1' 是只读的。
I also tried updating the datatable, and allowing the calculated column to update:
我还尝试更新数据表,并允许计算列更新:
dtblTasks.Rows(SelectedRowIndex)(5) = DateAdd(DateInterval.Minute, DateDiff(DateInterval.Minute, CDate("00:00:00"), CDate(txtDuration.Text & ":00")), dtblTasks.Rows(SelectedRowIndex)(4))
dgvTasks.DataSource = dtblTasks
But the calculated value doesn't change.
但计算出的值不会改变。
I am also providing an additional calculated value (in the SQL) where I find the difference in minutes and the append a string literal to that:
我还提供了一个额外的计算值(在 SQL 中),我找到了以分钟为单位的差异,并在其中附加了一个字符串文字:
( CAST ( DATEDIFF(MINUTE, Tasks.TaskStart, Tasks.TaskEnd) AS NVARCHAR(10) ) + ' mins') AS TaskDurationString
Is there a way to detach the grid from the datasource but still keep the data visible in the datagrid and manipulate it (without affecting the databound object)? Or perhaps some other approach to this problem?
有没有办法从数据源中分离网格,但仍然保持数据在数据网格中可见并对其进行操作(不影响数据绑定对象)?或者也许有其他方法可以解决这个问题?
回答by David Hall
Updating the calculated column in the DataTable
should work - I've just tried it, and both when I edit one of the values that are part of the calculated value, and when I change the value in code, that column is recalculated and the grid UI updates.
更新计算列中的DataTable
应该工作 - 我刚刚尝试过,并且当我编辑作为计算值一部分的值之一时,以及当我更改代码中的值时,该列将被重新计算并且网格 UI更新。
One thing to check is that you are calculating the value correctly. You should be using the DataTable~ column [
Expression` property]1. Something like this:
要检查的一件事是您计算的值是否正确。您应该使用DataTable~ column [
Expression` 属性] 1。像这样的东西:
dt.Columns["FullName"].Expression = "FirstName + LastName";
Since it appears that you don't actually need to bind any of these values to the DataTable
or persist them to the database, one possible solution is to do all of this in an unbound DataGridView
calculation column.
由于看起来您实际上不需要将这些值中的任何一个绑定DataTable
到数据库或将它们持久化到数据库中,因此一种可能的解决方案是在未绑定的DataGridView
计算列中执行所有这些操作。
To do that add a column using the designer, then in the cell validating event have something like this:
为此,使用设计器添加一列,然后在单元格验证事件中添加如下内容:
void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].Cells["TargetColumn"].Value = dataGridView1.Rows[e.RowIndex].Cells["FirstName"].Value.ToString() + dataGridView1.Rows[e.RowIndex].Cells["LastName"].Value.ToString();
dataGridView1.Rows[e.RowIndex].Height = dataGridView1.Rows[e.RowIndex].Cells["LastName"].Value.ToString().Length + dataGridView1.Rows[e.RowIndex].Cells["FullName"].Value.ToString().Length;
}