Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a Row After Setting DataSource to Datagridview

I had lots of questions related to datasource binding of datagrid. I had a DatagridView to which I am setting DataSource from a list

List<Myclass> li = new List<MyClass>();

MyClass O = new MyClass();
O.Name = "Aden";
O.LastName = "B";
O.Id = 12;
li.Add(O);
O = new MyClass();
O.Name = "Li";
O.LastName = "S";
O.Id = 22;
li.Add(O);

Mydgv.DataSource = li;

Where MyClass is

public Class MyClass
{
 public string Name {get; set;}
 public string LastName {get; set;}
 public decimal Id {get; set;}
}

Now Want to add a new Row to My DataGridView

DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells[0].Value = "XYZ";
row.Cells[1].Value = 50.2;
Mydgv.Rows.Add(row);

But it is not possible it raise error because Datasource of Datagrid is Binded with List li. So My first question is how could I do this?

My second question is, For doing this I made a solution to alter my List li and Add New row of data to it and then set it to datasource of datagrid but I think its not a feasible solution is there any better solution?

Even tried to do it by CurrencyManager

CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[MyGrid.DataSource];
currencyManager1.SuspendBinding();
DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells[0].Value = "XYZ";
row.Cells[1].Value = 50.2;
Mydgv.Rows.Add(row);
currencyManager1.ResumeBinding();

As I know through it I suspended Binding of DataGrid to provide formatting to Grid as I performed in one of my post Make Row Visible false. But why it doesn't work here in adding row to datagrid?

like image 455
Amit Bisht Avatar asked Dec 07 '13 08:12

Amit Bisht


People also ask

What is DataGridViewRow?

The DataGridViewRow class is used to access the individual cell elements, as well as to adjust the appearance and behavior of the row user interface (UI), such as height and cell style. Typically, you will want all rows or most rows in the control to share the same characteristics.


1 Answers

Try using a BindingList instead of List:

BindingList<MyClass> li = new BindingList<MyClass>();

Then you add or delete records from the list itself:

li.Add(new MyClass() { Id = 15, LastName = "Z", Name = "Agent" });

and the grid will automatically show that new row.

To have the individual property updates automatically appear in the grid, then your class needs to implement the INotifyPropertyChanged interface:

public class MyClass : INotifyPropertyChanged {
  public event PropertyChangedEventHandler PropertyChanged;

  protected void OnPropertyChanged(string propertyName) {
    if (PropertyChanged != null) {
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
  }

and then your property would have to raise the event:

private string lastName = string.Empty;

public string LastName {
  get { return lastName; }
  set {
    if (value != lastName) {
      lastName = value;
      OnPropertyChanged("LastName");
    }
  }
}

Now if you update a row from code, the grid will show that update:

li[1].LastName = "Q";

Also see Implementing INotifyPropertyChanged - does a better way exist?

like image 137
LarsTech Avatar answered Oct 14 '22 09:10

LarsTech