DevExpress DataBinding, Add new Record - c#

I am using DevExpress in my winform application, I have a gridview, data entry form, datanavigator, all bound to dataset.
I want to add new record, if using datanavigator "Add" it works good, how to do the same using a "New Record" button?
BindingSource.AddNew()
is not working, it usually does, but with devexpress its not working.

If you want to use binding then use your objects with binding source..
and use the binding list .AddingNew += new AddingNewEventHandler(listOfParts_AddingNew);
event to add new entity object ..
See the example of BindingList on MSDN.
void listOfParts_AddingNew(object sender, AddingNewEventArgs e)
{
e.NewObject = new Part(textBox1.Text, int.Parse(textBox2.Text));
}
DevExpress WinForm Controls works so fast with binding sources as compare to typed datasources etc... YOu can implement bindingSources using these example..
set gridview and the associcated controls datasource to bindsouce that you have created...
process your form with the this MSDN example..
have a look on this code snippet.. may be you will get some idea from this..
private void BindingLIstDemo_Load(object sender, EventArgs e)
{
InitializeListOfEmployees();
BindlstEmp();
listofEmp.AddingNew += new AddingNewEventHandler(listOfEmp_AddingNew);
listofEmp.ListChanged += new ListChangedEventHandler(listofEmp_ListChanged);
}
private void BindlstEmp()
{
lstEmpList.Items.Clear();
lstEmpList.DataSource = listofEmp;
lstEmpList.DisplayMember = "Name";
}
void listofEmp_ListChanged(object sender, ListChangedEventArgs e)
{
MessageBox.Show(e.ListChangedType.ToString());
//throw new NotImplementedException();
}
//declare list of employees
BindingList<Emp> listofEmp;
private void InitializeListOfEmployees()
{
//throw new NotImplementedException();
// Create the new BindingList of Employees.
listofEmp = new BindingList<Emp>();
// Allow new Employee to be added, but not removed once committed.
listofEmp.AllowNew = true;
listofEmp.AllowRemove = true;
// Raise ListChanged events when new Employees are added.
listofEmp.RaiseListChangedEvents = true;
// Do not allow Employee to be edited.
listofEmp.AllowEdit = false;
listofEmp.Add(new Emp(1, "Niranjan", 10000));
listofEmp .Add (new Emp (2,"Jai", 8000));
}
// Create a new Employee from the text in the two text boxes.
void listOfEmp_AddingNew(object sender, AddingNewEventArgs e)
{
e.NewObject = new Emp (Convert.ToInt32(txtId.Text), txtName.Text,Convert.ToInt32(txtSalary.Text));
}
private void btnAdd_Click(object sender, EventArgs e)
{
Emp empItem = listofEmp.AddNew();
txtId.Text = txtName.Text = txtSalary.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
Form1 obj = new Form1();
obj.Show();
}
private void btnDelete_Click(object sender, EventArgs e)
{
var sg = (from sc in listofEmp.ToList<Emp>() where sc.Name == ((Emp)lstEmpList.SelectedValue).Name select sc);
}
private void lstEmpList_SelectedIndexChanged(object sender, EventArgs e)
{
Emp se = listofEmp[lstEmpList.SelectedIndex];
txtId.Text = se.Id.ToString();
txtName.Text = se.Name;
txtSalary.Text = se.Salary.ToString();
}
Here I am using BindingList as datasouce BindingList<Emp> listofEmp; and on the place of grid listing of records are shown in a listbox control.. but all same...try this with your gridview..

Related

How to cause a Control follow its DataSource?

At the beginning, when I assign my collection to a control's DataSource, it follows what is in the collection. During the execution of my program, when the collection changes, I don't see any change in the control's content. How can I make the control follow the latest changes of the collection?
What I have is:
private void FormEditImages_Load(object sender, EventArgs e)
{
radListView1.DataSource = WebServiceHelper.Instance.CurrentSession.Images;
radListView1.DisplayMember = "Name";
radListView1.ValueMember = "Id";
}
private void radListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
var cameraOverlayImage = e.Item.DataBoundItem as CameraOverlayImage;
e.Item.Image = CreateThumbnail(cameraOverlayImage.Image, e.ListViewElement.ItemSize.Width - 12, e.ListViewElement.ItemSize.Height - 16, e.ListViewElement.BackColor);
e.Item.TextAlignment = ContentAlignment.MiddleCenter;
e.Item.TextImageRelation = TextImageRelation.ImageAboveText;
e.Item.ImageAlignment = ContentAlignment.MiddleCenter;
}
How can I change the ItemDataBound method when I change Load to:
private void FormEditImages_Load(object sender, EventArgs e)
{
var bindingSource = new BindingSource(WebServiceHelper.Instance.CurrentSession.Images, "Name");
radListView1.DataSource = bindingSource;
}

Gridview Update

