简体   繁体   English

vb.net更改数据绑定datagridview中单元格的值

[英]vb.net change value of a cell in a databound datagridview

I have a datagridview that has been populated from a datatable using the datasource property. 我有一个datagridview,它使用datasource属性从数据表中填充。 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. 此外,我不想更改数据绑定项。

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? 或许还有其他一些解决这个问题的方法?

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 . 您应该使用DataTable~ column [ Expression`属性] 1 Something like this: 像这样的东西:

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;
}

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

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