我有一个 DataGridView。一些单元从串行端口接收数据:我想将数据推送到单元中,并让它更新底层绑定对象。
我正在尝试这样的事情:
SetValueFromSerial (decimal newValue)
{
dataGridView.CurrentCell.Value = newValue;
}
使用字符串没有帮助:
dataGridView.CurrentCell.Value = newValue.ToString ();
在这两种情况下,我在网格中看不到任何内容,并且基础值没有变化。
我用谷歌搜索了这里,但没有找到任何东西。 (我可能错过了一些东西,也许是一些显而易见的东西,但我并不是“完全”懒惰。)
DataGridView
是数据绑定的,则不应直接修改单元格的内容。相反,您应该修改数据绑定对象。您可以通过
DataBoundItem
的 DataGridViewRow
访问该对象:MyObject obj = (MyObject)dataGridView.CurrentRow.DataBoundItem;
obj.MyProperty = newValue;
请注意,绑定对象应实现
INotifyPropertyChanged
,以便更改反映在
DataGridView
中
1.手动添加列:
DataGridViewColumn c = new DataGridViewColumn();
DataGridViewCell cell = new DataGridViewTextBoxCell();
c.CellTemplate = cell;
c.HeaderText = "added";
c.Name = "added";
c.Visible = true;
dgv.Columns.Insert(0, c);
2.在 DataBindingComplete 事件中执行如下操作:
foreach (DataGridViewRow row in dgv.Rows)
{if (row.Cells[7].Value.ToString()=="1")
row.Cells[0].Value = "number one"; }
(只是一个愚蠢的例子)
但请记住它必须位于 DataBindingComplete 中,否则值将保留为空白
dataGridView1.ReadOnly = false; //Before modifying, it is required.
dataGridView1.Rows.Add(); //Inserting first row if yet there is no row, first row number is '0'
dataGridView1.Rows[0].Cells[0].Value = "Razib, this is 0,0!"; //Setting the leftmost and topmost cell's value (Not the column header row!)
dataGridView1[1, 0].Value = "This is 0,1!"; //Setting the Second cell of the first row!
注:
之前我已经在设计模式下设计了列。
以下对我来说很好用
mydatgridview.Rows[x].Cells[x].Value="test"
mydatagridview.enabled = false
mydatagridview.enabled = true
dataGridView.Rows[rowIndex].Cells[columnIndex].Value = "New Value";
dataGridView.UpdateCellValue(columnIndex, rowIndex);
希望对您有所帮助。 =)
dataGridView.CurrentCell.Value = newValue;
dataGridView.EndEdit();
dataGridView.CurrentCell.Value = newValue;
dataGridView.EndEdit();
需要写两遍...
进行现场声明。
Private _currentDataView as DataView
因此,循环遍历所有行并搜索包含我知道位于我要更改的单元格旁边的值的单元格对我来说很有效。
Public Sub SetCellValue(ByVal value As String)
Dim dataView As DataView = _currentDataView
For i As Integer = 0 To dataView.Count - 1
If dataView(i).Row.Item("projID").ToString.Equals("139") Then
dataView(i).Row.Item("Comment") = value
Exit For ' Exit early to save performance
End If
Next
End Sub
以便您更好地理解。 我知道 ColumnName“projID”是 139。我循环直到找到它,然后我可以在我的案例“Comment”中更改“ColumnNameofCell”的值。我用它来添加运行时的注释。
Dim selectedRow As DataRowView
selectedRow = dg.Rows(dg.CurrentCell.RowIndex).DataBoundItem
selectedRow("MyProp") = "myValue"
dg.NotifyCurrentCellDirty(True)
感谢最后一排的 Saeed Serpooshan
String
值似乎与 DataGridView 单元格不兼容(尽管我没有尝试过或尝试过任何技巧)。
DataGridViewName.Rows[0].Cells[0].Value = 1;
这是我的示例 - 数据源首先是 DataTable,我将其传输到 List,然后创建 BindingList。然后,我创建 BindingSource 并使用 BindingList 作为 BindingSource 中的数据源。最后,DataGridView 中的 DataSource 使用此 BindingSource。
sp_Select_PersonTableAdapter adapter = new sp_Select_PersonTableAdapter();
DataTable tbl = new DataTable();
tbl.Merge(adapter.GetData());
List<Person> list = tbl.AsEnumerable().Select(x => new Person
{
Id = (Int32) (x["Id"]),
Ime = (string) (x["Name"] ?? ""),
Priimek = (string) (x["LastName"] ?? "")
}).ToList();
BindingList<Person> bindingList = new BindingList<Person>(list);
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = bindingList;
dgvPerson.DataSource = bindingSource;
同样非常重要的是:每个类的成员setter必须调用OnPropertyChanged()。没有它,它就行不通。所以,我的课程看起来像这样:
public class Person : INotifyPropertyChanged
{
private int _id;
private string _name;
private string _lastName;
public int Id
{
get { return _id; }
set
{
if (value != _id)
{
_id = value;
OnPropertyChanged();
}
}
}
public string Name
{
get { return _name; }
set
{
if (value != _name)
{
_name = value;
OnPropertyChanged();
}
}
}
public string LastName
{
get { return _lastName; }
set
{
if (value != _lastName)
{
_lastName= value;
OnPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
有关此主题的更多信息:http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx
第一次运行顺利,但第二次按时出现错误 “索引超出范围。必须为非负值且小于集合的大小。 参数名称:索引”,但是当我单击单元格时,会出现另一行,然后它适用于下一行,并继续...
DataGridView
已由
DataSource = x
填充(即数据绑定),那么您需要更改 绑定数据,而不是 DataGridView 单元格本身。 从已知行或列获取数据的一种方法是:
(YourRow.DataBoundItem as DataRowView).Row['YourColumn'] = NewValue;
DataTable dt = dgv.DataSource as DataTable; dt.Rows.Add(newValue); dgv.Rows[dgv.Rows.Count-1].Selected = true;