代码之家  ›  专栏  ›  技术社区  ›  XXXXX

如何在DataGridView中以编程方式设置单元格值?

  •  39
  • XXXXX  · 技术社区  · 17 年前

    SetValueFromSerial (decimal newValue)
    {
        dataGridView.CurrentCell.Value = newValue;
    }
    

        dataGridView.CurrentCell.Value = newValue.ToString ();
    

    我在谷歌上搜索了一下,但什么也没找到。(我可能错过了一些东西,也许是一些显而易见的东西,但我没有

    14 回复  |  直到 17 年前
        1
  •  33
  •   Thomas Levesque    17 年前

    DataGridView DataBoundItem DataGridViewRow

    MyObject obj = (MyObject)dataGridView.CurrentRow.DataBoundItem;
    obj.MyProperty = newValue;
    

    INotifyPropertyChanged

        2
  •  28
  •   Chuck Savage    14 年前
    dataGridView1[1,1].Value="tes";
    
        3
  •  5
  •   BornToCode    14 年前

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

    foreach (DataGridViewRow row in dgv.Rows)
    {if (row.Cells[7].Value.ToString()=="1")
    row.Cells[0].Value = "number one"; }
    

        4
  •  4
  •   Graviton    17 年前

    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!
    

    注:

        5
  •  4
  •   T30    12 年前

    你记得吗 refresh

    datagridview.refresh();
    
        6
  •  4
  •   Razib Ali    12 年前

    使用sql数据适配器更新数据等

    mydatgridview.Rows[x].Cells[x].Value="test"
    mydatagridview.enabled = false 
    mydatagridview.enabled = true 
    
        7
  •  3
  •   Mayank    11 年前

    我尝试了很多方法,唯一有效的方法是UpdateCellValue:

    dataGridView.Rows[rowIndex].Cells[columnIndex].Value = "New Value";
    dataGridView.UpdateCellValue(columnIndex, rowIndex);
    

        8
  •  1
  •   Jesse    13 年前

    dataGridView.CurrentCell.Value = newValue;
    
    dataGridView.EndEdit();
    
    dataGridView.CurrentCell.Value = newValue;
    
    dataGridView.EndEdit();
    

        9
  •  1
  •   noelicus    11 年前

    Dim selectedRow As DataRowView
    selectedRow = dg.Rows(dg.CurrentCell.RowIndex).DataBoundItem
    selectedRow("MyProp") = "myValue"
    dg.NotifyCurrentCellDirty(True)
    

        10
  •  1
  •   Matthis Kohli    10 年前

    String

    DataGridViewName.Rows[0].Cells[0].Value = 1;
    
        11
  •  1
  •   Eric Draven    10 年前

    DataGridView DataSource = x

    (YourRow.DataBoundItem as DataRowView).Row['YourColumn'] = NewValue;
    
        12
  •  0
  •   FrenkyB    12 年前

    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
    

    dataview

        13
  •  0
  •   Asif Ali    12 年前

    这是我的示例——数据源最初是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;
    

    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

        14
  •  0
  •   gamendola    9 年前
    private void btn_Addtoreciept_Click(object sender, EventArgs e)
    {            
        serial_number++;
        dataGridView_inventory.Rows[serial_number - 1].Cells[0].Value = serial_number;
        dataGridView_inventory.Rows[serial_number - 1].Cells[1].Value =comboBox_Reciept_name.Text;
        dataGridView_inventory.Rows[serial_number - 1].Cells[2].Value = numericUpDown_recieptprice.Value;
        dataGridView_inventory.Rows[serial_number - 1].Cells[3].Value = numericUpDown_Recieptpieces.Value;
        dataGridView_inventory.Rows[serial_number - 1].Cells[4].Value = numericUpDown_recieptprice.Value * numericUpDown_Recieptpieces.Value;
        numericUpDown_RecieptTotal.Value = serial_number;
    }
    

    推荐文章