When I GridView data Update in other forms then data update but when close this form then gridview data refresh not response automatically in C# win-forms application
Here my Code:
private void button1_Click(object sender, EventArgs e)
{
aCatagory=new tbl_Catagory();
aContext = new RM_Inventory_DataContext();
var product = (from p in aContext.tbl_Catagories
where p.CatagoriesID == Convert.ToDecimal(textBox1.Text)
select p).Single();
product.CatagoriesName = textBox2.Text;
aContext.SubmitChanges();
this.Close();
AddCatagories addCatagories=new AddCatagories();
addCatagories.GridView();
}
public void GridView()
{
gridControl1.DataSource = new RM_PM_Inverntory_Management_System.RM_Inventory_DataContext().tbl_Catagories;
}
You are not binding gridview after assigning the datasource.
public void GridView()
{
gridControl1.DataSource = What ever Data Source You have
gridControl1.DataBind();
}

EF 4.0 Datagridview not update

I am using EntityFramework.
The DELETE function will delete the selected customer and refresh the datagridview, but the ADD function did not refresh the datagridview with the new added customer. Any idea?
public CustomerDialog()
{
InitializeComponent();
nw = new northwindEntities();
}
private void CustomerDialog_Load(object sender, EventArgs e)
{
dgvCustomer.DataSource = nw.Customers;
}
private void btnDelete_Click(object sender, EventArgs e)
{
string strSelectedCustomerID = getSelectedCustomerID();
Customer customer = nw.Customers.Where(a => a.CustomerID == strSelectedCustomerID).First();
nw.Customers.DeleteObject(customer);
nw.SaveChanges();
}
//the new customer is persist on the database, but the dgvCustomer is not update.
private void btnAdd_Click(object sender, EventArgs e)
{
Customer newCustomer = new Customer() {
CustomerID = txtCustomerID.Text,
CompanyName = txtCompanyName.Text,
ContactName = txtContactName.Text
};
nw.Customers.AddObject(newCustomer);
nw.SaveChanges();
dgvCustomer.DataSource = nw.Customers ;
dgvCustomer.Refresh();
}
You may try calling BindingSource.ResetBindings() method after you called the SaveChanges() method.
Also, it could help to use a BindingList as the data source (see the article's code example how to use it).
Ugly, but pragmatic approach: dgvCustomer.DataSource = null; dgvCustomer.DataSource = nw.Customers;
As a side note, dgvCustomer.Refresh() will not help you in this case. It does not refresh the data binding, it will cause the control to redraw itself in the UI, which most is likely not what you intended.

Blank row is added to my DataGridView when working with a binding list

I have a DataGridView that I can add test data to my data grid view but when I want to enter anything manually (through textboxes and drop down boxes), I just get a blank row added to my data grid view
This is the code I have: This is to get to the form where data is entered manually
private void button1_Click(object sender, EventArgs e)
{
frmAddReptile frmAddReptile = new frmAddReptile();
frmAddReptile.ShowDialog();
_ReptileList.Add(_Reptile);
}
Then, on that form I have
internal Reptile _Reptile;
private void button1_Click(object sender, EventArgs e)
{
_Reptile = new Reptile();
_Reptile.Name = txtName.Text;
_Reptile.Country = Convert.ToString(cbxCountry.SelectedItem);
_Reptile.Province = Convert.ToString(cbxProvince.SelectedItem);
_Reptile.Town = Convert.ToString(cbxTown.SelectedItem);
_Reptile.Collector = txtCole.Text;
_Reptile.Length = Convert.ToInt32(txtLen.Text);
_Reptile.Coordinates =txtCo.Text;
_Reptile.Weight = Convert.ToInt32(txtwe.Text);
_Reptile.Date = Convert.ToString(dtpDate.Value.ToShortDateString());
this.Close();
}
Then, once the button is clicked only a black row added to my data grid view.
Ok i have now figured out if i add everything to the samewindows form it works(adds to the data grid view)
private void button1_Click(object sender, EventArgs e)
{
_Reptile = new Reptile();
_Reptile.Name = txtName.Text;
_Reptile.Country = Convert.ToString(cbxCountry.SelectedItem);
_Reptile.Province = Convert.ToString(cbxProvince.SelectedItem);
_Reptile.Town = Convert.ToString(cbxTown.SelectedItem);
_Reptile.Collector = txtCole.Text;
_Reptile.Length = Convert.ToInt32(txtLen.Text);
_Reptile.Coordinates =txtCo.Text;
_Reptile.Weight = Convert.ToInt32(txtwe.Text);
_Reptile.Date = Convert.ToString(dtpDate.Value.ToShortDateString());
_ReptileList.Add(_Reptile);
}
this is what i have now but how can i make it add data form a differnt form
does it have something to do with internal Reptile _Reptile;?

How to Add new row in Devexpress gridview, When we set DataSource from List<CITEM> objlst

How to Add new row in Devexpress gridview, When we set DataSource from List objlst
I want to add new row at runtime.
private void SetData()
{
List<CITEM> lstItem = new List<CITEM>();
gridControl1.DataSource = lstItem;
gridView1.PopulateColumns();
}
private void button1_Click(object sender, EventArgs e)
{
gridView1.AddNewRow();
}
It will work if you change your list to a bindinglist
private void button1_Click(object sender, EventArgs e)
{
object[] obj = //your row object;
gridView1.AddNewRow();
gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0], obj[0].ToString());
gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[1], obj[1].ToString());
gridView1.UpdateCurrentRow();
}

Categories

Resources